【问题标题】:Making knitr run a r script: do I use read_chunk or source?让 knitr 运行 r 脚本:我使用 read_chunk 还是 source?
【发布时间】:2013-03-08 06:04:40
【问题描述】:

我正在运行 R 版本 2.15.3 和 RStudio 版本 0.97.312。我有一个脚本可以从各种来源读取我的数据并创建多个 data.tables。然后我有另一个 r 脚本,它使用在第一个脚本中创建的 data.tables。我想把第二个脚本变成一个 R markdown 脚本,这样分析的结果就可以作为报告输出。

我不知道read_chunk 的用途,而不是source。我的read_chunk 不工作,但source 工作。无论哪种情况,我都看不到 RStudio 工作区面板中的对象。

请解释read_chunksource的区别?我为什么要使用其中一个?为什么我的 .Rmd 脚本不起作用

这是一个非常简单的示例

它不起作用。我收到以下消息

错误:找不到对象“z”

两个简单的文件...

测试 rmd.R 的源代码

x <- 1:10
y <- 3:4
z <- x*y  

测试源.Rmd

Can I run another script from Rmd
========================================================

Testing if I can run "test of source to rmd.R"

```{r first part}
require(knitr)
read_chunk("test of source to rmd.R")
a <- z-1000
a
```

The above worked only if I replaced "read_chunk" with "source". I 
can use the vectors outside of the code chunk as in inline usage. 
So here I will tell you that the first number is `r a[1]`. The most 
interesting thing is that I cannot see the variables in RStudio 
workspace but it must be there somewhere.

【问题讨论】:

    标签: r knitr rstudio


    【解决方案1】:

    read_chunk()只读取源代码(供以后参考);它不会评估source() 这样的代码。 read_chunk() 的用途在 this pagethe manual 中进行了说明。

    【讨论】:

    • 我读过“代码外部化”。也许因为我不是一个“真正的”程序员,我一开始并没有意识到阅读和评估之间的区别。为什么要阅读以前的代码但不评估它?对于我想做的事情,我可以看到我显然必须使用source
    • 我还了解到,每次 knitr 运行都是一个单独的会话,并且不能使用我的 RStudio 会话中存在的对象。因此,当我编写(起草、开发)我的 .Rmd 脚本时,我需要首先简单地拉入由原始源 .r 文件生成的 .Rdata 图像文件。如果我在开发过程中重复运行我的 .Rmd 以查看我的表现,我将不得不在每次执行 source 行时等待 10-20 秒。一旦一切正常,我就可以恢复使用source,以便将来的运行获取最新数据。
    • 1.存储代码块以供将来参考; 2.如果你想要速度,使用块选项cache=TRUE。我的答案中的两个链接是我最好的解释尝试。如果您已阅读它们但仍然不理解 read_chunk(),我建议您将其抛在脑后——您可能并不真正需要它。
    • 子文档呢?它有不同的用途吗?听起来很相似“这个想法就像 LaTeX 中的命令 \input{} 或 \include{} 来管理较小部分的大型文档。”来自http://yihui.name/knitr/demo/child/ 与“您不必将 R 代码放入输入文档;使用 knitr,您可以将输入文档与 R 脚本分开”来自http://yihui.name/knitr/demo/externalization/ 我希望能快速了解基本差异(如果有的话) . +1
    • @Yihui source 似乎不起作用——为了 html 的缘故,我因为在我的代码中包含 &lt; 字符而大喊大叫。我该怎么做?
    【解决方案2】:

    没有从knitrAFAIK 中交互式运行块的选项。但是,这可以通过以下方式轻松完成:

    #' Run a previously loaded chunk interactively
    #'
    #' Takes labeled code loaded with load_chunk and runs it in the /global/ envir (unless otherwise specified)
    #'
    #' @param chunkName The name of the chunk as a character string
    #' @param envir The environment in which the chunk is to be evaluated 
    run_chunk <- function(chunkName,envir=.GlobalEnv) {
        chunkName <- unlist(lapply(as.list(substitute(.(chunkName)))[-1], as.character))    
        eval(parse(text=knitr:::knit_code$get(chunkName)),envir=envir) 
    } 
    NULL
    

    【讨论】:

      【解决方案3】:

      如果它对其他人有帮助,我发现使用read_chunk() 阅读脚本而不进行评估可能有两种用途。首先,您可能有一个包含许多块的脚本,并希望控制哪些块在哪里运行(例如,特定位置的绘图或表格)。当我想在脚本中运行所有内容时(例如,在文档的开头加载一组标准的包或自定义函数),我使用source。我已经开始在文档的早期使用read_chunk 来加载脚本,然后有选择地在我需要的地方运行我想要的块。

      其次,如果您直接或交互地使用 R 脚本,您可能需要加载包、数据等的长代码前导码。但是,如果之前主文档中的代码块已经加载了数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-06
        • 2019-12-14
        • 1970-01-01
        • 2015-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多