windows() closing when R terminates - r

I wonder if there is a workaround for how to keep output from windows() open after R terminates.
To specify. I've built a system for production which is run from BASH. BASH runs the R instance and terminates it when it is finished. During the run it produces plots which i would like to display and keep open even after the R instance terminates so I can decide to keep them or not.
Can you keep the opened windows from closing or is there an alternative?
Don't really know what the example code would be here... since all im using is grDevices::windows().
Any suggestions?
# Plot data for overview ----
if(cfg$verbose) cat(crayon::green("Plot data for visualization of
problem\n"))
plots <- Learning_package::plot_iris(dat)
grDevices::windows();gridExtra::grid.arrange(plots$p1, plots$p2,
plots$p3, plots$p4,
plots$p5, plots$p6,
nrow = 2)
Expected result is to NOT close the window when bash is finished with its run.

Related

How to stop taking input from standard input in rstudio

I actually want to take input from R in script mode (i.e. I'll copy and run the program written in an RScript) and I am using the readLines function for that (since I've come to know that readline function is meant to be used only in interactive mode). When I run the following code (in rstudio),
k = as.integer(readLines("stdin",n=1))
x = c()
it starts taking input, but the problem is that it doesn't stop taking input. Even if I click on the red octagon, it is not stopping and neither am I able to quit the session. I've to restart the computer. Any help on how can we stop it?? (I'll also be very happy if you suggest some better function to take and process input in script mode)

Why is source speed different from RStudio console line code?

I have a script with self-written functions (no plots). When I copy-paste that script into the R-Studio console, it takes ages to execute, but when I use source("Helperfunctions.R") it doesn't take more than a second.
Question: Where does the difference in speed come from?
I am aware of two differences between running code via the source() function vs. entering code at the R-Studio console:
From ?source:
Since expressions are not executed at the top level, auto-printing is not done.
The way I understand this: source() will not plot graphs (unless made specific with e.g. print(plot)), while the R Studio console codes will always plot graphs. I'm sure this will affect the speed of execution to a certain degree, but this seems irrelevant in my case, because there are barely any plot calls.
And:
(...) the complete file is parsed before any of it is run
I have been working with R for a while now, but I'm not sure whether this relevant for the speed-issue I'm having. Is it possible that completely parsing all code "before any of it is run" speeds up the execution of my helper functions script by a factor of a hundred?
Edit: I'm using R version 3.2.3.
The issue is not source() vs. console line code. Instead, it is an issue of how RStudio sends code from the source pane to the console.
When I copy the content of Helperfunctions.R and run it in RGui (instead of RStudio), the code is executed with nearly the same speed as when I use source("Helperfunctions.R") in RStudio.
Apparently, lines of code always (?) require more execution time in RStudio than in RGui. Even though you may usually not notice the time-difference when executing a couple of lines in the console, it seems to make a huge difference when, say, 3.000 lines of code are being executed in the R Studio console at once.
My understanding is that upon using source("Helperfunctions.R") in the RStudio source pane, the code is not actually sent to the RStudio console (which would have been slow), but is actually executed directly in the R language.

Can't seem to turn off a device using dev.off() in R

I'm using Jupyter notebooks running an R kernel (3.2.2) in OSX, working through some basic stats exercises. A few days ago I played with ggplot for the first time, and ran some commands from a notebook cell experimenting with printing to pdf files, in other words using something like the following: pdf("file.pdf"); plot(x, y); dev.off() I didn't keep any of those cell contents, so I can't see exactly what I ran but I imagine it is possible I create some pdfs and didn't include a dev.off() command?
My problem is, since that session, now every cell I run in any notebook (including new blank ones) generates an unwanted pdf file. If that code involves plotting a figure, the pdf contains that figure, if the code is anything else, the pdf created is unable to be opened. I can't find any way, elegant or brutish, to stop these pdfs from being created.
If I go to a new blank notebook:
running dev.list() returns pdf: 2
running dev.cur() returns pdf: 2
running dev.off() returns null device: 1
but then, if immediately after, I run either dev.list() or dev.cur() again, they again return pdf: 2
I am able to open additional new devices, and dev.off() succeeds in closing them. But this pdf: 2 device won't go away. I tried terminating all my sessions, rebooting my machine, etc., to no avail; Suggestions?
Additionally, If I run a pdf() command from a console in Terminal (i.e., not from a notebook), my plots show as being generated by Quartz; I understand this might be a Quartz problem and not an R problem, but my question remains, how do I close this device, or otherwise stop all these pdfs from being created?
This may be a problem when a sink() has been opened but not closed, perhaps because an error occurred before the closing code was reached.
Try this to plug an open sink:
sink(NULL)
ht #mdsumner
graphics.off()
should forcefully close all devices. Check out this page for more useful information.

Make an R script write lines to the console faster (Rgui)

I've written quite a large R script (1000+ lines). Currently there is a rm(list=ls()) statement at the top of the script, as I need to test how it runs cleanly.
I run the code by ctrl + A, ctrl + R
The problem is is that this seems to take a long time in the Rgui to write each line to the console before running it. I feel R should be able to write to the the console faster than this and was wondering if there is a faster way to run a script.
(ie hide the lines written to the console and just run the script)
Best way is to simply source the document. If you have it saved then just type source("FullPathOfmyfile.R") and it will run without printing the commands, it will only print the output and print statements. Alternatively you can set echo = FALSE.

R script using X11 window only opens for a second

I'm running an R script via my Linux Mint 16 command line. It contains a boxplot that I want to display in a window. So I'm using x11() function for creating that window. Here is my code:
testdata <- data.frame(sample(1:1000,size=100,replace=T), row.names=NULL)
colnames(testdata)<-c("data")
x11()
boxplot(testdata, main="Boxplot der Testdaten", horizontal=TRUE)
When I run this function in Rstudio, it will open a window and show the boxplot created. But whenever I run it from the command line of my Linux Mint 16 machine, the window will open for a second and then close again. I can see the boxplot for a second. I couldn't really find a reason for this. I'm quite new to R and never used X11 before. Any ideas would be really appreciated. Thanks!
This is more-or-less a FAQ. Part of this is that you seem to misunderstand how all commands terminate. I.e. when you call ls it does not stop either.
So here you need to something extra. Possibly approaches:
Just sleep via Sys.sleep(10) which would wait ten seconds.
Wait for user input via readLines(stdin()) or something like that [untested]
Use the tcltk package which comes with R and is available on all platforms to pop up a window the user has to click to make the click disappear. That solution has been posted a few times over the years on r-help.
But in this day and age, you may also rethink the issue. I had good success preparing analysis and visualization for colleagues via the most-awesome shiny package which displays to a web page. Everybody has a web browser...
You can sleep until all windows are closed
while(names(dev.cur()) !='null device') Sys.sleep(1)
on my machine, after calling x11(), names(dev.cur()) is "X11cairo". After closing all/any windows opened with x11, names(dev.cur()) becomes "null device"
testdata <- data.frame(sample(1:1000,size=100,replace=T), row.names=NULL)
colnames(testdata)<-c("data")
x11()
boxplot(testdata, main="Boxplot der Testdaten", horizontal=TRUE)
# wait until window is closed (check every second)
while(names(dev.cur()) !='null device') Sys.sleep(1)

Resources