In Rmarkdown, I cannot read files that I can read from the console and in an R script, because Rmarkdown is not following the same paths as my R scripts and console commands.
Here is a minimum reproducible example:
- Create new project test.Rproj
- Create a subdirectory called scripts
- Run the following R Script scripts/test.R:
test <- as.data.frame(c(1, 2, 3))
dir.create("data")
write.csv(test, "data/test.csv")
rm(test)
test <- read.csv("data/test.csv")
- Quit R, and reopen test.Rproj.
- Knit the following Rmarkdown document (scripts/test.Rmd):
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
_```
```{r read-data}
test <- read.csv("data/test.csv")
_```
Yields the following error: Quitting from lines 12-13 (test.Rmd) Error in file(file, "rt") : cannot open the connection Calls: ... withVisible -> eval -> eval -> read.csv -> read.table -> file Execution halted
(Note, the backticks in the .Rmd file are properly specified -- I added underscores above so that the backticks appeared in the code block.)
Two seemingly related issues:
- I can read the test.csv file via Rmarkdown if it is in the scripts subdirectory, rather than the data subdirectory.
- When I run
list.files()
from the console or script, I receive of list of files in the top-level directory (i.e., where test.Rproj is located), including the data and scripts subdirectories. When I runlist.files()
from Rmarkdown, I get a list of files in the scripts subdirectory.
How can I fix this problem?
Session info:
- R version 4.1.0 (2021-05-18)
- RStudio version 1.4.1717
- Platform: x86_64-apple-darwin17.0 (64-bit)
- Running under: macOS Big Sur 11.5.1