Skip to content

RMarkdown: Working directory differs between "Run chunk" and "Knit Rmd" #758

@roaldarbol

Description

@roaldarbol

Describe the bug
Working with R Markdown, I read data and sourced scripts from other folders. I work from a root folder, which contains the sub-folders /scripts and /data, and I need to specify relative paths to import my data and source other R scripts. The problem seems to be that the working directory differs between Run chunk and Knit Rmd. Running chunks seem to assume my working directory is /root, whereas Knit uses /scripts (when the script I'm using is located in that folder). I have a feeling it might be myself having missed a setting somewhere, regardless any help would be appreciated. FYI I'm quite new to VS Code, but have already gotten a good working setup with radian, httpgd, etc.

To Reproduce
Steps to reproduce the behavior:

  1. Set up folder structure:
Root folder
| -- scripts
|     + -- `Test.Rmd`
|     + -- `helper_functions.R`
| -- data
  1. Create a test Test.Rmd script and save in /scripts:
---
title: "Test"
author: "John Doe"
date: "2/8/2021"
output: html_document
---

```{r setup}
source("scripts/helper_functions.r")

(Can't close the R chunk here, but insert ticks yourself)

  1. Create helper_functions.Rin /scripts, e.g. containing:
sq <- function(x){
  return(x^2)
}
  1. Open Test.Rmd
  2. Run chunk - works fine, our function sq shows up in the R Workspace.
  3. Knit document
  4. See error:
Quitting from lines 9-10 (Test.rmd) 
Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
  1. Remove scripts/ from Test.Rmd:
source("helper_functions.r")
  1. Document knits well, but now Run chunk doesn't work

Do you want to fix by self? (We hope your help!)

If easy, yes.

setting.json

// R path for Mac OS X
"r.rterm.mac": "/Users/username/opt/anaconda3/bin/radian",

// Use bracketed paste mode
"r.bracketedPaste": true,

Expected behavior
Running code chunks and knitting should exhibit the same behaviour - otherwise one will have to change the paths every time before knitting.

Environment (please complete the following information):

  • OS: macOS Big Sur
  • VSCode Version: 1.60.0-insider
  • R Version: 4.1.0
  • vscode-R version: 2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions