使knitr运行r脚本:我使用read_chunk还是source? [英] Making knitr run a r script: do I use read_chunk or source?

查看:106
本文介绍了使knitr运行r脚本:我使用read_chunk还是source?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行带有RStudio 0.97.312的R版本2.15.3.我有一个脚本可以从各种来源读取我的数据并创建多个data.tables.然后,我还有另一个r脚本,该脚本使用在第一个脚本中创建的data.tables.我想将第二个脚本转换为R markdown脚本,以便将分析结果作为报告输出.

I am running R version 2.15.3 with RStudio version 0.97.312. I have one script that reads my data from various sources and creates several data.tables. I then have another r script which uses the data.tables created in the first script. I wanted to turn the second script into a R markdown script so that the results of analysis can be outputted as a report.

相对于source,我不知道read_chunk的用途.我的read_chunk无法正常工作,但是source正常工作.无论使用哪种实例,我都无法在RStudio的工作区面板中看到对象.

I do not know the purpose of read_chunk, as opposed to source. My read_chunk is not working, but source is working. With either instance I do not get to see the objects in my workspace panel of RStudio.

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

Please explain the difference between read_chunk and source? Why would I use one or the other? Why will my .Rmd script not work

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

It does not work. I get the following message

错误:找不到对象'z'

Error: object 'z' not found

两个简单的文件...

Two simple files...

对rmd.R进行源测试

test of source to 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.

推荐答案

read_chunk()仅读取源代码(以供将来参考);它不像source()那样评估代码. read_chunk()的目的在此页面

read_chunk() only reads the source code (for future references); it does not evaluate code like source(). The purpose of read_chunk() was explained in this page as well as the manual.

这篇关于使knitr运行r脚本:我使用read_chunk还是source?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