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?