Run ggsave() in task manager - r

I'm trying to run this code in task manager. It runs successfully in Rstudio, but there is an error when I ran it in Task Manager. Here is the R code.
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('path/graph.pdf',
g,
device = 'png')
The error when I ran it in Task Manager looks like this:
Error in (function (file = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", :
cannot open file 'Rplots.pdf'
Calls: ->
Execution halted
The post below talks about the vanilla options when calling Rscript...but I couldn't figure out what the solution is...
Rscript ggplot - ggsave problem

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

Error in singlefold(obs, k) : insufficient records:2, with k=5 / Unused argument (kfolds = 4)

The code I used:
competition <- ENMevaluate(occ = occ_coord, env = env, bg.coords = bg_coord, method = "randomkfold", RMvalues=seq(0.5, 5, 0.5), fc = c("L", "LQ", "H", "LQH"), algorithm='maxent.jar')
Unable to solve this, previously used a different code which sad kfolds is an unused arguement that is why replaced it to the above^
Previous Code:
competition <- ENMevaluate(occ = occ_coord, env = env, bg.coords = bg_coord, method = "randomkfold", kfolds=4, RMvalues=seq(0.5,4,0.5), fc = c("L", "LQ", "H", "LQH"), algorithm='maxent.jar')
Previous Error:
Error in ENMevaluate(occ = occ_coord, env = env, bg.coords = bg_coord, : unused argument (kfolds = 4)
Anything will be helpful right now as I am very short on time (<24 hours).
Thank you!

Saving plot as jpg/tiff in R using ssh

I am using a particular R package called wTO (https://cran.r-project.org/web/packages/wTO/index.html)
One of the function which does the main computation also creates a plot among other things. As I am running the scripts on a CentOS server using SSH, I would like to save the plots. I am not sure where I should insert the commands for saving the plots. Kindly help.
The command is
> x= wTO.Complete(k = 32, n = 100, Data, Overlap, method = "p", method_resampling = "Bootstrap", pvalmethod = "BH", savecor = F, expected.diff = 0.2, lag = NULL, normalize = F, plot = T)
You need to open a graphic device before you call the function:
http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/png.html
After opening every plot will be written to this device until you call dev.off().
So you would do:
jpeg(filename = "yourname.jpeg")
x= wTO.Complete(k = 32, n = 100, Data, Overlap, method = "p", method_resampling = "Bootstrap", pvalmethod = "BH", savecor = F, expected.diff = 0.2, lag = NULL, normalize = F, plot = T)
dev.off()
You can then use/transfer your plot saved in yourname.jpeg.

Plots in for loop of R markdown not working

I am trying to use for loop in R markdown but it is not working for some reason. Here is the code:
```{r}
for (i in 1:length(endothelial_ids)) {
id <- endothelial_ids[i]
sym <- neuron_symbols[i]
VlnPlot(object = seurat_object, features.plot = id, do.return = TRUE) + labs(title = sym)
renderPlot({
fp <- FeaturePlot(object = seurat_object, features.plot = id, cols.use = c("grey", "blue"),
reduction.use = "tsne", do.return = TRUE)
lapply(fp, function(x){x + labs(title = sym)})
})
}
```
Without for loop the code is working and the generated .html report file looks good.
In the console I see several warnings:
output file: /private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl/cell_type_assignments.knit.md
Warning in file.rename(from, to) :
cannot rename file '/private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl/file613d370b37d8_files/figure-html' to '/private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl//private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl/file613d370b37d8_files/figure-html', reason 'No such file or directory'
Warning in file.rename(from, to) :
cannot rename file '/private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl//private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl/file613d370b37d8_files/figure-html' to '/private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl/file613d370b37d8_files/figure-html', reason 'No such file or directory'
Output created: /private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl/file613d370b37d8.html
Any suggestions would be greatly appreciated.
Update
If I wrap VlnPlot in print() function, it draws it, but not FeaturePlot:
print(VlnPlot(object = seurat_object, features.plot = id, do.return = TRUE) + labs(title = sym))
print(renderPlot({
fp <- FeaturePlot(object = seurat_object, features.plot = id, cols.use = c("grey", "blue"),
reduction.use = "tsne", do.return = TRUE)
lapply(fp, function(x){x + labs(title = sym)})
}))
And in place of the FeaturePlot I see the following in the report:
If I remove renderPlot() function and wrap lapply() in print(), it is printing me two plots instead of one: with the original wrong title and with the final correct one. So, I need a way to avoid printing the original one that happens just from the code:
fp <- FeaturePlot(object = seurat_object, features.plot = id, cols.use = c("grey", "blue"),
reduction.use = "tsne", do.return = TRUE)

different behavior on same code in different installation of r

When I run following code:
library(turboEM)
library(SparseM)
library(quantreg)
library(numDeriv)
library(foreach)
library(parallel)
libraary(iterators)
library(doParallel)
library(logbin)
library(glm2)
#-------------------------------------------------------------------------
data(heart)
head(heart)
start.p <- sum(data1$deaths) / sum(data1$patients)
fit.glm <- glm(cbind(deaths, patients-deaths) ~ factor(age) + factor(severity) +
factor(onset) + factor(region), family = binomial(log),
start = c(log(start.p), -rep(1e-4, 8)), data = data1,trace = TRUE, maxit = 100)
fit.logbin <- logbin(formula(fit.glm), trace = 1,data=data1)
summary(fit.logbin)
# Speed up convergence by using single EM algorithm
fit.logbin.em <- update(fit.logbin, method = "em")
summary(fit.logbin.em)
# Speed up convergence by using acceleration methods
fit.logbin.acc <- update(fit.logbin, accelerate = "squarem")
summary(fit.logbin.acc)
fit.logbin.em.acc <- update(fit.logbin.em, accelerate = "squarem")
summary(fit.logbin.em.acc)
sink("C:\\Users\\ak.biglarian\\Desktop\\MrsZooghi\\Out.logbin.txt")
summary(fit.logbin.em)
sink()
#-------------------------------------------------------------------------
logbin(formula = formula(fit.glm), data = heart, method = "em",
trace = 1)
on different installations of R-project, I get different results on same code!
In one installation I get the code running pretty good but in another installation I get following errors:
> fit.logbin <- logbin(formula(fit.glm), trace = 1,data=data1)
Error in logbin(formula(fit.glm), trace = 1, data = data1) :
unused argument (data = data1)
or
> logbin(formula = formula(fit.glm), data = heart, method = "em",
+ trace = 1)
Error in logbin(formula = formula(fit.glm), data = heart, method = "em", :
unused arguments (formula = formula(fit.glm), data = heart, method = "em")
What is the reason? And how shall I fix this?

How do I embed surface plots in html documents using knitr and plotly?

I think the R Markdown (Rmd) code below should produce a html document with a surface plot embedded in it.
```{r, plotly=TRUE}
library(plotly)
py <- plotly()
x_vec = c(seq(-5, 4.9, 0.1))
x_matrix = matrix(c(x_vec), nrow = 100, ncol = 1)
y_matrix = matrix(c(x_vec), nrow = 1, ncol = 100)
data <- list(
x = x_vec,
y = x_vec,
z = matrix(c(cos(x_matrix %*% y_matrix) + sin(x_matrix %*% y_matrix)), nrow = 100, ncol = 100),
type = "surface")
layout <- list(
title = "Waaaves in r",
scene = list(bgcolor = "rgb(244, 244, 248)"))
response <- py$plotly(data, session="knitr",
kwargs = list(
layout = layout,
world_readable=FALSE,
filename = "waves example",
fileopt = "overwrite"))
```
The document creates an output with the code embdedded, but there is no plot. Just an error message saying,
"Uh oh, an error occured on the server."
Any idea what is going on here?
Instead of running py$plotly(), run py$irplot() (without the session="knitr" argument). The latter works with the py$ggplotly() method, not the py$plotly() one.

Resources