R Imagemagick "skips" annotations and turns monochrome - r

I'm working on a composite graph in R, for which I export a ggplot and then load it into magick for further annotations and compositing another graph. Since alignment is a lot of trial and error, I will keep a version with all "final" annotations and use a second image to play around with.
Usually, this works pretty well, but from time to time either or both of two errors will happen:
The image turns into black/white (not even grayscale, just hard black/white)
Some of the annotations are being "skipped", meaning they just don't show up in the preview
I have tested the second error, and the sequence of commands matters. All annotations until a certain point will be applied and all thereafter won't.
The only thing that reliably "fixes" the error is restarting the PC. Restarting the R session, clearing the cache, restarting Rstudio remain without effect. Once this error has occurred, it will keep persisting (rerunning the code will produce the exact same result) until a system reboot.
A simplified version of what my code does:
graph1 <- image_read(path1)
graph2 <- image_read(path2)
annotated_graph <- graph1 %>% # creating a saved version so I don't have to run all of the code every time
image_border(color='white', geometry = '2000x1000') %>%
image_annotate("text1", size = 100, color = "black", location = "+1000+4000") %>%
image_annotate("text2", size = 100, color = "black", location = "+2000+4000")
annotated_graph %>% # opens current "experiment" state in viewer
image_annotate("text3", size = 100, color = "black", location = "+3000+4000") %>%
image_composite(image_scale(graph2, "1000"), offset = "+1000+1000")
To test if the viewer itself might be the problem, I have tested opening it in a browser (to no avail). I also tried opening it in the plot window with the code below, but it just shows the same error (in lower resolution):
tiff_file <- tempfile()
image_write(annotated_graph, path = tiff_file, format = 'tiff')
r <- raster::brick(tiff_file)
raster::plotRGB(r)
I thought this might be a memory issue, but according to task manager there's several gigabytes of free RAM.
I'd be really happy to resolve the issue altogether, but finding a way to restart whatever component needs to be restarted without rebooting the entire system would already be splendid!
My specs: Win10 on AMD64, R 4.0.2, magick 2.4.0

Related

R wordcloud2 showing blank page - persistent problem

I have the exact same problem as previously asked May 3 '18.
wordcloud2 performs fine until I try to add a mask. The code executes fine in RMarkdown with no error but also produces only a blank page.
Previous suggestions were to re-install wordcloud2 which I did. However, this did not resolve of the problem.
# works fine gives word cloud with red palette
wordcloud2(hmtTable,
size = 0.7,
color = rep_len(redPalette, nrow(hmtTable)))
# produces blank page, heart.png is in subdirectory figs
wordcloud2(hmtTable,
size = 1.5,
figPath = "figs/heart.png",
color = rep_len(redPalette, nrow(hmtTable)))
This code should have produced a word cloud in shape of mask shape. It did not, and no error message was given.

R png()/pdf() doesn't work when running script but works if executing step by step

I'm creating a script to cluster my data in a server. I need to save the text output and the images as well. The text output works just fine but when I try to use the png() + plot() + dev.off() thing to save the plots, no image is created.
[ADDED FOR CLARIFICATION]
What I need to do is to SAVE the plot (i.e. generate an image file) in running mode. If I run the code step by step, the file is created.
I already tried to change the image format to PDF and JPG using the corresponding functions but I'm still getting no images as output when running the code as script. When stepping, it works great.
Since it takes a little while for R to render the image when I'm running step by step, I tried to add Sys.sleep(2) in between commands (code below) but nothing changed.
I think the problem might be related to the package that I'm using and the type of object it generates (library(NMF)). I looked at the documentation to see if there was something about the way the plot() function works with the type of object that the clustering algorithm generates but the text is vague:
"The result (of estim.r <- nmf(esGolub, 2:6, nrun=10, seed=123456) for example) is a S3 object of class NMF.rank, that contains a data.frame with the quality measures in column, and the values of r in row. It also contains a list of the consensus matrix for
each value of r".
"All the measures can be plotted at once with the method plot (Figure 1), and the function consensusmap generates heatmaps of the consensus matrix for each value of the rank".
There is another type of image that can be generated after the clustering runs: the consensusmap. This one works on both cases (stepping and running).
The script is pretty short. Here it is:
library(NMF)
data = read.csv('R.csv', header=TRUE, sep=";")
res1 <- nmf(data, rank=2:5, nrun=1, "brunet", "random")
# this always works
capture.output(summary(res1) ,file = "summary.txt", append = TRUE)
# this always works too
png(filename = 'consensus.png', width = 1366, height = 768, units = 'px')
consensusmap(res1)
dev.off()
# this does not work on 'running mode', only 'stepping mode'
png(filename = 'metrics.png', width = 1366, height = 768, units = 'px')
# added hoping it would fix the issue. It didn't
Sys.sleep(2)
plot(res1)
# added hoping it would fix the issue. It didn't
Sys.sleep(2)
dev.off()
The summary.txt file is generated, the consensus.png too. The metrics.png is not. What's going on here??

