0

I'm having issues printing or exporting a plot in Linux using the source() function. The linux command I use is R CMD BATCH file.R. I have a file called load, which has the relevant packages (I have to run the code from a different file using the source function because I only have administrative approval to install packages from my home directory, despite the files and output needing to be in a shared directory). From the file load in my home directory, I run:

install.packages("ggplot2")
install.packages("lattice")

library(ggplot2) 
library(lattice)

source("shareddirectory/graphs.R", echo = TRUE, print.eval = TRUE)

As mentioned, the file "graphs.R" is located in a shared directory, where I'd like the output. I've tried a few methods in this file to print or export the graph.

Method 1:

plot <- ggplot(df, aes(x, y))
print(plot)

or

plot <- print(ggplot(df, aes(x, y)))

Method 2

pdf("plot.pdf")
plot <- ggplot(df, aes(x, y))
dev.off()

Method one doesn't print anything. For method two, I've tried nearly every function (e.g., pdf(), jpeg(), png(), postscript(), etc.). The output is a jumbled mess of words and symbols. Does anyone know why that is, or how I can print the graph?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
llsabang
  • 143
  • 1
  • 10
  • What exactly does the output look like? `ggplot(df, aes(x, y))` isn't a complete plot, you need a `geom_*()` in there to actually draw something. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Does it work interactively if you don't use `R CMD BATCH`? – MrFlick Feb 03 '20 at 18:48
  • Yes, you're correct! I should've added geom_point(). I figured out though that I need to right click the file (rather than just opening) in order for it to display correctly. Must just be a quirk of the server. – llsabang Feb 03 '20 at 20:12

0 Answers0