Disable GUI, graphics devices in R - r

Is there an easy way to turn of all GUI elements in R and run it solely from the command line on OSX?
I'm trying to replicate the behavior of a remote linux terminal on my OSX machine. Thus plot() should just save a file and things like CRAN mirror selection should be text, not a Tk interface. I'm having trouble finding where to set this behavior.

I had this exact question and wanted a way to do it without changing my existing code. I usually run with graphics support but sometimes I'll run a script on the server for a larger dataset and then I just want the plots to be output somewhere automatically.
In Dirk's answer Ian Fellows gives the simple solution. On the command line in R type:
options(device=pdf)
And then any plots will be written to the current directly to an Rplots.pdf file.
If you want the files to not be plotted at all then use
options(device=NULL)

For the plots you can just direct the output to a file using the pdf() command (or png(), jpeg()...).

I don't own an OS X box, but did you try to unset the X11 environment variable DISPLAY:
DISPLAY="" R --vanilla
When I do that on Linux and query R for capabilties(), x11 comes up as FALSE as desired.

I don't run OSX but you could attempt to run R from the Terminal application, rather than the Mac OSX launcher, and see whether that runs as you need.
As Matti writes, you can send output to files using the following commands; but I don't know if that's really the substance of your question.
png("pngfile.png")
plot(foo)
title(main="bar")
dev.off()
So instead of the quartz graphical object, your output goes to the file.
Similarly, you can output what would normally appear in the terminal to a file.
sink("foo.file")

Related

Rgui command-line for sourcing R file

What command line option to use behind Rgui.exe for immediately sourcing an R source file? Instead of having to type source("c:\MyGreatSource.R") manually afterwards. Something like:
Rgui.exe --source "c:\MyGreatSource.R"
Sounds like a simple question answered in any beginner's manual, but I couldn't find such an option anywhere.
I found a workable solution, maybe others are interested. Again, what I like to do is to start the Rgui and work there. All my work environment and functions are defined in an R source file, which I constantly develop further during working. So each of my commands in the GUI starts with Load1(); where Load1 is a function which simply sources my R file, to update the changes I have just made. Obviously, Load1 is also defined in my R file, so I need to get it in the first place, without much effort. I have set the command-line options for neither loading nor saving the workplace; I don't like my old mess from the previous session with test variables and so.
However, my solution now is to just create a workplace RData file which only contains the definition of my Load1 function. This workplace file is easily loaded at every start by just adding its path into the command-line options "D:\MyLoad1.RData"
I use a AutoHotkey Script
run,C:\Program Files\R\R-3.3.3\bin\x64\Rgui.exe
WinWait,RGui (64-bit)
WinWaitActive,RGui (64-bit)
Sleep 100
Send,source("%1%")
Send,{enter}

Run R (on Linux) interactively with a simple repl (no readline, no assumed terminal)

