I have a bunch of different graphs that I am trying to make, and I want to have an individual R file for each graph in addition to an R file that source
s all of these R files.
This makes it easier to find and edit things since there is only code for one graph in each R file, but I still want to be able to make all of the graphs at once.
I thought I would be able to do this by having an R that just source
s all of the individual R files that make the graphs.
However, executing the the test2.R
script leads to a blank PDF file being created in ~/Documents/
, whereas uncommenting the first two lines of test.R
and just running that R script by itself leads to the expected PDF file in ~/Documents/
.
Why is this the case? Is there a way to fix it?
test.R
(saved as ~/Desktop/test.R
)
#library(ggplot2)
#library(Cairo)
CarPlot <- ggplot() +
stat_summary(data = mtcars,
aes(x = factor(gear),
y = mpg,
fill = factor(gear)
),
fun.y = "mean",
geom = "bar"
)
setwd("~/Documents/")
CairoPDF(file = "CarPlot.pdf")
CarPlot
dev.off()
test2.R
(saved as ~/Desktop/test2.R
)
library(ggplot2)
library(Cairo)
source(file = "~/Desktop/test.R")