Cauder
Cauder

Reputation: 2597

Command fails to run in RMarkdown despite working in regular R Script

I have this command and it works fine when I run it from an R script

df <- data.table::rbindlist(lapply(Sys.glob("myfolder/part-*.parquet"), arrow::read_parquet)) %>% 
  as_tibble() %>% 
  mutate(mycol = as.character(mycol))

But, when I run the same command from an RMarkdown paragraph, I get this error

Error: Problem with `mutate()` input `mycol`.
x object 'mycol' not found
ℹ Input `mycol` is `as.character(mycol)`.

And then when I run the command without the last line, then I get an empty data frame.

I'm on the most up to date r studio with the most up to date R on a Mac.

Upvotes: 0

Views: 195

Answers (1)

Waldi
Waldi

Reputation: 41220

The default search path for a Markdown document is the folder where the document is located.
This might be different from the working directory used by the RStudio session.
Verify that the .Rmd file is located in the getwd() folder.

To avoid path problems, a very effective solution under RStudio is :

  1. to work with Projects instead of standalone files
  2. to use the here package which allows you to reference files according to the project root, see also Ode to the here package

Upvotes: 3

Related Questions