正确的 R Markdown 代码组织 [英] Proper R Markdown Code Organization

查看:38
本文介绍了正确的 R Markdown 代码组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读 R Markdown (此处此处此处)并使用它来创建可靠的报告.我想尝试使用我正在运行的小代码来进行一些临时分析并将它们转换为更具可扩展性的数据报告.

I have been reading about R Markdown (here, here, and here) and using it to create solid reports. I would like to try to use what little code I am running to do some ad hoc analyses and turn them into more scalable data reports.

我的问题相当广泛:有没有一种方法可以围绕 R Markdown 项目组织代码?比如说,有一个脚本可以生成所有的数据结构吗?

My question is rather broad: Is there a proper way to organize your code around an R Markdown project? Say, have one script that generates all of the data structures?

例如:假设我有 cars 数据集,并且我已经引入了制造商的商业数据.如果我想将制造商附加到当前的 cars 数据集,然后使用操纵的数据集 cars.by.name 为每个公司生成一个单独的汇总表,该怎么办?以及使用 cars.import?

For example: Let's say that I have the cars data set and I have brought in commercial data on the manufacturer. What if I wanted to attach the manufacturer to the current cars data set, and then produce a separate summary table for each company using a manipulated data set cars.by.name as well as plot a certain sample using cars.import?

现在我打开了两个文件.一个是具有所有数据操作的 R 脚本文件:子集和重新分类值.另一个是 R Markdown 文件,我正在其中构建文本以伴随各种感兴趣的表格和图.当我从 R 脚本文件中调用一个对象时——比如:

Right now I have two files open. One is an R Script file that has all of the data manipulation: subsetting and re-categorizing values. And the other is the R Markdown file where I am building out text to accompany the various tables and plots of interest. When I call an object from the R Script file--like:

```{r}
table(cars.by.name$make)
```

我收到一个错误提示 Error in summary(cars.by.name$make) : object 'cars.by.name' not found

编辑 2: 我发现这个旧线程很有帮助.链接

EDIT 2: I found this older thread to be helpful. 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.

推荐答案

经常,我有很多报告需要运行相同代码,但参数略有不同.分别调用我所有的统计"函数,生成结果然后只是引用是我通常做的.实现方法如下:

Often times, I have many reports that need to run the same code with slightly different parameters. Calling all my "stats" functions separately, generating the results and then just referencing is what I typically do. The way to do this is as follows:

---
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 文件.

You can save an RData file by using the save.image() command.

希望有帮助!

这篇关于正确的 R Markdown 代码组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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