[英]Accessing data generated by an R script with r markdown and knitr
I am new to R markdown and knitr and haven't found the answer to this question: 我是R markdown和knitr的新手,尚未找到此问题的答案:
I have R scripts where I've written functions and have assigned data to position 1 (.GlobalEnv). 我在R脚本中编写了函数,并已将数据分配到位置1(.GlobalEnv)。 How do I access my data and run my functions within R markdown and generate the .html file with knitr?
如何在R markdown中访问数据并运行函数,并使用knitr生成.html文件?
Here's a trivial example. 这是一个简单的例子。 In a script file I generate:
在脚本文件中,我生成:
some.x.data<-1:10
some.y.data<-1:10
toy.fn<-function(){
tot<-some.x.data + some.y.data
tot
}
toy.fn() works in the script file. toy.fn()在脚本文件中工作。
My R markdown file contains: 我的R markdown文件包含:
---
title: "trivial test"
author: "me"
date: "July 9, 2015"
output: html_document
---
```{r}
plot(some.x.data, some.y.data)
toy.fn()
```
When I click knit HTML, I get the following error: 单击编织HTML时,出现以下错误:
Error in plot(some.x.data, some.y.data) : object 'some.x.data' not found Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> plot 绘图错误(some.x.data,some.y.data):找不到对象'some.x.data'调用:... withCallingHandlers-> withVisible-> eval-> eval-> plot
Thanks 谢谢
RStudio opens a new R session to knit()
your Rmd
file, so the objects in .GlobalEnv
will not be available to that session (they are two separate sessions), so when you are knitr
ing HTML
there is not way to know what some.x.data
, some.y.data
and toy.fn
is. RStudio打开一个新的R会话
knit()
您的Rmd
文件,所以在对象.GlobalEnv
将无法使用该会话(它们是两个单独的会话),所以当你knitr
荷兰国际集团HTML
没有办法知道some.x.data
, some.y.data
和toy.fn
是。
Either you need to recreate them in your Rmd
file. 要么你需要重新创建他们在您的
Rmd
文件。 If you don't want any output just do: 如果您不希望任何输出,请执行以下操作:
```{r, echo = FALSE, message = FALSE}
some.x.data<-1:10
some.y.data<-1:10
toy.fn<-function(){
tot<-some.x.data + some.y.data
tot
}
```
Full Rmd
: 完整
Rmd
:
---
title: "trivial test"
author: "me"
date: "July 9, 2015"
output: html_document
---
```{r, echo = FALSE, message = FALSE}
some.x.data<-1:10
some.y.data<-1:10
toy.fn<-function(){
tot<-some.x.data + some.y.data
tot
}
```
```{r}
plot(some.x.data, some.y.data)
toy.fn()
```
Or 要么
knit
manually by yourself: library(knitr); knit('your_file.Rmd')
自己手工
knit
: library(knitr); knit('your_file.Rmd')
library(knitr); knit('your_file.Rmd')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.