简体   繁体   English

Knitr全球环境

[英]Knitr global environment

I can't seem to get R markdown/knitr to see/use objects in my global environment in R. 我似乎无法让R markdown / knitr在我的全球环境中查看/使用对象。

From what I've read knitr should use the global environment as standard but every one of my objects I include in a code chunk returns the error 根据我的阅读,knitr应该使用全局环境作为标准,但我在代码块中包含的每个对象都会返回错误

## Error: object 'XXX' not found

Am I missing something really simple here? 我错过了一些非常简单的东西吗?
Do I need to manually load objects from the global environment first? 我是否需要先从全局环境手动加载对象?

Thanks in advance 提前致谢

Marty 马蒂

If you've already saved the object(s) to a file, then one clean approach for markdown purposes is as follows: 如果您已将对象保存到文件中,那么用于降价目的的一种干净方法如下:

if(file.exists("rfModel.Rda")){
  load("rfModel.Rda")} else {
modFit <- train(class~.,method="rf",data=train)
}

This effectively bypasses the lengthy model build time by only building it if it does not exist as an object yet, so that it preserves reproducibility. 这有效地绕过了冗长的模型构建时间,只有当它不作为对象存在时才构建它,这样它才能保持可重复性。 This is similar to the cache idea, but is more generalizable IMHO. 这类似于缓存的想法,但更通用的恕我直言。

It sounds like you want the same code to work with both knitr and your global environment. 听起来您希望使用相同的代码来处理knitr和您的全局环境。 This can be useful when building complicated Rmd files that require testing during construction. 在构建需要在构造期间进行测试的复杂Rmd文件时,这非常有用。

The issue lies in that knitr uses the local folder when you press knit, and does not look for the project home folder (ie your Rproj - I am assuming you use relative paths). 问题在于knitr在你按下knit时使用本地文件夹,而不是寻找项目主文件夹(即你的Rproj - 我假设你使用相对路径)。 So when you go to run code it only works for one or the other. 因此,当您运行代码时,它只适用于其中一个。 The approach around this is to write code in your Rmd using relative paths to the project folder (as you would in a normal R script), and redirect knitr to use the project home folder. 解决这个问题的方法是使用项目文件夹的相对路径在您的Rmd中编写代码(就像在普通的R脚本中一样),并重定向knitr以使用项目主文件夹。 To do this insert the following code at the top of your rmd script. 为此,请在rmd脚本的顶部插入以下代码。

 ```{r setup, include=FALSE}
    library(knitr)

    dd <- getwd()
    knitr::opts_knit$set(root.dir  = paste0(dd,'/../../'))
    knitr::opts_chunk$set(cache.path = paste0(dd,'/cache/'))
    knitr::opts_chunk$set(fig.path = paste0(dd,'/figures/')) 

    ```

This code does the following: 此代码执行以下操作:

  • first, finds the current directory for your rmd. 首先,找到rmd的当前目录。
  • second, sets the project root directory. 第二,设置项目根目录。 I keep my files two folders in, hence the '/../../' , this will need to be adjusted for your folder structure. 我保存我的文件两个文件夹,因此'/../../',这将需要调整您的文件夹结构。
  • third, you need to set the cache folder path manually, as the default setting no longer works, hence cache wont work. 第三,您需要手动设置缓存文件夹路径,因为默认设置不再有效,因此缓存不起作用。
  • finally, do the same for the figures folder, as again you need to overwrite the default. 最后,对数字文件夹执行相同操作,同样需要覆盖默认值。

Happy coding. 快乐的编码。

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

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