Setting Working Directory using knitr

Hi I'm using the following code to set my working directory, however when I go to my files it doesn't appear to have set it as my working directory and I was hoping someone might know what I've done wrong?

opts_knit$set(root.dir ="C:/Users/ruthw/Documents/Flexanalysis/Aug 2020/20200820_HEPG2_Ctrl_48hr_LIPIDS_TA30_RP_220+")
knitr::opts_chunk$set(echo = T, warning = F, message = F, cache = T, error = F, cache.lazy = F)

There might be some confusion as to what you mean by "go to my files".

The command opts_knit$set(root.dir ="C:/Users/ruthw/Documents/Flexanalysis/Aug 2020/20200820_HEPG2_Ctrl_48hr_LIPIDS_TA30_RP_220+")
changes the working directory of all subsequent code chunks when you compile your R Markdown doc
You can confirm this by adding getwd() to a code chunk, compile the document, and check the output from getwd()

"Go to my files" kind of suggest you might be expecting this change to have affected the working directory of the RStudio IDE. Changing oot.dir won't affect the IDE's working directory.

Authoritative documentation on this is is the R Markdown Cookbook's ,

1 Like

Thanks so much, I think that's exactly it! I will add an extra line of code.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.