I'd like to run R as an inferior process. I'd like R to display help pages as html (in the browser) and to display plots as if it were run interactively. I know I can run R with the --interactive argument but then R seems to assume it's running on a terminal end emit control sequences.
How should I run R to get html help pages & plot (as with --interactive) and simple textual output with no control sequences?
If I use R, it assumes to be run non-interactively (html help pages or plots won't work but the output contains no control sequences).
If I use R --interactive --no-readline, it runs interactively (help pages and plots work as expected) but the output is garbled and difficult to parse since it assumes to be running on a terminal.
Is there a way to control the assumptions R makes about the terminal it's running in?
It seems that argument order matters. (Part of) the problem gets resolved when calling R as R --no-readline --interactive.

Octave -- Plotting from Terminal Command Line

Background:
I'm new to Octave, as of today.
I am running GNU Octave, version 4.0.2 on Ubuntu 16.04 LTS
I'm using the command-line interface at the terminal (ie, open the terminal and, type "octave").
I'm aware that there is a GUI, but I want to see if I can get this working in the terminal [such a setup is my preference in most applications]
PROBLEM:
Plots are plotted in text. As in:
How can I get a good-looking plot in a plotting window? I genuinely haven't been able to find this information in 10 minutes of Googling...I think it should be easier than that....maybe someday this post will be the easy solution for others.
Best,
-Ryan
I personally do this a lot; it looks like you have it installed in a *nix environment... Octave can produce nice looking figures through cooperation with a couple graphic toolkits, GNUPlot being the one i'm more familiar with.
Check out the documentation at: https://www.gnu.org/software/octave/doc/v4.0.1/Plotting.html
Without seeing your code we're kind of just guessing. I would try something like
figure();
to see if you get a fresh plot window, or try
available_graphics_toolkits()
to ensure that you actually have something installed to generate separate figures.

Starting R and calling a script from a batch file

I have an R-based GUI that allows some non-technical users access to a stats model. As it stands, the users have to first load R and then type loadGui() at the command line.
While this isn't overly challenging, I don't like having to make non-technical people type anything at a command line. I had the idea of writing a .bat file (users are all running Windows, though multi-platform solutions also appreciated) that starts R GUI, then autoruns that command.
My first problem is opening RGui from the command line. While I can provide an explicit path, such as
"%ProgramW6432%\R\R-2.15.1\bin\i386\Rgui.exe"
it will need updating each time R is upgraded. It would be better to retrieve the location of RGui from the %path% environment variable, but I don't know an easy way to parse that.
The second, larger problem is how to call commands for R on startup from the command line. My first thought is that I could take a copy of ~/.Rprofile, append the extra command, and then replace the original copy of the file once R is loaded. This is awfully messy though, so I'd like an alternative.
Running R in batch mode isn't an option, firstly since I can't persuade GUIs to display themselves, and secondly because I would like the R console available, even if the users shouldn't need to use it.
If you want a toy GUI to test your ideas, try this:
loadGui <- function()
{
library(gWidgetstclck)
win <- gwindow("test")
rad <- gradio(letters[1:3], cont = win)
}
Problem 1: I simply do not ever install in the suggested default directory on Windows, but rather group R and a few related things in, say, c:/opt/ where I install R itself in, say,c:/opt/R-current so that the path c:/opt/R-current/bin will remain constant. On upgrade, I first renamed to R-previous and then install into a new R-current.
Problem 2: I think I solved that many moons ago with scripts. You can now use Rscript.exe to launch these, and there are tcltk examples for waiting for a prompt.
I have done similar a couple of times. In my cases the client was using windows so I just installed R on their computer and created a shortcut on their desktop to run R. Then I right click on the shortcut and choose properties to get the propertiest dialog. I then changed the "Start in" folder to the one where I wanted it to run from (which had the .Rdata file with the correct data and either a .First function in the .Rdata file or .Rprofile in the folder). There is also a "Run:" option that has a "Minimized" option to run the main R window minimized.
I had created the functions that I wanted to run (usually a specialized gui using tcltk) and any needed data and saved them in the .Rdata file and also either created .First or .Rprofile to run the comnand that showed the gui. The user double clicks on the icon on the desktop and up pops my GUI that they can work with while ignoring the other parts.
Take a look at the ProjectTemplate library. It does what you want to do. It loads used libraries from a batch file an run R files automatically after loading as well as a lot of other usefull stuff as well...
Using the answer from https://stackoverflow.com/a/27350487/41338 and a comment from Richie Cotton above I have arrived at the following solution to keeping a script alive until a window is closed by checking if the pointer to the window is valid.
For a RGtk2 window created and shown using:
library(RGtk2)
mainWindow <- gtkWindow("toplevel", show = TRUE)
Create a function which checks if the pointer to it exists:
isnull <- function(pointer){
a <- attributes(pointer)
attributes(pointer) <- NULL
out <- identical(pointer, new("externalptr"))
attributes(pointer) <- a
return(out)
}
and at the end of your script:
while(!isnull(mainWindow)) Sys.sleep(1)

avoid displayed figures during sweave/pgfsweave compilation

When compiling with sweave/pgfsweave, every time a figure is created in R it is shown in a graphics windows (during the sweave compilation process). This is helpful in many cases as I can see what the figures look like as the document is being compiled.
But when I compile through ssh a large document this can be very slow. Is there a way to tell sweave/pgfsweave to avoid displaying the figure during the compilation (I still want the figure in the final pdf document though).
For interactive sessions, the figs.only Sweave option controls this behavior. To plot figures only to the target graphics files (and not to a console graphical window) set figs.only=TRUE.
As explained in the RweaveLatex help file:
figs.only: logical (‘FALSE’). By default each figure chunk is run
once, then re-run for each selected type of graphics. That
will open a default graphics device for the first figure
chunk and use that device for the first evaluation of all
subsequent chunks. If this option is true, the figure chunk
is run only for each selected type of graphics, for which a
new graphics device is opened and then closed.
As with other Sweave options, you can set this option: (1) for the current compilation (e.g. Sweave("example.Rnw", figs.only=TRUE); (2) within the .Rnw file, using \SweaveOpts{figs.only=TRUE}; or (3) as a global default, by putting SWEAVE_OPTIONS="figs.only=TRUE" in, e.g., $R_HOME/etc/Renviron.site
figs.only is the correct way to go, and I also want to mention the default graphical device in R here:
For now you may look at this: http://yihui.name/en/2010/12/a-special-graphics-device-in-r-the-null-device/
After R 2.14.1 (not released yet) you will be able to set the default device to a null PDF device, which is both safe and fast: https://github.com/yihui/knitr/issues/9
If you sweave from the command line instead of in an interactive session, graphics aren't produced in an interactive graphic window.
You can run R from the command line by just typing R CMD Sweave mydoc.Rnw or via a batch file, or a makefile for larger projects. I've started to use makefiles for many of my sweave documents as it handles dependencies, can clear up after itself and much more.
One option could be
<<label=myplotlabel, fig=TRUE, include=FALSE>>=
graph code
#
then
\begin{figure}[h]
\includegraphics[width=6cm, height=6cm]{myplotlabel}
\caption{My Plot}
\label{fig:label}
\end{figure}

Resources