【问题标题】:Error when trying to Preview R Notebook. Code execution works, but conversion to notebook fails尝试预览 R Notebook 时出错。代码执行有效,但转换为笔记本失败
【发布时间】:2019-12-20 20:41:30
【问题描述】:

我一直在尝试解决 R 块正确执行的 R 笔记本中的错误,但是当我尝试预览笔记本时,我收到以下错误:

创建笔记本时出错:值的长度必须为 3,但 FUN(X[[1]])result 的长度为 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 &lt;- with(WineTasting,friedman(Taster,Wine,Taste,alpha=0.05, group=TRUE,console=TRUE))

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

我尝试使用不同的数据集运行它,但无济于事(我收到相同的错误消息)。谷歌并没有真正产生任何有用的结果,而且似乎之前在 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))

【问题讨论】:

  • 请创建一个minimal reproducible example。这样其他人就可以复制和粘贴您的代码来重现并希望解决问题。
  • 我现在为您添加了一个可重现的最小示例。
  • 感谢您的示例,尽管这不是我所要求的。 R 代码独立工作,但在 RNotebook 中失败。尝试删除console = TRUE。看来 RNotebook 不喜欢那样。
  • 或者,您可以使用普通的 R markdown 文档(即 output: html_document 而不是 output: html_notbook)。
  • 啊,我成功了。我删除了console=TRUE 参数,就像你说的那样,只是添加了一个print (Friedmantest) 声明。笔记本预览现在打印弗里德曼测试的输出。感谢您的帮助!

标签: r r-markdown rnotebook


【解决方案1】:

为了使它起作用,我删除了console=TRUE 参数,添加了print(Friedmantest)statement。笔记本预览现在打印弗里德曼测试的输出。现在正确的代码如下所示:

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

【讨论】:

    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2020-11-11
    • 2017-06-13
    • 1970-01-01
    相关资源
    最近更新 更多