vscode-R icon indicating copy to clipboard operation
vscode-R copied to clipboard

Smart Knit does not respect .RProfile / {renv}

Open beansrowning opened this issue 2 years ago • 21 comments

Describe the bug Smart/background knitting session ignores workspace .RProfile. This is an issue in my case, as I use {renv} and the background process can't locate the library path even when the working directory is changed to "workspace root".

To Reproduce Steps to reproduce the behavior:

  1. Pull up any rmarkdown template
  2. use {renv} to install dependencies
  3. Run "Knit"
  4. See error

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

I can assist, but don't code in TS normally.

Expected behavior Smart knit either launches non-vanilla by default, or has an additional option to respect workspace environment.

Screenshots If applicable, add screenshots to help explain your problem.

image

Environment (please complete the following information):

  • OS: Win 10
  • VSCode Version: 1.61.0-insider
  • R Version: 4.1.0
  • vscode-R version: 2.3.0

beansrowning avatar Sep 28 '21 14:09 beansrowning

Thanks for reporting this! I'll have to double check, but I suspect this is because the cwd isn't set when launching the knit process, should have a fix tonight

ElianHugh avatar Sep 30 '21 01:09 ElianHugh

@beansrowning would you be able to test if this PR solves the renv issue?

ElianHugh avatar Sep 30 '21 12:09 ElianHugh

Yes, I'll try it out today @ElianHugh. Thanks for the quick work on that!

beansrowning avatar Sep 30 '21 14:09 beansrowning

Ha, well now I've bricked my install. I uninstalled the extension, rebuilt your branch and installed that, and now none the the commands work:

image image

I tried to revert by deleting the extension and re-installing 2.3 from VSCode, but it's still broken. Seems like this is a known bug, but is there any guidance?

#106, #6, #72

beansrowning avatar Sep 30 '21 16:09 beansrowning

Argh! It's weird that reinstalling didn't work, it makes me think that it didn't clear out the previous install.

Would you be able to delete any R extension folder that is present under '%USERPROFILE%.vscode\extensions'? It should be something like 'Ikuyadeu.r-2.3'.

Sorry for the trouble!

ElianHugh avatar Oct 01 '21 00:10 ElianHugh

Ah, wiping the directory did the trick--thanks!

Unfortunately, I still can't get the vsix file I built to install correctly (or it appears to, and then results in the command not found errors above).

beansrowning avatar Oct 01 '21 03:10 beansrowning

Unfortunately, I still can't get the vsix file I built to install correctly [...]

Have you tried with the vsix file from here?

ManuelHentschel avatar Oct 01 '21 08:10 ManuelHentschel

Unfortunately, I still can't get the vsix file I built to install correctly [...]

Have you tried with the vsix file from here?

That one did the trick, thanks! Fix confirmed working on my platform 🎉

beansrowning avatar Oct 01 '21 13:10 beansrowning

I update #807 so that the knit process is started from the knit working directory, i.e. either workspace root or document folder so that multi-root workspace is properly supported. I wonder what should be the behavior for the following scenario:

Workspace root contains .Rprofile and renv folder, and the rmd file is located in a subfolder (e.g. doc). If "document folder" is chosen as the knit working directory, should we also respect the .Rprofile and renv setup in the workspace root?

Or should we always start the knit process from the workspace root of the document since renv and .Rprofile is mostly setup in the workspace root?

renkun-ken avatar Oct 07 '21 02:10 renkun-ken

I tried the same in RStudio and its behavior is keeping the working directory of the knit process consistent with the knit working directory, i.e. if "document folder" is chosen, then the knit process is also started from the document folder, as currently implemented in #807.

renkun-ken avatar Oct 07 '21 06:10 renkun-ken

I think I might be running into the same issue with {renv}, and believe that it may have to do with the knit wd implemented in #807. Similarly, VSCode-R cannot find the {renv} library using .libPaths() using the radian terminal when trying to knit (smart or otherwise), and therefore cannot knit files that contain packages in the {renv} library. The Rmd file is in a subdirectory or the workspace root which contains the renv/ folder and .Rprofile e.g., project-root/analysis/glm/test.Rmd. I believe that although RStudio keeps the wd of the knit process consistent with the output directory, it still finds the {renv} library, so I wonder if there's something else going on in RStudio? The code works with inline evaluation perfectly (i.e., when not knitting).

