简体   繁体   English

R Markdown HTML 输出与 R Studio 输出不匹配

[英]R Markdown HTML output doesn't match the R Studio output

I am sorry in advance if this question sounds stupid: I am at ease with R but relatively new to Markdown.如果这个问题听起来很愚蠢,我事先很抱歉:我对 R 很满意,但对 Markdown 来说相对较新。 I realize that Markdown .Rmd script is meant to be reproducible, so whatever is in the Markdown script has to come from it and not Global Environment or other script.我意识到 Markdown .Rmd 脚本是可重现的,所以 Markdown 脚本中的任何内容都必须来自它,而不是全局环境或其他脚本。 I have done a tedious work of copying my very long intial .R script ito .Rmd, with explanations, like a report.我已经完成了将我很长的初始 .R 脚本复制到 .Rmd 的繁琐工作,并附有解释,就像报告一样。 My problem is the following: after running the code in .Rmd script I get the outputs below each chunk.我的问题如下:在 .Rmd 脚本中运行代码后,我得到每个块下面的输出。 I then Knit it, and the outputs in HTML document are not the same.然后我编织它,HTML文档中的输出不一样。 The essentials are the same, but the model summaries are not.基本要素相同,但模型摘要不同。 I simply cannot understand why.我简直无法理解为什么。 I have of course tried restarting R Studio, cleaning up Global Environment and starting again from blank script.我当然尝试过重新启动 R Studio,清理全局环境并从空白脚本重新开始。 The tooth-grinding problem is, my script is long and some chunks are heavy (like imputation of missing data using MICE).磨牙的问题是,我的脚本很长,有些块很重(比如使用 MICE 对缺失数据进行插补)。 So every time I have a problem and I have to re-compute everything, it's a very long coffee break.所以每次我遇到问题,我必须重新计算一切,这是一个很长的咖啡休息时间。

While I cannot include the code for this reason, I still hope very much that someone has encountered this problem before and can share their experience.虽然由于这个原因我不能包含代码,但我仍然非常希望有人以前遇到过这个问题并且可以分享他们的经验。 I particularly want to know what happens if you leave some chunks {r eval=FALSE} and run them manually for the first time only.我特别想知道如果你留下一些块 {r eval=FALSE} 并且只在第一次手动运行它们会发生什么。 Could this be a source of the problems?这可能是问题的根源吗? If so, how do you guys Knit long computation-heavy scripts?如果是这样,你们如何编织长计算量的脚本?

Thanks very much in advance.首先十分感谢。

PS After throwing this bottle into the sea, I'll go and try splitting my script into few scripts to pinpoint the problem (and to be able to include the part that causes the problem). PS 把这个瓶子扔进海里后,我会试着把我的脚本分成几个脚本来查明问题(并能够包括导致问题的部分)。

So, apparently the bug above has the following explanation:所以,显然上面的错误有以下解释:

  • The outputs shown below the chunk codes in R.Studio(.Rmd) are based on the data held in Global Environment. R.Studio(.Rmd) 中块代码下方显示的输出基于全局环境中保存的数据。
  • The Knitted HTML, on the contrary, is rendered by running the script from .Rmd.相反,针织 HTML 是通过从 .Rmd 运行脚本来呈现的。

Normally it shouldn't pose a problem.通常它应该不会造成问题。 But if some code chunks are with eval=FALSE to skip the repeated lengthy execution (in my case, data imputation using MICE), then there's imputed data in Global Environment and non-imputed data being knitted.但是,如果某些代码块使用 eval=FALSE 来跳过重复的冗长执行(在我的情况下,使用 MICE 进行数据插补),那么全局环境中的插补数据和非插补数据正在编织。 So, the models in knitted HTML are run on incomplete set of data and are all off.因此,针织 HTML 中的模型在不完整的数据集上运行并且全部关闭。

Before receiving the suggestion with cache=TRUE, I found another workaround, which is doing all required transformations and imputations once, then saving the data with a new code chunk, then setting EVAL=FALSE for this chunk and the chunks above that no longer have to be run (even though some of them still have to be shown).在收到 cache=TRUE 的建议之前,我找到了另一种解决方法,即执行所有必需的转换和插补一次,然后使用新的代码块保存数据,然后为该块和上面不再有的块设置 EVAL=FALSE运行(即使其中一些仍然必须显示)。 Then, I import the treated data in a hidden chunk (eval=TRUE, include=FALSE) and run the rest of the training, etc. While technically it's not the best in terms of reproducibility, it saved my neck and computation time.然后,我将处理过的数据导入隐藏块(eval=TRUE,include=FALSE)并运行其余的训练等。虽然从技术上讲,它在可重复性方面不是最好的,但它节省了我的脖子和计算时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM