ggsave error: cannot find function "dev" - r

I get the following error when trying to save my ggplot:
Error in ggsave(paste0(projDir, "figs/lmGtaGridCombined.png")) :
could not find function "dev"
This was code that worked earlier this year, before the newest ggplot(2.0.0). The 3rd party code I'm using (https://rpubs.com/Koundy/71792; which uses ggthemes) worked great earlier this year to make my images more publication-friendly. However, now the ggthemes code gave errors related to:
Error in FUN(X[[i]], ...) :
Theme element 'text' has NULL property: margin, debug
In addition: Warning message:
`axis.ticks.margin` is deprecated. Please set `margin` property of `axis.text` instead
>
which I thought I fixed by changing this line:
text = element_text()
to this:
text = element_text(margin=margin(), debug= F),
which made a version of my image print, but many aspects (ie, x-label spacing flush instead of offset) not right. Plus- ggsave gave the above error, which is now my biggest problem, as I am considering manually fixing the image if I can just get it to print similarly to as it did before.
I've tried png ..... dev.off but I can't get it to print in the right dimensions and resolution as it did originally with ggsave.
Please help. I've been scared to post on stackoverflow up until now, but I'm very desperate. Thanks in advance.

Related

Error in upset_data : unused argument (nsets = 5) What could be causing this and how coul I fix the issue?

I am very new to R and I would like to make UpSet plot of peptides.
I have five lists (sets) in .txt format (ID_list_vysledok_pos_vs_healthy_files.txt, ID_list_vysledok_pos_neg_files.txt, ID_list_vysledok_New_vs_Old_collumn_files.txt, ID_list_vysledok_neg_vs_healthy_files.txt, ID_list_vysledok_Coated_vs_Liquid_junction_files.txt)
Each .txt file is just list of peptides in this format: List of peptides
I was following step by step instructions by this link : http://genomespot.blogspot.com/2017/09/upset-plots-as-replacement-to-venn.html
The code is working without problems until I want to make the graph with : upset(res2, nsets=5)
It gives me following error: Error in upset_data(data, intersect, mode = mode, encode_sets = encode_sets, : unused argument (nsets = 5)
The res2 dataframe looks like it should be looking to make this graph : View(res2)
I think the problem is trivial and sets may not be properly defined (but in the link I was following it is working), but I was not able to fix this...
I installed these libraries : library(plyr), library(reshape2), library(UpSetR)
Just to be sure I will provide the code (same as in link): Photo of code
Thank you for any suggestions.
It looks like you might be mixing up packages. You are trying to use upset_data function from ComplexUpset package, but passing nsets argument which works with upset from UpSetR package. I would recommend taking a closer look at which functions you use. If you want to use both UpSetR and ComplexUpset, you can distinguish them with ComplexUpset::upset and UpSetR::upset.

Why am I receiving "plow.new has not been called yet" when trying to plot PCAs?

This is the end goal however when trying to plot the code below
plot(PCAloadings[,1:2], arrows(0,0,PCAloadings[,1],PCAloadings[,2]), text(PCAloadings[,1:2], labels=rownames(PCAloadings))
I recieve the following error:
in text.default(PCAloadings[, 1:2], labels = rownames(PCAloadings)) : plot.new has not been called yet
I am unsure how to resolve this issue or what it pertains to, any help is welcome.
EDIT: when trying to run line by line only the first line runs. When trying to run arrows or text lines I run into the same error
plot(PCAloadings[,1], PCAloadings[,2])
This is the output from the first line alone
Seems to be a syntax problem. Try
plot(PCAloadings[,1:2])
arrows(0,0,PCAloadings[,1],PCAloadings[,2])
text(PCAloadings[,1:2], labels=rownames(PCAloadings))

openmap NullPointerException Error in osmtile could not obtain tile

I am trying to plot a small rectangle of a map:
library(OpenStreetMap)
upper_left <- c(47.413, 8.551);
lower_right <- c(47.417, 8.556);
map_osm <- openmap(upper_left, lower_right, type = 'osm' );
plot(map_osm );
When I run that, the openmap function gives me the error Error in osmtile(x%%nX, y, zoom, type) : could not obtain tile: 540 298 10.
The documentation of OpenStreetMap seems to indicate that I need to add an API key. However, I am not sure how exactly I would do that (because I use type='osm', not type = url) and I am also unclear where I'd get such an API key from.
The java.lang.NullPointerException and the following R-error (Error in osmtile(...)) seem to come from an older version of OpenStreetMap.
By updating OpenStreetMap to the latest version (0.3.4 currently), the error disappears and the example code of OP should work as it is, without needing an API key.
The accepted answer is not adequate as the error can occur even with the most recent package version.
Sometimes if a particular area is not available in a specific style, you get an error similar to the one mentioned above independent of the package version. The solution would be to try the function with a different style. This is mentioned in the following blog post
As an example, the following modification may solve the issue:
library(OpenStreetMap)
upper_left <- c(47.413, 8.551);
lower_right <- c(47.417, 8.556);
map_osm <- openmap(upper_left, lower_right, type = 'opencyclemap');
plot(map_osm)

grDevices::dev.new() does not work in the first time

My question: How to let grDevices::dev.new() work well ?
To avoid the error Error in plot.new() : figure margins too large coursing the small plot region,
I wrote the code grDevices::dev.new() in the front of plot().
However this code does not work in the first time (or after pushing the button .clean all plot button )
And this cause the errors (e.g., in the R CMD check).
Do I misunderstand the function grDevices::dev.new() ?
REF?:
R: Open new graphic device with dev.new() does not work
Answer
Using grDevices::windows() instead of grDevices::dev.new(), I overcome the issues.
Unfortunately, we cannot use grDevices::windows() , because the following error occurs in the R CMD check:
Error in grDevices::windows() :
screen devices should not be used in examples etc

Error in file(con, "w") : cannot open the connection [Using R-Studio to plot interactive bar graphs using rCharts, knitr]

I am getting an error when I am trying to run the code below in R-Studio 3.3.2 on a Mac (OS Sierra)
devtools::install_github('ramnathv/rCharts')
install.packages("knitr")
require(rCharts)
require(knitr)
haireye <- as.data.frame(HairEyeColor)
n1 <- nPlot(Freq ~ Hair, group = 'Eye', type = 'multiBarChart',
data = subset(haireye, Sex == 'Male')
)
n1$save('fig/n1.html', cdn = TRUE)
cat('<iframe src="fig/n1.html" width= 100%, height=600</iframe>')
Pls see output below:
Error in file(con, "w") : cannot open the connection
In addition: Warning message: In file(con, "w") : cannot open file 'fig/n1.html': No such file or directory
But I am able to generate the reqd bar graph in the viewer when I use:
n1$show(cdn = TRUE)
in lieu of n1$save('fig/n1.html', cdn = TRUE)
To take care of write permission issues (if any), I also tried including the line below, altering the WD path wherever necessary.
knitr::knit2html('Users/documents/n1.html')
But it did not help. I see the n1.html file created but it only opens an empty browser.
Any help to resolve this is appreciated.
Best,
S
A lot of times we face this error due to caching in RStudio and in that case, actual code errors don't show up. Restart RStudio and this error will be gone and actual code errors would show.
You have two separate problems.
The connection error appears because the fig/ folder does not exist. Create the folder and the save command will work. R has functions to check the existance of directories and create new ones if you would like to do it in your code.
The second problem comes from the way you save, you should use n1$save('fig/n1.html', standalone = TRUE). Here you have a similar situation.
As a side-note, I would say rCharts is not currently developed or mantained at all, so I would recommend you to use another library for your charts. In my opinion Plotly is quite nice. rCharts brought the NVD3 project to R and the chart style is in my opinion really nice. However, as far as I know both projects are stopped so I would look for a library that is still alive.
I have fixed this problem with good old rm(list=ls()) . I know I have
fallen into sequences where the error stops execution of my script. I fix the error, and then it won't run. This is likely due to lazy evaluation but it is a near impossible problem to diagnose, so the solution at the top works almost all the time.

Resources