尝试预览 R Notebook 时出错。代码执行有效,但转换为笔记本失败

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

我一直在尝试解决 R notebook 中 R 块正确执行的错误,但是当我尝试预览 notebook 时,出现以下错误:

创建笔记本时出错:值的长度必须为 3,但 FUN(X[[1]]) 结果的长度为 1。请参见第 31 行。

产生错误的代码是我的数据的 Friedman 分析(使用 agricolae 包)。

这是正确执行并用于 Friedman 分析的块(这些是我的 R 笔记本中的代码块,但我省略了笔记本中所需的 {r} 和反勾号这个问题的目的):

包安装

if(!require(agricolae))
{
print("You are missing the package 'agricolae', we will now try to install it...")
install.packages("agricolae")
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))))

head(WineTasting)

弗里德曼检验

这是错误所在:

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

Friedmantest 代码工作正常并向控制台打印正确的结果,但无法在笔记本中预览,从而产生错误。

我已经尝试 运行 使用不同的数据集,但无济于事(我收到相同的错误消息)。 Google 并没有真正产生任何有用的结果,并且之前似乎没有在 Whosebug 中讨论过这个错误。任何帮助将不胜感激。

最小的可重现示例

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))

为了使其正常工作,我删除了 console=TRUE 参数并添加了 print(Friedmantest) 语句。笔记本预览现在打印弗里德曼检验的输出。正确的代码现在看起来像这样:

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