使用子目录使用Knitr [英] Working with knitr using subdirectories
问题描述
我有一个项目文件夹,这是我的工作目录.我们称之为 project . project 文件夹下有4个子目录:code
,data
,figures
和documents
.
I have a project folder, which is my working directory. Let's call it project. Under the project folder are 4 subdirectories: code
, data
, figures
, and documents
.
我想将我的.Rmd
文件放在code
子目录中.我希望这些图进入figures
子目录.我希望.html
,.md
和.docx
文件进入documents
子目录.我想从data
子目录中读取我的数据.这种组织结构可能吗?我似乎无法使其正常工作.
I would like to put my .Rmd
file in the code
subdirectory. I would like the figures to go into the figures
subdirectory. I would like the .html
, .md
, and .docx
file(s) to go into the documents
subdirectory. I would like to read in my data from the data
subdirectory. Is this organizational structure possible? I can't seem to get it to work.
我从这里开始将工作目录设置为project
,因为我知道knitr会查找具有.Rmd
文件的文件夹,如果未指定该目录,则将其视为工作目录.
I start with this to set the working directory to project
because I understand that knitr looks to the folder with the .Rmd
file and treats it as the working directory if this is not specified.
```{r setglobal, cache = FALSE, include = TRUE}
library(knitr)
opts_knit$set(root.dir = "..")
```
然后我尝试设置相对于工作目录的图形路径.
Then I try setting the figure path relative to the working directory.
```{r setchunk, cache=FALSE, include=TRUE}
opts_chunk$set(fig.path = "./figures/")
getwd()
```
工作目录正确报告为project
.可以从data
子目录中正确读取数据.正确渲染了所有输出,并运行了所有代码.
The working directory is correctly reported as project
. The data is correctly read from the data
subdirectory. All output is rendered properly and all code runs.
```{r readdata}
crctx <- readRDS("./data/crctx.rds")
getwd()
*run lots of analyses here*
```
但是这些数字最终出现在project/code/figures
目录而不是project/figures
.
But the figures end up in the project/code/figures
directory instead of project/figures
.
我也尝试过设置base.dir
,但这似乎并没有改变任何东西.我添加了参数
I have also tried setting base.dir
but that doesn't seem to change anything. I added the parameter
base.dir = "./figures"
到opts_knit$set
列表.我什至尝试对完整路径进行硬编码.但是似乎没有什么可以改变这一点.
to the opts_knit$set
list. I even tried hard-coding the full path. But nothing seems to be able to change this.
我正在使用R 3.10和RStudio 0.98.953.这是Mac 0SX 10.9.4.
I am using R 3.10 and RStudio 0.98.953. This is Mac 0SX 10.9.4.
我错过了什么吗?有没有办法将输出文件放在自己的目录中?
Am I missing something? And is there a way to put the output files in their own directory?
之所以重要,是因为我希望我们的公司都使用相同的目录结构,这将使我们能够更好地组织项目.
The reason this is important is that I would like our company to all use the same directory structure, and this will allow us to organize our projects better.
在此先感谢您的帮助.
更新:
我意识到opts_chunk$set(fig.path = "./figures/")
是不正确的.我假设使用opts_knit$set(root.dir = normalizePath("../"))
将根目录设置为项目文件夹将对knitr进行全局更改,因为这是应该这样做的.而且它确实适用于数据子目录,现在可以使用"./data"
访问该子目录,从而允许代码运行.但是,全局设置不适用于图形输出.因此,正确的规范是opts_chunk$set(fig.path = "../figures/")
-使用../
而不是./
.我认为Richie Cotton修复了此问题,一段时间以来我还不太清楚.
I realize that opts_chunk$set(fig.path = "./figures/")
is incorrect. I assumed that setting the root directory to the project folder using opts_knit$set(root.dir = normalizePath("../"))
would make a global change to knitr, since that is what it is supposed to do. And it does work for the data subdirectory, which can now be accessed with "./data"
which allows the code to run. However, the global setting doesn't apply to the figure output. Therefore, the correct specification is opts_chunk$set(fig.path = "../figures/")
-- using ../
instead of ./
. I think Richie Cotton fixed this, and I didn't quite see it for a while.
我还使用OSX中的Terminal来创建从project/documents
到project/code/figure
的符号链接,该链接默认由knitr创建.这样,knitr会在右侧的子子目录中查找,但是所有内容最终都在projects/documents
中.那真的很好.我无法让R使用file.symlink正确创建符号链接.但是它在Terminal中可以正常工作.走吧.
I also used Terminal in OSX to create a symbolic link from project/documents
to project/code/figure
which is created by default by knitr. With this, knitr looks to the right sub-subdirectory, but everything ends up in projects/documents
. That worked really well. I can't get R do create the symbolic link properly using file.symlink. But it works fine in Terminal. Go figure.
更新2 :
我也输出了文件.您必须直接使用knit命令.文本在下面.
I got the output files to work as well. You have to use the knit command directly. The text is below.
用于在代码/分析中使用.Rmd编织降价文档并在输出/报告中进行输出的编织代码:
knit("./code/knitr_file.Rmd", "./documents/knitr_output.md)
Knitr code to knit a markdown document with .Rmd in code/analysis and output in output/reports:
knit("./code/knitr_file.Rmd", "./documents/knitr_output.md")
Pandoc代码将.md文件转换为.docx-使用与.md文件相同的文件夹
pandoc("./documents/knitr_output.md", format = "docx)
Pandoc code to convert .md file to .docx -- uses the same folder as the .md file
pandoc("./documents/knitr_output.md", format = "docx")
pandoc('knitr_output.md', format='html') # HTML
pandoc('knitr_output.md', format='latex') # LaTeX/PDF
pandoc('knitr_output.md', format='docx') # MS Word
pandoc('knitr_output.md', format='odt') # OpenDocument
pandoc('knitr_output.md', format='html') # HTML
pandoc('knitr_output.md', format='latex') # LaTeX/PDF
pandoc('knitr_output.md', format='docx') # MS Word
pandoc('knitr_output.md', format='odt') # OpenDocument
推荐答案
尝试一下.假定您在工作目录project
中列出了4个文件夹.它还假定您在data
中有一个名为myData.csv
的.csv
文件.
Try this. It assumes you have the 4 folders you listed inside the working directory project
. It also assumes you have a .csv
file called myData.csv
in data
.
编织文件时,图将保存在figures
中.最后,代码在code
中查找html
文件,并将其移至documents
.可能有更好的方法.
When you knit the file, the plot will be saved in figures
. At the end, the code looks for html
files in code
and moves them to documents
. There's probably a better way to do this.
```{r setup}
library(knitr)
opts_knit$set(root.dir=normalizePath('../'))
opts_chunk$set(fig.path = "../figures/", dev='pdf') # corrected path and added dev
```
```{r import}
dat <- read.csv("data/myData.csv")
```
```{r plot}
# pdf(file="figures/test.pdf") # I do this in setup instead
plot(dat)
# dev.off()
```
```{r move}
files <- list.files("code/")
index <- grep("html", files)
file.rename(file.path("code", files[index]),
file.path("documents", files[index]))
```
这篇关于使用子目录使用Knitr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!