R Need to restart RStudio to view and save in a file using dev.copy() and dev.off()

I am trying to create a plot and eventually save it as a file. But because I am making a lot of changes and want to test it out, I want to be able to view and save the plot at the same time. I have looked at this page to do what I want to do but in my system, it does not seem to be working as it is supposed to.
Here are my codes:
png('Save.png')
sample.df <- data.frame(group = c('A','B','A','C','B','A','A','C','B','C','C','C','B'),
X = c(2,11,3,4,1,6,3,7,5,9,10,2,8),
Y = c(3,8,5,2,7,9,3,6,6,1,3,4,10))
plot(Y ~ X, data = sample.df)
dev.copy(png, 'Save.png')
dev.off()
There are several issues (I am new to R so I might be missing something entirely):
(1) When I use png(), I cannot view the plot in RStudio so I used dev.copy() but it does not allow me to view my plot in R studio
(2) Even after I use dev.off(), I cannot view the saved file until I close the RStudio (says "Windows Photo Viewer can't open this picture because the picture is being edited in another program"). I need to restart every time so it is very inconvenient.
What am I doing wrong and how could I view and view saved file without restarting RStudio every time? Thank you in advance!
Addition
Based on Love Tätting's comments, when I run dev.list(), this is what I get.
> png('Save.png')
>
> sample.df <- data.frame(group = c('A','B','A','C','B','A','A','C','B','C','C','C','B'),
+ X = c(2,11,3,4,1,6,3,7,5,9,10,2,8),
+ Y = c(3,8,5,2,7,9,3,6,6,1,3,4,10))
>
> plot(Y ~ X, data = sample.df)
>
> dev.copy(png, 'Save.png')
png
3
> dev.off()
png
2
> dev.list()
png
2
> dev.off()
null device
1
> dev.list()
NULL
Why do I not get RStudioGD?
RStudio has its own device, "RStudioGD". You can see it with dev.list(), where it by default is the first and only one.
R's design for decoupling rendering and backend is by the abstraction of devices. Which ones you can use is platform and environment dependent. dev.list() shows the stack of current devices.
If I understand your problem correctly you want to display the graph first in RStudio, and then decide whether you want to save it or not. Depending on how often you save th image you could use the 'export' button in the plot pane in RStudio and save it manually.
Otherwise, your choice of trying to copy it would be the obvious one for me as well.
To my knowledge the device abstraction in R does not allow one to encapsulate the device as an object, so one for example could make it an argument to a function that does the actual plot. Since dev.set() takes an index as argument, passing the index as argument will be dependent on state of the stack of devices.
I have not come up with a clean solution to this myself and have sometimes retorted to bracketing the plot rendering code with a call to a certain device and saving it right after, and switching device depending on a global.
So, if you can, use RStudios export functionality, otherwise an abstraction would need to maintain the state of the global stack of devices and do extensive testing of its state as it is global and you cannot direct a plot call to a certain device, it simply plots to the current device (to my knowledge).
Edit after OP comment
It seems that it is somewhat different behaviour you are experiencing if you cannot watch the file after dev.off, but also need to quit RStudio. For some type of plot frameworks there is a need to call print on the graphical object to have it actually print to the file. Perhaps this is done by RStudio at shutdown as part of normal teardown procedures of open devices? In that ase the file should be empty if you forcibly look in its contents before quiting RStudio.
The other thing that sometimes work is to call dev.off twice. I don't know exactly why, but sometimes more devices get created than I have anticipated. After you have done dev.off, what does dev.list show?
Edit after OP's edit
I can see that you do, png(); dev.copy(); dev.off(). This will leave you with one more device opened than closed. You will still have the first graphics device that you started open as can be seen when you do the listing. You can simply remove dev.copy(). The image will be saved on dev.off() and should be able to open from the filesystem.
As to why you cannot see the RStudio graphics device, I am not entirely sure. It might be that other code is messing with your device stack. I would check in a clean session if it is there to make sure other code isn't tampering with the device stack. From RStudio forums and other SO questions there seem to have been plot pane related problems in RStudio that have resolved after updating RStudio to the latest. If that is a viable solution for you I would try that.
I've just added support for RStudio's RStudioGD device to the developer's version of R.devices package (I'm the author). This will allow you to do the following in RStudio:
library("R.devices")
sample.df <- data.frame(
group = c('A','B','A','C','B','A','A','C','B','C','C','C','B'),
X = c(2,11,3,4,1,6,3,7,5,9,10,2,8),
Y = c(3,8,5,2,7,9,3,6,6,1,3,4,10)
)
figs <- devEval(c("RStudioGD", "png"), name = "foo", {
plot(Y ~ X, data = sample.df)
})
You can specify any set of output target types, e.g. c("RStudioGD", "png", "pdf", "x11"). The devices that output to file will by default write the files in folder figures/ with filenames as <name>.<ext>, e.g. figures/foo.png in the above example.
The value of the call, figs, holds references to all figures produced, e.g. figs$png. You can open them directly from R using the operator !. For example:
> figs$png
[1] "figures/foo.png"
> !figs$png
[1] "figures/foo.png"
The latter call should show the PNG file using your system's PNG viewer.
Until I submit these updates to CRAN, you can install the developer's version (2.15.1.9000) as:
remotes::install_github("HenrikBengtsson/R.devices#develop")

