2

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:

  • 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
nicholas
  • 903
  • 2
  • 12
  • Can you do a different `R` operation without you importing data through the `RMarkdown to see if it will work? Like you writing a vector into `R` in the same `RMarkdown` Project. – Daniel James Aug 07 '21 at 23:55
  • Yes -- I can do anything in Rmarkdown except read from / write to a subdirectory other than *scripts*. – nicholas Aug 08 '21 at 00:02
  • Can you try to read in a `.csv` file? Use `read.csv()` function. – Daniel James Aug 08 '21 at 00:15
  • What exactly do you mean by "run the same line in Rmarkdown". You mean you have an Rmarkdown document open in RStudio and you press the "knit" button? Or something else? It's very odd that you are getting a shell error. You you please provide a minimal [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with a small Rmarkdown file that shows exactly what you are trying to do. It seems like you might have set up the code chunk wrong but it's hard to tell without seeing any actual code. – MrFlick Aug 08 '21 at 00:18
  • @MrFlick, I edited the question to create a reproducible example. – nicholas Aug 08 '21 at 00:59
  • @DanielJames, I have the same issue with csv (which is the file type in the edited example). – nicholas Aug 08 '21 at 00:59
  • Then try my answer – Daniel James Aug 08 '21 at 01:01
  • Try specifying the full path, it may be that the working directory that the .Rmd file calls is not the same as the R session. Does that help? – Justin Cocco Aug 08 '21 at 01:02

2 Answers2

3

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.

Daniel James
  • 1,381
  • 1
  • 10
  • 28
2

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    I don't recommend doing this as when computers are switched and these options are not explicitly shared, code won't work. Instead see answer on `here()` by Daniel James. – its.me.adam Feb 03 '23 at 20:22