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.
When I do it in R markdown:
{r echo=FALSE}
dirname(parent.frame(2)$ofile)
script.dir <- dirname(sys.frame(1)$ofile)
setwd(script.dir)
I get error as below:
Error in dirname(parent.frame(2)$ofile) : a character vector argument expected calls :<Anonymous>...
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.
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}
require("knitr")
opts_knit$set(root.dir = "~/path/to/project")
The ~/
is your HOME
directory on Linux (and maybe Mac). If you're on Windows you'll have to tweak this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With