Automaticly capture plots send to windows() device in r

I am trying to capture all plots a user makes in a script and save it in a folder. I have tried png("C:/path/to/plotfolder/Rplot%03d.png") and it works great for most plots. Unfortunately the department got alot of functions that use the windows() function. Those plots are not captured, and I've been unable to find a function that can capture plots send to those devices.
png("path/to/somewhere/plot.png")
windows()
plot(1:20)
dev.off()
This creates an empty file called plot.png.
I would really like to avoid forcing users to avoid those functions or rewrite them, so is there any way to capture plots send to a windows() device with a single call as in png()? If this is not the case, I guess i will have to just remove the windows() calls from the scripts or something.
Extra information
I need this since i'm trying to create a rscript function that runs a script and saves all output including warnings,errors, prints and plots to a specific folder that can then later be opened by another application. I would like not to interfere with the users code forcing them to make alternate functions when working with this script.
edit
Tried looking into knitr, as it seems to be able to find and save plots in windows() devices, but so far i have been unable to find anything. UPDATE: Not compatible with what i want
edit
I have found something that could work sometimes, but not a solution. Using savePlot in a while loop could save all open windows, but if several plots have been send to the device it will only save the last
I have found somewhat of a solution, though i would like a better solution. Since it's an supposed to run as an rscript i just replaced windows() with a wrapper for png(). As
windows <- function (width = 480, height = 480, pointsize = 12, record, rescale, xpinch,
ypinch, bg = "white", canvas, gamma, xpos, ypos, buffered, title, restoreConsole = TRUE,
clickToConfirm, fillOddEven, family = "sans", antialias = c("default", "none", "cleartype", "grey", "subpixel"),
filename = plotoutput) {
png(filename,width,height,pointsize,bg,restoreConsole,family,antialias)
}
Thanks for the help James.

R - Keep log of all plots

I do a lot of data exploration in R and I would like to keep every plot I generate (from the interactive R console). I am thinking of a directory where everything I plot is automatically saved as a time-stamped PDF. I also do not want this to interfere with the normal display of plots.
Is there something that I can add to my ~/.Rprofile that will do this?
The general idea is to write a script generating the plot in order to regenerate it. The ESS documentation (in a README) says it well under 'Philosophies for using ESS':
The source code is real. The objects are realizations of the
source code. Source for EVERY user modified object is placed in a
particular directory or directories, for later editing and
retrieval.
With any editor allows stepwise (or regionwise) execution of commands you can keep track of your work this way.
The best approach is to use a script file (or sweave or knitr file) so that you can just recreate all the graphs when you need them (into a pdf file or other).
But here is the start of an approach that does the basics of what you asked:
savegraphs <- local({i <- 1;
function(){
if(dev.cur()>1){
filename <- sprintf('graphs/SavedPlot%03d.pdf', i)
dev.copy2pdf( file=filename )
i <<- i + 1
}
}
})
setHook('before.plot.new', savegraphs )
setHook('before.grid.newpage', savegraphs )
Now just before you create a new graph the current one will be saved into the graphs folder of the current working folder (make sure that it exists). This means that if you add to a plot (lines, points, abline, etc.) then the annotations will be included. However you will need to run plot.new in order for the last plot to be saved (and if you close the current graphics device without running another plot.new then that last plot will not be saved).
This version will overwrite plots saved from a previous R session in the same working directory. It will also fail if you use something other than base or grid graphics (and maybe even with some complicated plots then). I would not be surprised if there are some extra plots on occasion that show up (when internally a plot is created to get some parameters, then immediatly replaced with the one of interest). There are probably other things that I have overlooked as well, but this might get you started.
you could write your own wrapper functions for your commonly used plot functions. This wrapper function would call both the on-screen display and a timestamped pdf version. You could source() this function in your ~/.Rprofile so that it's available every time you run R.
For latice's xyplot, using the windows device for the on-screen display:
library(lattice)
my.xyplot <- function(...){
dir.create(file.path("~","RPlots"))
my.chart <- xyplot(...)
trellis.device(device="windows",height = 8, width = 8)
print(my.chart)
trellis.device(device = "pdf",
file = file.path("~", "RPlots",
paste("xyplot",format(Sys.time(),"_%Y%m%d_%H-%M-%S"),
".pdf", sep = "")),
paper = "letter", width = 8, height = 8)
print(my.chart)
dev.off()
}
my.data <- data.frame(x=-100:100)
my.data$y <- my.data$x^2
my.xyplot(y~x,data=my.data)
As others have said, you should probably get in the habit of working from an R script, rather than working exclusively from the interactive terminal. If you save your scripts, everything is reproducible and modifiable in the future. Nonetheless, a "log of plots" is an interesting idea.

Resources