我在RStudio中编写了一个Rmarkdown文档(编译为HTML),并且有一些代码块故意产生错误.例如:
```{r}
sum(a)
```
Run Code Online (Sandbox Code Playgroud)
由于a此块没有先前的定义,因此自然会生成错误消息object 'a' not found.我想在最终的HTML文件中显示此错误消息,但当我Ctrl+Shift+K在RStudio中按"Knit HTML"时,编译器报告错误并停止编织.
那么如何knitr在编译时忽略这样的错误并将其显示在编织的HTML文档中呢?
我正在为大型数据集构建分析工作流程,但首先我必须在较小规模的数据集上验证它.我想做的是将我的"采样"数据集与实际数据集分开,方法是将它们放在这样的环境中:
sample_data<-new.env()
attach(sample_data)
# downloading sample_data sets
sample_df_1 <- some_download_function(parameters1)
sample_df_2 <- some_download_function(parameters2)
...
# doing some stuff with them
...
Run Code Online (Sandbox Code Playgroud)
然而,当我做到这一点,sample_df_1并且sample_df_2将被存储在全球环境,而不是我的sample_data环境.当然我可以使用,assign(..., envir=sample_data)但这有点乏味,我不希望它们出现在最终的代码中.
使用它也不理想,with因为它内部的代码行不能一个接一个地执行,这使得它在开发阶段相当不方便.
我希望实现的是同一种行为的debug和undebug,例如:
switch_to_env(sample_data)
# Everything done here will be done within environment "sample_data"
# And the lines of codes here can be executed one by one
switch_to_env(.GlobalEnv)
Run Code Online (Sandbox Code Playgroud)
正如@Gregor指出的那样,"设置选项"可能更好地描述了我正在寻找的内容:一个允许用户指定R REPL评估表达式的环境的选项.
谢谢!