prevent rgl from opening window in package examples - r

I have several packages that make extensive use of rgl in documentation examples. Every time I do R CMD check, every such example opens a collection of rgl windows briefly, which is annoying.
Is there some rgl setting I can use in the .Rd files to suppress this behavior? Is there any downside to doing this?

Set the environment variable RGL_USE_NULL=TRUE and rgl won't try to open any windows.
This should be done outside of R, before running R CMD check. It can also be done inside R using Sys.setenv(RGL_USE_NULL = TRUE) before loading rgl if you don't want to see rgl displays during an interactive session.

Related

R : GUIs fast to launch (alternatives to shiny ?)

I am trying to make some GUI for my CRAN package
some f function that locally launches a popup with a few params rendering a few outputs, like launching locally a simple shiny app.
the popup permits to select the parameters easily and then a "validate" button triggers the return of fwith the selected parameters, like shiny with stopApp
I have already done that with shiny but i think the result is a bit unsatisfying because shiny apps are slow to launch and stop (if there is an object to serialize i guess).
I have seen that some packages, like vdiffr, seem to answer this problem by making a list of the objects to change, thus they only launch shiny once to change everything with some selecter. If possible I'd like to avoid this solution.
So the question is : is there some kind of GUI framework more convenient than Shiny for this (only local, fast launch and fast return) ? Or some fine way to do that, that is light enough (I don't want to make my package to be extraordinary heavy for a small popup) ?
Here are GUI facilities that come with R (no packages needed):
(1) For sufficiently simple applications select.list, menu, readline, file.choose and choose.dir can be used and will present with a text or graphical user interface depending on what environment the user has. These all come with R and launch very quickly and are easy to program. See the help pages of those commands and try:
select.list(c("oranges", "apples", "pears"))
menu(c("oranges", "apples", "pears"))
readline("Enter name of fruit: ")
choose.dir()
file.choose()
(2) A possibility which is capable of user interfaces as sophisticated as shiny is the tcltk package. This comes with R so it does not need to be installed. It only needs to be loaded using a library statement. (If a user builds R from source then it is possible to build it without tcltk capability but in 99% of all cases tcltk will be present).
There are a few short examples of R source code employing tcltk here: https://www.stat.berkeley.edu/~s133/Gui-a.html, quite a few more examples at James Wettenhall's site and one can run the Rcmdr R package to see an example of a sophisticated tcltk user interface although running Rcmdr will take longer to load than a simple application due to its large size.
Because tcltk is a part of R itself it does not have a CRAN page but for additional packages that use tcltk look at the reverse dependencies of the tcltk2 package at the bottom of its CRAN page since any package that depends on it must also use tcltk.
That really depends on your coding knowledge. My first approach, however, would be to try to improve your shiny app. For that you can use the profvis package to check why exactly your shiny app is slow. Should you decide to stay with Shiny, there a lot of ways on how to reduce the starting time of Shiny. However, if you should come to the conclusion, that you really want to throw shiny overboard, then you should consider using JavaScript.
Either you write your complete app in pure JavaScript
Or you can use JavaScript within Shiny (e.g. r2d3)
Of course there are other ways as #Pork Chop mentioned of using rmarkdown or normal markdown.

Unable to get plots displayed in Rstudio graphical viewer.

When I run my code all my graphs (both ggplot2 and plot) are displayed in an external "Quartz 2 [*]" graphical viewer.
I would like them to be displayed in the R studio GUI plot area to I can better save and view my files, along with the previous versions.
Is there anyway to stop this?
I recently updated my version of R, along with the X11 and xQuartz on my mac (also up to date), and I am guessing these updates are behind it.
I have seen many forums explaining how to get rstudio to export to an external viewer (e.g. "quartz()"), but not the other way.
I have looked, but these threads have not helped:
ggplot plots in scripts do not display in Rstudio
plot panel does not produce plots Rstudio
dev.off() even when hidden hide <- dev.off() is not working either.
Any help would be great,
Thanks.
If you upgrade R without upgrading RStudio, the graphics engine may not be compatible. To fix the "Quartz 2" popout, upgrade RStudio to a newer version that supports the graphics engine in the version of R you have installed.
I had the same problem, and noticed the following output to the console:
Warning message:
R graphics engine version 15 is not supported by this version of RStudio. The Plots tab will be disabled until a newer version of RStudio is installed.
Looks like I forgot to reinstall Rstudio as well.. That sorted my problem.

ggplot2 library installation in R Studio

New to R Studio and wondering to install new package like ggplot2 (or any other additional packages/libraries), any convenient ways? Thanks.
regards,
Lin
You can do what Heroka said, which is the quickest way, except that you have to know the exact name of the package you want to install.
The other way is to go to the bottom right quadrant and select the "packages" tab and click on "install packages" above the list. This will open a new window where you will select what package you want to install, where on your system and from where. Also, there's auto-completion, so you just have to enter the first letters and select which package you want.
The advantage to doing things in script (library(ggplot2)) is that loading a package can be saved at the top of the script and easily run every time you run a script. Clicking cannot be saved, and must be done manually every time.
Use install.packages(ggplot2) in the console of RStudio or another GUI and then load the package using library(ggplot2) afterwards. If you don't get any errors you're good to go

How to get rid of side effect of tcltk2 package?

After working with the tcltk2 library to create a GUI window in R, when I revert to using a window produced with a package which depends on tcltk (without the 2), I get this error message on Windows 7:
Error in col2rgb(col) : invalid color name 'SystemButtonFace'
It must be that tcltk2 changes some underlying options for working with GUIs in R, because the error message is absent prior to loading tcltk2. I am working with the following version of R:
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Is there a way to deal with this side effect (i.e., remove it)?
I tried to simply detach the tcltk2 package (which is a supplement to the tcltk package), but that doesn't do it.
I also tried replacing the colours in the GUI window that generated the error messages with R colour names like "red" and "blue" (instead of colours like #CCCFFF).
Any help would be greatly appreciated.
Thanks,
Isabella
It is not just an annoyance - it prevents R from properly displaying subsequent GUI windows.
I've put together some example R code. The code uses the PBSmodelling package to create an R GUI window (where the package depends on tcltk but not on tcltk2, both of which are R packages). The GUI window thus created is properly displayed in R. However, after loading the tcltk2 package in R and trying to display the same window again, I get the error message:
Error in col2rgb(col) : invalid color name 'SystemButtonFace'
To give you some context, most of the R GUI windows I've written for my project so far rely on PBSmodelling and work fine. I only need to use tcltk2 to create a couple of windows for the project, but these windows cannot (at least at the moment) be integrated with the majority of the other windows because of the above mentioned error message.
Here is the R code that will reproduce the error message:
## install & require PBSmodelling package
## for creating R GUI windows
install.packages("PBSmodelling")
require(PBSmodelling)
## function to plot a sinusoid (to be called by GUI window)
myPlot <- function() {
getWinVal(scope="L");
x <- seq(0,500)*2*n*pi/500;
plot(x,sin(x),type="l");
}
## create an R GUI window with PBSmodelling
winStr=c( "window title=Simple",
"entry name=n value=5",
"button function=myPlot text=\"Plot sinusoid\"")
## display R GUI window created with PBSmodelling
createWin(winStr,astext=TRUE)
## install & require tcltk2 package
install.packages("tcltk2")
require(tcltk2)
## try to display R GUI window created with PBSmodelling,
## this time after loading the tcltk2 package in R
createWin(winStr,astext=TRUE)
## Error message:
## Error in col2rgb(col) : invalid color name 'SystemButtonFace'
If you have any ideas for resolving this issue, please let me know.
Many thanks,
Isabella

Disable GUI, graphics devices in 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")

Resources