Traceplot works in RStudio but not by RScript

I got this tutorial running. http://data.princeton.edu/pop510/hospStan.html

I can run thru perfectly inside RStudio and get a plot. But when I ran the same script using RScript hospital.r, no plots comes out. Is traceplot only supposed to work inside RStudio?

You have to call it with Rscript --default-packages=Rcpp because reasons.

I added --default-packages=Rcpp, but still no plot.

Was there an error message? I am not sure what ggplot2 does in non-interactive mode anyway.

make sure that if you do

pl <- ggplot(...)

in an Rscript script, you also do

print(pl)

otherwise the plot will not output. Of course you still have to choose the device, etc… as usual for non-interactive plotting.

Are you looking for a Rplots.pdf file? That’s where plots that would be drawn in the plotting pane are saved.

This is kind of a trap because it replaces all the default packages, so stats and graphics and others won’t be loaded.

I would add

library(methods)
library(Rcpp)

to the top of the script to ensure those get loaded. methods is usually the culprit when Rscript doesn’t work right.

Wonderful. Yes, Rplots.pdf is there. I didn’t know that’s how plots are done. Thank you.