nicholas
nicholas

Reputation: 983

Rmarkdown does not follow same paths as R script and console commands

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:

  1. Create new project test.Rproj
  2. Create a subdirectory called scripts
  3. 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")
  1. Quit R, and reopen test.Rproj.
  2. 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:

  1. I can read the test.csv file via Rmarkdown if it is in the scripts subdirectory, rather than the data subdirectory.
  2. 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 run list.files() from Rmarkdown, I get a list of files in the scripts subdirectory.

How can I fix this problem?

Session info:

Upvotes: 2

Views: 1473

Answers (2)

MrFlick
MrFlick

Reputation: 206167

When you knit a document in RStudio, by default the working directory is set to the current directory of the Rmd document (so that would be the "scripts" folder). Since the "scripts" folder does not contain the "data" directory, you get that error. You can change the default to use the project root directory if you prefer. That's an option in the RStudio Global Options menu.

enter image description here

See See https://bookdown.org/yihui/rmarkdown-cookbook/working-directory.html for more info

Upvotes: 2

Daniel James
Daniel James

Reputation: 1433

Try something that looks like this as I am not sure of the nature of your `R Markdown.

test <- readRDS(here::here("data/test_data.rds"))

The bottom line is to use the here function from the here package.

Upvotes: 3

Related Questions