I am using a R package that renders the png images.
As I am running the command below in a script, I would like to avoid the error below and still be able to save the images.
How shall I do it ?
> ChIPQCreport(SAMPLE,
+ reportName = paste(ENTRY, "ChIPQC", sep="."),
+ reportFolder = paste(ENTRY, "ChIPQCreport", sep="."))
Saving 7 x 7 in image
Error in png_dev(..., res = dpi, units = "in") : X11 is not available
Related
When I try to save a ggplot to a new directory with ggsave(), I get an error.
library(ggplot2)
ggplot(mtcars, aes(x = cyl)) +
geom_bar()
ggsave("current_folder/new_folder/new_plot.png")
Produces the error:
Saving 11.3 x 7.32 in image
Error in png_dev(..., res = dpi, units = "in") :
unable to start png() device
In addition: Warning messages:
1: In png_dev(..., res = dpi, units = "in") :
unable to open file 'current_folder/new_folder/new_plot.png' for writing
2: In png_dev(..., res = dpi, units = "in") : opening device failed
If the target directory does not already exist, ggsave() will not create it as part of saving. It will instead throw this error.
First create the directory, either in your operating system outside of R or with dir.create like in this answer: https://stackoverflow.com/a/29784923.
Then save with ggsave().
I have R 3.5.1 and RStudio 1.1.453 running on MacOS High Sierra 10.13.5, platform x86_64-apple-darwin17.6.0. I want to understand why I can print and export plots but can't use a graphics device to save the plots.
I can use R's extrafont package to embed the Corbel font into PDF graphs. I can save these graphs by using print() and then exporting via the Plot window. Using this method, the font embeds and displays perfectly.
However, I want R to save several dozen such graphs without having to manually print() and save each one. There are various methods for this.
One is R's PDF Graphics Device. Unfortunately, this device does not seem to interact nicely with spaces and other characters. Here's the function that produces the ggplot:
jobreport_grouped <- function(table, N)
{
#Group
table <- table %>%
group_by(Education, Desired) %>%
summarize(n())
#Order
table <- arrange(table, desc(`n()`))
#Response label
response_count <- sum(table$`n()`)
#Sample size label
samplesize <- paste("(N = ", sum(table$`n()`), ")", sep="")
#Grouped barplot
ggplot(table[which(table$`n()`>N),],
aes(fill=Education,
x=reorder(Desired,-`n()`),
y=`n()`)
) +
scale_fill_manual(values = paletteIDCN) +
IDCNtheme_grouped_dense +
xlab(paste("Number of Active Members: ",
member_count,
"\n",
"Number of Responses: ",
response_count,
sep="")
) +
ylab("Number of IDCN Members") +
ggtitle(paste(desired_title("Professional Areas"),
"\n",
today,
sep = ""
)
)+
geom_col(position="dodge")
}
And then a call of this function along with an attempt to write the plot
> getGroupedJobReport <- function(A,n){
+ p <- jobreport_grouped(A,n)
+ ggsave(p, filename = "~/TMP-R/TDP/plots/Desired_GROUPED.pdf", device = pdf,
+ width = 12, height = 7, units = "in")
+ }
> getGroupedJobReport(A1,2)
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x20
2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x27
3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x20
After this, I tried a MacPorts installation of Cairo together with CRAN's Cairo package and got a different error:
> getGroupedJobReport <- function(A,n){
+ p <- jobreport_grouped(A,n)
+ ggsave(p, filename = "~/TMP-R/TDP/plots/Desired_GROUPED.pdf", device = cairo_pdf,
+ width = 12, height = 7, units = "in")
+ }
> getGroupedJobReport(A1,2)
Warning message:
In dev(file = filename, width = dim[1], height = dim[2], ...) :
failed to load cairo DLL
What alternatives to R's native PDF Graphics Device and Cairo are there? If any, which ones should I think of using?
I am trying to save a jpeg picture in png format using the magick package in R and I'm facing an error.
Below is the error that I get using this code:
library(magick)
testPic <- "https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/President_Roosevelt_-_Pach_Bros.tif/lossy-page1-165px-President_Roosevelt_-_Pach_Bros.tif.jpg"
image <- image_read(testPic)
image_info(image)
image_convert(image, format = "png", depth = NULL)
Error in magick_image_write(image, format, quality) :
Magick: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `' # warning/png.c/MagickPNGWarningHandler/1656
This is a bug in imagemagick. The workaround is to add strip = TRUE to image_read():
library(magick)
testPic <- "https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/President_Roosevelt_-_Pach_Bros.tif/lossy-page1-165px-President_Roosevelt_-_Pach_Bros.tif.jpg"
image <- image_read(testPic, strip = TRUE)
image_info(image)
image_convert(image, format = "png", depth = NULL)
I'll try to ping upstream again to fix this.
I am trying to make a gif out of an R-Script using a function to generate the images.
I have a function that given some information creates a Map with dots on it.
I use this function on a Vector obtaining a series of different images, and I would like to put them together in a gif. It looks more or less like that:
createMap <- function(my_variable){
my_map <- a_map() + geom_point() # some variable missing
png(filename = paste(aDate, ".png", sep = ""), width = 3149, height = 2183, units = "px")
plot(mw_map)
dev.off()
}
ImageMagick is installed on my pc and the conversion file "converter.exe" also. Later I try to generate the gif using
saveGIF({
lapply(my_vector, createMap)
}, movie.name = "MY_GIF.gif")
but I get an error message:
> convert: improper image header `Rplot1.png' #
> error/png.c/ReadPNGImage/4362. convert: no images defined `MY_GIF.gif'
> # error/convert.c/ConvertImageCommand/3254.
an error occurred in the conversion...
does anybody know what I did wrong?
After creating the map png files. Use the below code. You don't need ImageMagick is installed on PC.
library(magick)
png.files <- sprintf("Rplot%02d.png", 1:10) #Mention the number of files to be read
GIF.convert <- function(x, output = "animation.gif")#Create a function to read, animate and convert the files to gif
{
image_read(x) %>%
image_animate(fps = 1) %>%
image_write(output)
}
GIF.convert(png.files)
For more details check this link: Link
I get Rscript error of:
Error in dev.copy2pdf(file = fname, out.type = "pdf") :
can only print from a screen device
Execution halted
I am running the following R source in my Mac OSX console with:
Rscript --vanulla charts.R
I am using R version 3.3.2. Here is my source:
library(quantmod)
sym <- 'IBM'
d <- getSymbols(sym,src = "yahoo", auto.assign = FALSE)
chartSeries(d, name = sym, theme = "white", bar.type = 'ohlc',
line.type = "l",TA = "addVo();addSMA()",
subset = 'last 6 months')
addRSI()
dev <- dev.prev()
fname <- sprintf("%s.pdf",sym)
dev.copy2pdf(file = fname, out.type = "pdf")
dev.off()
How do I fix the dev.copy2pdf() if I want to output a PDF running on the conole. It runs fine within my RStudio,
Thanks
This seems to have fixed it
#https://stackoverflow.com/questions/5625394/problem-saving-pdf-file-in-r-with-ggplot2
pdf(fname)
chartSeries(d, name=sym, theme="white",bar.type='ohlc',line.type="l",TA="addRSI();addVo();addBBands();addSMA()",subset='last 6 months')
dev.off()