Search code examples
rr-markdownrnotebook

Error when trying to Preview R Notebook. Code execution works, but conversion to notebook fails


I've been trying to trouble shoot an error in R notebook where the R chunks execute correctly, but when I try to preview the notebook, I get the following error:

Error creating notebook: values must be length 3, but FUN(X[[1]])result is length 1. See line 31.

The code that creates the error is the Friedman analysis of my data (which uses agricolae package).

Here are the chunks that execute correctly and that are used for the Friedman analysis (these are code chunks from my R notebook, but I've omitted the {r} and back ticks required in the notebook for the purpose of this question):

Package installation

if(!require(agricolae))
{
print("You are missing the package 'agricolae', we will now try to install it...")
install.packages("agricolae")
library(agricolae)
}

Data frame creation


WineTasting <- data.frame(
  Taste = c(5.40, 5.50, 5.55,
            5.85, 5.70, 5.75,
            5.20, 5.60, 5.50,
            5.55, 5.50, 5.40,
            5.90, 5.85, 5.70,
            5.45, 5.55, 5.60,
            5.40, 5.40, 5.35,
            5.45, 5.50, 5.35,
            5.25, 5.15, 5.00,
            5.85, 5.80, 5.70,
            5.25, 5.20, 5.10,
            5.65, 5.55, 5.45,
            5.60, 5.35, 5.45,
            5.05, 5.00, 4.95,
            5.50, 5.50, 5.40,
            5.45, 5.55, 5.50,
            5.55, 5.55, 5.35,
            5.45, 5.50, 5.55,
            5.50, 5.45, 5.25,
            5.65, 5.60, 5.40,
            5.70, 5.65, 5.55,
            6.30, 6.30, 6.25),
  Wine = factor(rep(c("Wine A", "Wine B", "Wine C"), 22)),
  Taster = factor(rep(1:22, rep(3, 22))))

head(WineTasting)

Friedman test

This is where the error lies:

Friedmantest <- with(WineTasting,friedman(Taster,Wine,Taste,alpha=0.05, group=TRUE,console=TRUE))

The Friedmantest code works fine and prints the correct results to the console, but fails to preview in the notebook, generating the error.

I've tried running this with different data sets, to no avail (I get the same error message). Google didn't really yield any helpful results and it doesn't seem like this error has been discussed in Stackoverflow before. Any help would be appreciated.

Minimal reproducible example

library(agricolae)
WineTasting <- data.frame(
  Taste = c(5.40, 5.50, 5.55,
            5.85, 5.70, 5.75,
            5.20, 5.60, 5.50,
            5.55, 5.50, 5.40,
            5.90, 5.85, 5.70,
            5.45, 5.55, 5.60,
            5.40, 5.40, 5.35,
            5.45, 5.50, 5.35,
            5.25, 5.15, 5.00,
            5.85, 5.80, 5.70,
            5.25, 5.20, 5.10,
            5.65, 5.55, 5.45,
            5.60, 5.35, 5.45,
            5.05, 5.00, 4.95,
            5.50, 5.50, 5.40,
            5.45, 5.55, 5.50,
            5.55, 5.55, 5.35,
            5.45, 5.50, 5.55,
            5.50, 5.45, 5.25,
            5.65, 5.60, 5.40,
            5.70, 5.65, 5.55,
            6.30, 6.30, 6.25),
  Wine = factor(rep(c("Wine A", "Wine B", "Wine C"), 22)),
  Taster = factor(rep(1:22, rep(3, 22))))

Friedmantest <- with(WineTasting,friedman(Taster,Wine,Taste,alpha=0.05, group=TRUE,console=TRUE))

Solution

  • To make it work I removed the console=TRUE argument added a print(Friedmantest)statement. The notebook preview now prints the output of the Friedman Test. The correct code now looks like this:

    Friedmantest <- with(WineTasting,friedman(Taster,Wine,Taste,alpha=0.05, group=TRUE))
    print(Friedmantest)