如何在 R markdown 中将当前文件位置设置为工作目录? [英] How to set current file location as work directory in R markdown?

查看:131
本文介绍了如何在 R markdown 中将当前文件位置设置为工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 mydata.RDATA 要在 R 中使用,然后我需要 load(),这意味着我需要先 setwd() 当前目录.我已经知道 如何在 R 中实现.

I have mydata.RDATA to be used in R, then I need to load(), which means I need to setwd() curent directory first. I already know how to do it in R.

当我在 R markdown 中这样做时:

When I do it in R markdown:

{r echo=FALSE}目录名(parent.frame(2)$ofile)script.dir <- 目录名(sys.frame(1)$ofile)setwd(script.dir)

我收到如下错误:

dirname(parent.frame(2)$ofile) 中的错误:字符向量参数预期调用:<匿名>...

推荐答案

如果你的 .Rmd 文件在一个子文件夹你需要指定 knitr 的根目录,甚至 如果您已使用 setwd() 或什至是 RSudio 项目指定了工作目录.

If your .Rmd file is in a subfolder you need to specify the root directory for knitr, even if you've specified a working directory with setwd() or even an RSudio project.

幸运的是,这就像将以下块添加到 .Rmd 文件的开头一样简单,就在 YAML 之后:

Fortunately this is as easy as adding the following chunk to the start of your .Rmd file, right after the YAML:

{r "setup", include=FALSE}要求(编织者")opts_knit$set(root.dir = "~/path/to/project")

~/ 是您在 Linux(也可能是 Mac)上的 HOME 目录.如果您使用的是 Windows,则必须对此进行调整.

The ~/ is your HOME directory on Linux (and maybe Mac). If you're on Windows you'll have to tweak this.

这篇关于如何在 R markdown 中将当前文件位置设置为工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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