1

I am learning R Notebook currently and I am having a path problem. I have made an R project and hosted all of my scripts and files in a folder containing the R project. I am trying to run:

Next, install libraries:

library(tidyverse)
library(CHNOSZ)
library(janitor)

Then import and merge the files using:

d <-list.files(path="./merge", pattern="*.csv", full.names = TRUE) %>% 
  map_df(~read_csv(., col_types = cols(.default = "c")))

d

and the result is a tibble with 0 rows. Also,

write.csv(d,file="data_generated/FinalmergeAllCompounds_comb.csv")

throws the error Error in file(file, ifelse(append, "a", "w")) : cannot open the connection

When I run this as a normal script within the same project in Rstudio the script works just fine. I merge the .csv files without an issue and I can export it back out into my project directory with no problems.

It seems to be a conflict between RNotebook and the file path. How do I fix this?

MBell
  • 57
  • 1
  • 8
  • The working directory for the R notebook is the directory where the file is hosted by default. You can change this in the knitr options. See the [Rmarkdown guide](https://bookdown.org/yihui/rmarkdown/notebook.html). – csgroen Oct 07 '19 at 15:25
  • I have tried building an R notebook of a different script within the same same project directory, and it worked without these problems. Could it be a problem with the path="merge" (I have tried both path="merge" and path="./merge"? – MBell Oct 07 '19 at 15:38
  • Have you tried setting the working directory in the knitr options to see if that works? – csgroen Oct 07 '19 at 15:42

1 Answers1

2

The answer to the question was to add:


knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())

to the top of the notebook script based on csgroen's comment and this post Setting work directory in knitr using opts_chunk$set(root.dir = ...) doesn't work

MBell
  • 57
  • 1
  • 8