How do I configure R so programs developed in RStudio run successfully in R?

This is specific to the Pandoc issue you have on Windows. Some might think that this is a bit of a hack. I assume that you have RStudio + R installed on all PC, including the ones you run your batch scripts on.

RStudio comes bundled with Pandoc and adds a few environment variables that are not in a pure R session. You can use Sys.getenv() to see which variables are added in a RStudio session and compare this to a pure R session.

If you save your specific RSTUDIO_PANDOC path as MY_RSTUDIO_PANDOC in a .Renviron file, then you can write you own function to render the Rmd file. In your render function you set the Sys.setenv("RSTUDIO_PANDOC" = Sys.getenv("MY_RSTUDIO_PANDOC")) and then you'll will use the Pandoc bundled with RStudio when the rmarkdown::render function is called, and you won't have a need to install pandoc separately. The env var should of course be added by all who are running the function, including the server/PC running the batch jobs.

1 Like

Thanks. So, to make R aware of RStudio's resources, do I just need to set some variables in the .Renviron file that R uses? If that's true, and someone can point me to the environment variable names, maybe I'll have a systematic solution.

Expanding from your answer, I opened RStudio and ran

sink(file = "~/RStudioEnv.txt")
Sys.getenv()
sink()

then opened R and ran

sink(file = "~/REnv.txt")
Sys.getenv()
sink()

When I compared "~/RStudioEnv.txt" to "~/REnv.txt", I found that RStudio had 13 environmental variables that were not in R's environment. Four seemed to point to resources that my R session might need:

  1. RMARKDOWN_MATHJAX_PATH
  2. RSTUDIO_MSYS_SSH
  3. RSTUDIO_PANDOC
  4. RSTUDIO_WINUTILS

(the other nine were: GFORTRAN_STDERR_UNIT, GFORTRAN_STDOUT_UNIT, RS_LOCAL_PEER, RS_RPOSTBACK_PATH, RS_SHARED_SECRET, RSTUDIO, RSTUDIO_SESSION_PORT, RSTUDIO_USER_IDENTITY, SESSIONNAME).
Are there four analogous environmental variables that my R session will look for, which I could created using the folder paths the RStudio variables use? Or maybe I can put something in .Renviron that will add those four folder paths to my PATH. If there is a "right" or recommended approach to this kind of thing in R, please let me know.

I would experiment a bit, and be careful not to replace all RStudio env vars every time you load RStudio and only set the vars needed to render a file without problems in batch.

I have created my own pandoc environment variable and specifically set the RSTUDIO_PANDOC path in the function to render my Rmd file. I use the .Renviron file, but the .Rprofile is also an option.