正确的 R Markdown 代码组织
Proper R Markdown Code Organization
我一直在阅读 R Markdown (here, here, and here) 并使用它来创建可靠的报告。我想尝试使用我的代码 运行 做一些临时分析并将它们变成更具可扩展性的数据报告。
我的问题相当宽泛:是否有适当的方法来围绕 R Markdown 项目组织代码?比如说,有一个脚本可以生成所有数据结构吗?
例如:假设我有 cars
数据集,并且我已经引入了制造商的商业数据。如果我想将制造商附加到当前 cars
数据集,然后使用操纵数据集 cars.by.name
为每个公司生成单独的摘要 table 并绘制特定样本怎么办?使用 cars.import
?
编辑: 现在我打开了两个文件。一个是包含所有数据操作的 R 脚本文件:对值进行子集化和重新分类。另一个是 R Markdown 文件,我在其中构建文本以伴随各种 table 和感兴趣的图。当我从 R 脚本文件中调用一个对象时——如:
```{r}
table(cars.by.name$make)
```
我收到一条错误消息 Error in summary(cars.by.name$make) : object 'cars.by.name' not found
编辑 2: 我发现这个旧线程很有帮助。 Link
---
title: "Untitled"
author: "Jeb"
date: "August 4, 2015"
output: html_document
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
table(cars.by.name$make)
```
```{r}
summary(cars)
summary(cars.by.name)
```
```{r}
table(cars.by.name)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
plot(cars.import)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
很多时候,我有很多报告需要 运行 相同 代码但参数略有不同。分别调用我的所有 "stats" 函数,生成结果,然后仅引用是我通常做的事情。方法如下:
---
title: "Untitled"
author: "Author"
date: "August 4, 2015"
output: html_document
---
```{r, echo=FALSE, message=FALSE}
directoryPath <- "rawPath" ##Something like /Users/userid/RDataFile
fullPath <- file.path(directoryPath,"myROutputFile.RData")
load(fullPath)
```
Some Text, headers whatever
```{r}
summary(myStructure$value1) #Where myStructure was saved to the .RData file
```
您可以使用 save.image()
命令保存 RData 文件。
希望对您有所帮助!
此类问题有解决方案,已解释 here。
基本上,如果您有包含代码的 .R 文件,则无需在 .Rmd 文件中重复代码,但您可以包含 .R 文件中的代码。为此,代码块应在 .R 文件中命名,然后可以按名称包含在 .Rmd 文件中。
test.R:
## ---- chunk-1 ----
table(cars.by.name$make)
test.Rmd
仅在 .Rmd 文件之上一次:
```{r echo=FALSE, cache= F}
knitr::read_chunk('test.R')
```
对于您包含的每个块(将 chunk-1
替换为 .R 文件中该特定块的标签):
```{r chunk-1}
```
请注意,它应该留空(按原样),在 运行 时间内,您来自 .R 的代码将被带到这里,运行。
我一直在阅读 R Markdown (here, here, and here) 并使用它来创建可靠的报告。我想尝试使用我的代码 运行 做一些临时分析并将它们变成更具可扩展性的数据报告。
我的问题相当宽泛:是否有适当的方法来围绕 R Markdown 项目组织代码?比如说,有一个脚本可以生成所有数据结构吗?
例如:假设我有 cars
数据集,并且我已经引入了制造商的商业数据。如果我想将制造商附加到当前 cars
数据集,然后使用操纵数据集 cars.by.name
为每个公司生成单独的摘要 table 并绘制特定样本怎么办?使用 cars.import
?
编辑: 现在我打开了两个文件。一个是包含所有数据操作的 R 脚本文件:对值进行子集化和重新分类。另一个是 R Markdown 文件,我在其中构建文本以伴随各种 table 和感兴趣的图。当我从 R 脚本文件中调用一个对象时——如:
```{r}
table(cars.by.name$make)
```
我收到一条错误消息 Error in summary(cars.by.name$make) : object 'cars.by.name' not found
编辑 2: 我发现这个旧线程很有帮助。 Link
---
title: "Untitled"
author: "Jeb"
date: "August 4, 2015"
output: html_document
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
table(cars.by.name$make)
```
```{r}
summary(cars)
summary(cars.by.name)
```
```{r}
table(cars.by.name)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
plot(cars.import)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
很多时候,我有很多报告需要 运行 相同 代码但参数略有不同。分别调用我的所有 "stats" 函数,生成结果,然后仅引用是我通常做的事情。方法如下:
---
title: "Untitled"
author: "Author"
date: "August 4, 2015"
output: html_document
---
```{r, echo=FALSE, message=FALSE}
directoryPath <- "rawPath" ##Something like /Users/userid/RDataFile
fullPath <- file.path(directoryPath,"myROutputFile.RData")
load(fullPath)
```
Some Text, headers whatever
```{r}
summary(myStructure$value1) #Where myStructure was saved to the .RData file
```
您可以使用 save.image()
命令保存 RData 文件。
希望对您有所帮助!
此类问题有解决方案,已解释 here。
基本上,如果您有包含代码的 .R 文件,则无需在 .Rmd 文件中重复代码,但您可以包含 .R 文件中的代码。为此,代码块应在 .R 文件中命名,然后可以按名称包含在 .Rmd 文件中。
test.R:
## ---- chunk-1 ----
table(cars.by.name$make)
test.Rmd
仅在 .Rmd 文件之上一次:
```{r echo=FALSE, cache= F}
knitr::read_chunk('test.R')
```
对于您包含的每个块(将 chunk-1
替换为 .R 文件中该特定块的标签):
```{r chunk-1}
```
请注意,它应该留空(按原样),在 运行 时间内,您来自 .R 的代码将被带到这里,运行。