How do I prevent Rplots.pdf from being generated?
I wonder if you have another command that opens a device before or after the code snippet you've given us. When you're all done run dev.cur()
to see if there was a device left open. If not, it should return the null device.
Here are ways you can recreate getting a Rplots.pdf
or a Rplot001.png
; the layout
and par
commands open a device if one isn't open, and since no filename has been given, it uses the default filename.
options(device="pdf")
layout(1:4)
dev.off()
options(device="png")
par()
dev.off()
Maybe seeing that happen here will give you a clue as to what's happening with your code.
How to stop R from creating empty Rplots.pdf file when using ggsave and Rscript
If you look at the defaults for the width and height arguments in ggsave
, you'll see that they are par("din")[1]
and par("din")[2]
. If you run this in the console, you'll see that it opens a graphics window, if one isn't already open.
This sort of makes sense, since in order to get the device width/height in inches, you need an actual device. I suppose one could argue that par("din")
should return an error if no device is open, in which case Hadley surely would have written ggsave
differently.
Indeed, from ?par
:
If the current device is the null device, par will open a new device
before querying/setting parameters.
Hence, specifying a width/height will prevent the spurious device from opening.
Empty Rplot.pdf file is created every time Rstudio is started
What is causing the issue is the effects
package (version 4.0.0). If there is a .R
file with the line library(effects)
in the project folder and I open the project, or if I open an .Rmd
file containing that line from within Rstudio, the plot is created (to be sure, the issue is not happening when loading the package).
I downgraded to a previous version and that seems to have solved the problem.
Rscript is plotting to PDF
You are running R in a non-interactive way - Rscript is meant for scripts - hence the default plotting device is pdf()
, not x11()
or whatever is your OS's default (windows()
by the looks of it). It is trivial to open an alternative device, however; use x11()
or windows()
. The problem you have in trying to write a script that will display a plot on screen is that, in your example code shown, the script terminates immediately upon drawing the plot, whether displayed on screen or on the pdf()
device. At best you might get it to pause using Sys.sleep()
, e.g.:
x <- 1:10
y <- sin(x)
x11() ## or windows()
plot(x,y)
Sys.sleep(10)
I think you are going about this the wrong way. If you want interactivity when running an R "script", by which I mean a set of R statements that perform some analysis, you'd be better off getting an editor/IDE on your OS that allows you to step through the script a line or chunk of code at a time, plus interact with the running R session. I use Emacs and the ESS extension for this. You might consider Tinn-R or RStudio as alternatives.
Rscript
is meant for running scripting or batch-like jobs that do not need human interaction or intervention.
Set Rplots.pdf generation location
There should be a line in data_analysis.r which says
pdf(file="RPlots.pdf")
You can edit this line to location you want and hard code it.
pdf(file="/mylocation/RPlots.pdf")
How to avoid generating pdf-file per figure in sweave?
You can't avoid generating the figures. RStudio isn't really doing much of the work here; it's just directing other software to do it.
- R generates the figure, and the LaTeX source code to import it.
- LaTeX imports the figure and produces the final .pdf for the whole document.
You can tell R to put the files in a particular place using \SweaveOptions{prefix.string = figs/}
. Put this into your document somewhere
pretty early, and all figures will be put into a directory called "figs" (which must exist for this to work).
For more details about the options in Sweave, see the vignette in the utils
package.
Show plot instead of save a PDF of it
Using either the standalone R console or another programme such as RStudio, the default behaviour is to show the plot rather than save it as a pdf. Try using dev.off()
to reset the graphics device.
Failing that, what happens if you call getOption("device")
?
Run ggsave() in task manager
This helped.
grDevices::dev.set(1)
library(ggplot2)
pdf(NULL)
options(bitmapType = 'cairo', device = 'pdf')
g <- ggplot()+geom_line(data = data.frame(a = 1:10, b = 21:30),
aes(x = a, y = b))
ggsave(tf<-tempfile(fileext = ".png"),
g,
device = 'png')
Related Topics
Ubuntu 16.04 R Installation: Configure: Gdal-Config Not Found or Not Executable
Apply a Function to All Variables Starting with Specific Pattern in R
Outputing N Tables in Shiny, Where N Depends on the Data
How to Bookmark and Restore Dynamically Added Modules
How to Create a Pie Chart with Percentage Labels Using Ggplot2
Inserting a Table Under the Legend in a Ggplot2 and Saving Everything to a File
R: Save All Data.Frames in Workspace to Separate .Rdata Files
Finding Unique Combinations Irrespective of Position
Ordering Factors in Number Order for Ggplot
How to Join Data from 2 Different CSV-Files in R
Exporting Multiple Panels of Plots and Data to *.Png (In the Style Layout() Works Within R)
Ggplot Inserting Space Before Degree Symbol on Axis Label
Understanding Lm and Environment
How to Install R Packages via Proxy [User + Password]