Another data point is that I am using a {targets} pipeline, and this runs perfectly in VSCode-R, as I believe this sets the wd to the project root during the pipeline run (ropensci/targets#230).

Thanks for the work putting this together - it's been great moving towards using VSCode for R!

OS: macOS Big Sur v11.6 VSCode Version: 1.62.0-insider R Version: 4.1.1 vscode-R version: 2.3.1

arnold-c avatar Oct 21 '21 15:10 arnold-c

Workspace root contains .Rprofile and renv folder, and the rmd file is located in a subfolder (e.g. doc). If "document folder" is chosen as the knit working directory, should we also respect the .Rprofile and renv setup in the workspace root?

I prefer this solution: .Rprofile and renv setup in the workspace root should be respected, even if "document folder" is chosen as the knit working directory.

When I write an Rmd document, I usually assume that the document's working directory (using a relative path to load the workspace's dataset) is where the document is located, and assume that the R environment is set up from the root directory.

altairwei avatar Nov 16 '21 07:11 altairwei

@arnold-c have you found a workaround for this? I have the same issue - a project setup with {targets}, packages manged by {rend}, and an RMD in a subfolder of the workspace (/doc). Knitting as part of targets::tar_make() is fine, but if I want to just quickly render the RMD it cannot find packages unless I also install them in my main library. Did you find a way around this?

tarensanders avatar Mar 25 '22 09:03 tarensanders

@tarensanders no, I'm afraid not really for the smart knit etc. I've found that updating to the latest {renv} GitHub/developer build has helped a lot with packages being located etc, and I just use rmarkdown::render(here::here("dir", "file")) in the command line if I want to knit outside of the {targets} pipeline. This obviously requires listing packages in the .Rmd file, but to point the file to _targets/ requires using the below chunk (see https://github.com/ropensci/targets/issues/230).

withr:with_dir(
    here::here(), {
    tar_load(target)
    }
)

arnold-c avatar Mar 25 '22 14:03 arnold-c

Thanks for the updates re: renv. I'll try to have a look at this over the coming days

ElianHugh avatar Mar 26 '22 06:03 ElianHugh

Hi @arnold-c and @tarensanders, sorry to bug both of you! As I haven't used renv before, I was wondering if I could get some more information on how your folders are structured (particularly where the .Rprofiles are located).

If I want to just quickly render the RMD it cannot find packages unless I also install them in my main library.

Does this mean that the renv/library folder has the appropriates packages for knitting, but it isn't being taken into account when smart knitting? If so, I think the expected behaviour would be that prioritise .Rprofile in workspace root. Would this appropriately fix the issue?

ElianHugh avatar Mar 30 '22 03:03 ElianHugh

Happy to help if I can, @ElianHugh.

As I haven't used renv before, I was wondering if I could get some more information on how your folders are structured (particularly where the .Rprofiles are located).

A sample folder structure with renv (and targets) would look like:

  • R/
    • some_funcs.R
  • _targets/
  • docs/
    • report.Rmd
  • renv/
    • library/
    • activate.R
  • .Rprofile
  • renv.lock

This .Rprofile contains source("renv/activate.R") and then renv.activate.R contains code to change which library is being used (you probably already know this).

Does this mean that the renv/library folder has the appropriates packages for knitting, but it isn't being taken into account when smart knitting?

That's correct.

If so, I think the expected behaviour would be that prioritise .Rprofile in workspace root. Would this appropriately fix the issue?

I think that would solve the issue.

tarensanders avatar Mar 30 '22 03:03 tarensanders

Happy to help if I can, @ElianHugh.

If so, I think the expected behaviour would be that prioritise .Rprofile in workspace root. Would this appropriately fix the issue?

I think that would solve the issue.

~~Great, thank you for your help! I think what is happening is that the knit wd setting is overwriting any changes to the wd that may occur from the renv .Rprofile.~~

~~I wonder if it would make sense to have a third working directory setting that is something like "unchanged" or "defer to .Rprofile"~~

ElianHugh avatar Mar 30 '22 06:03 ElianHugh

OK, so a very basic change appears to fix renv with regard to knitting:

local_rprof <- ".Rprofile" %in% list.files()

# set the knitr chunk eval directory
# mainly affects source calls
if (nzchar(knit_dir) && !local_rprof) {
    knitr::opts_knit[["set"]](root.dir = knit_dir)
}

Not sure about checks/limitations on this however. E.g., perhaps only take into account a local .Rprofile if there is a renv folder

ElianHugh avatar Apr 18 '22 03:04 ElianHugh

What if we always start the knit process from workspace root and call setwd() afterwards before knitting to the user chosen knit directory?

renkun-ken avatar Apr 18 '22 07:04 renkun-ken

I see what you mean, although the actual wd isn't altered, just the knitr option.

ElianHugh avatar Apr 26 '22 02:04 ElianHugh

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] avatar Apr 27 '23 01:04 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar May 12 '23 01:05 github-actions[bot]