Error in dev.off() : cannot shut down device 1 (the null device)
Turns out that the only problem with this was with where the file was trying to save to. I altered the code to save to a different directory and it worked fine
Function that saves to files produces an error
Change your code like so
f1<-function(x){
write.table(as.matrix(summary(x)),file="dane.txt",sep=";",row.names=T)
#I want here to make two plots.
png(filename="hist.png")
hist(x)
dev.off()
dev.cur()
png(filename="density.png")
plot(density(x))
dev.off()
}
You only missed the () of dev.off()
How to plot in RStudio and not have a new window pop up (R Graphics: Device (ACTIVE)?
For others who like me may still encounter this issue:
This is probably caused by an R update to 3.3.2 and is fixed by installing a newer version of RStudio. In my case 1.0.136 did the trick.
Suppressing null device output with R in batch mode
One of the nice things about R is that you can view the source of many functions:
> dev.off
function (which = dev.cur())
{
if (which == 1)
stop("cannot shut down device 1 (the null device)")
.Internal(dev.off(as.integer(which)))
dev.cur()
}
<environment: namespace:grDevices>
So it calls .Internal(dev.off(...))
and then returns dev.cur(), which I suppose would be useful if you have several devices open so you know which one became active. You could use .Internal(dev.off(as.integer(dev.cur())))
in your script, or even patch dev.off
to only return the value of dev.cur()
if it is something else than the null device, and send the patch to the maintainers of R.
Also, graphics.off()
calls dev.off()
for all devices and doesn't return anything.
Related Topics
Extract First N Digits from a String
Creating a Stacked Bar Chart Centered on Zero Using Ggplot
R: How to Filter a Timestamp by Hour and Minute
Split Line by Multiple Points Using Sf Package
Embed Instagram/Youtube into Shiny R App
How to Append R Data Frame into Existing Excel Without Overwriting
Using Dplyr to Group_By and Conditionally Mutate a Dataframe by Group
Include Link to Local HTML File in Datatable in Shiny
R: Xmleventparse with Large, Varying-Node Xml Input and Conversion to Data Frame
Passing a List of Arguments to a Function with Quasiquotation
How to Use Different Font Sizes in Ggplot Facet Wrap Labels
How to Use Custom Cross Validation Folds with Xgboost
R Finding Duplicates in One Column and Collapsing in a Second Column
Shiny Sliderinput from Max to Min
How to Get All Possible Combinations of N Number of Data Set