Using RStudio from Windows command prompt

I have an automated Windows bat file that runs everyday, and as a final step I want to produce a plotly figure and export it using htmlWidgets.

Here is a simple reprex of a an R file, plotlyExample.R say:

library(plotly)
dat <- data.frame(x = 1:100, y = cumsum(rnorm(100)))
p <- plot_ly() %>%
  add_trace(type="scatter", mode="lines+markers", data = dat, 
            x = ~x, y = ~y)
htmlwidgets::saveWidget(p, file = "plotlyTest.html", selfcontained = TRUE)

If I open this file in RStudio (on Windows) and run the code, it works fine and I get a html file that works fine. Whether I set selfcontained to TRUE or FALSE makes no difference.

However, if I run the file in R (not RStudio) or using "C:\Program Files\R\R-3.6.1\bin\Rscript.exe" in my bat file, I get:

    Error in htmlwidgets::saveWidget(p, file = "plotlyTest.html", : Saving a widget with selfcontained = TRUE requires pandoc. For details see:
    https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md

If I set selfcontained = FALSE I get no errors, but the html file is just blank when I try to view it.

I am at this point unable to install Pandoc myself (company policy), and so essentially I would like to leverage the installation that RStudio ships with (or that is what I'm assuming is going on here), but from my bat file. Is this possible? Or any other ways around the issue?

I'd start by checking the output of

rmarkdown::find_pandoc()

first in RStudio then via R or Rscript

Thanks nirgrahamuk, that was exactly the type of direction I needed. I then realized that the issue is that Pandoc (as shipped with RStudio) is not included in PATH, so adding the following to the top of my script:

Sys.setenv(PATH = paste(c(Sys.getenv("PATH"), "C:\\Program Files\\RStudio\\bin\\pandoc;"), collapse = ""))

solved it all for me.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.