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

时间:2019-08-14 09:42:55

标签: r r-markdown rnotebook

我一直试图在R笔记本中拍摄错误,其中R块正确执行,但是当我尝试预览笔记本时,出现以下错误:

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

造成错误的代码是我的数据的弗里德曼分析(使用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并没有真正产生任何有用的结果,而且以前在Stackoverflow中似乎没有讨论过此错误。任何帮助将不胜感激。

最小的可复制示例

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

1 个答案:

答案 0 :(得分:0)

为使其正常工作,我删除了console=TRUE参数,并添加了print(Friedmantest)语句。现在,笔记本预览会打印Friedman测试的输出。现在正确的代码如下所示:

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