View command in Eclipse/statET - r

R has a useful command ("View") that lets one see a table like a spreadsheet in a pop-up window. I used this command in the eclipse console while running statET and the spreadsheet popped up for a few milliseconds and then vanished.
To be specific, I have a matrix called "mat" and I typed
View(mat)
Does anyone know how to fix this?

In the Object Browser window in Eclipse, you should see a list of the R objects you have in memory (under .GlobalEnv), as well as packages you've loaded (like reshape2 or plyr).
If you right-click on an R object in the .GlobalEnv list, you'll see an option to Open in Data Viewer. Clicking this option opens the data in a spreadsheet format in the Editor window, which is useful for getting a quick view of a small data set, or a subset of a larger one.

Related

How to open the viewer pane in browser?

Just a simple question that I couldn't find an answer. I just updated my R Studio and R, and one function that I used a lot was opening the results in Viewer pane in my browser.
For example, with the resulting tables from packages like sjPlot or expss, I used the "Show in new window" to visualize the tables in Chrome or other default browser. Don't know if this was set by a old package, but I can't get this result right now. Actually, when I click this option, nothing is happening.
When I change to my old R version 3.6.2, I can use the "show in new window", but can't make to work in the version 4.0.1.
Anyone would know how can I get this function again?
Thanks
options(viewr=NULL) should do the trick.
In my case, i use browseURL instead of rstudioapi::viewer open file in browser directly.
browseURL(sprintf("%s/test.html", tempdir()))
if your file not in tempdir(), you should copy it to tempdir() first:
system(sprintf("cp %s %s", test_file, tempdir()))

R studio List and Dataframes auto name completion not working anymore

Suddenly R Studio stopped auto completing my list and data frame contents when entering mylist$ then pressing Tab. The result message I get is
No matches
I tried that on very simple lists or data frames like:
simpledataset <- data.frame(a=rep(0,100),b=rep(1,100))
However, when I load the dataset Mtcars, it correctly shows the column names.
I reset %localappdata%\RStudio-Desktop by renaming it, but didn't change anything.
Any idea of what could be the cause of that?
This sounds like a bug in the autocompletion system that is occasionally triggered when certain packages are loaded, or the byte-compiler optimization setting is increased. You might try checking a few things:
What is the value of compiler::getCompilerOption("optimize")? Autocompletion is known to fail when this value is equal to 3 in the current RStudio release (v0.99.896). You can try running compiler::setCompilerOptions(optimize = 2) to resolve this.
What packages do you have loaded? You can try loading packages one-by-one to see which affects RStudio's ability to provide autocompletions.
Finally, there should be a fix for this in the preview version of RStudio -- you can try it out at https://www.rstudio.com/products/rstudio/download/preview/.
Go to Tools>Global Options... and set autocomplete there.

R View() does not display all columns of data frame

I have been adding columns to a data frame and using View() to check that it did what I expected. I have repeated lines of code along the lines of:
x$p <- 3 * x$a
x$q <- sqrt(x$b + x$c)
View(x)
This worked fine until the number of columns exceeded 100 (there are 47,000 rows). When I added another two columns, dim(x) shows 102 columns, names(x) shows 102 names, summary(x) shows summaries of all the expected columns. However, View(x) only displays the first 100 columns and doesn't display the last two added columns.
If I try View(x[,-(1:10)]) the most recently added columns are displayed.
I can't see any mention in the View documentation of a limit on the number of columns. Can anyone explain what is happening here?
(Updated)
You can have View() open in one of the quadrants or in a separate notepad-ish window. It opens in the quadrant where my source code is displayed on my machine at work, and in another window on my machine at home. In the latter case, it displays >1k rows & >100 columns (I just checked).
I'm not sure how you can get this to change permanently, IIRC when I updated RStudio and ran View() the first time, a window popped up and asked me to choose what program I wanted to use to display the file. In one case I selected RStudio, and in the other case, I selected notepad. In both cases, the 'use this program by default from now on' radio button was selected; I have never seen this window since. If you can switch to displaying with notepad, you should be able to get out of this problem. However, short of a permanent change, you can get View() to display your data in a separate window using the code utils::View(). This approach works on my machine at work. Here is what it looks like:
Note that I am running RStudio version 0.97.248 on a Windows 7 machine.
Here is what it looks like on my home machine where it comes up in a new window automatically:
I also see this problem with x <- matrix(1:200,nrow=1); View(x) in RStudio, but not in vanilla R. It is a known limitation and they're working on it. You can contact the devs on their forum to give your feedback (and have done so, I see).
I just ran into this issue also. As suggested by gung above, the utils::View() function is helpful as a workaround for browsing all available columns in a data frame, whereas Rstudio still defaults to only the first 100 available columns when using the View() function.
The workaround is very useful for identifying the column names for creating a subset from an existing data frame. However, it doesn't provide a quick column enumeration that the RStudio View() function allows. It's been a few years since the original post in 2013, but this limitation in the RStudio environment seems to still be effective in present-day 2017.
I found a solution that worked for me in a closed RStudio Github issue. You can change the maximum number of columns displayed (say to 1000) using the following command:
rstudioapi::writeRStudioPreference("data_viewer_max_columns", 1000L)
You should be able to run this just once, and it will thereafter be saved to your settings file as the new default in every subsequent R session. Under my Linux system, these preferences are stored in ~/.config/rstudio/rstudio-prefs.json. The relevant line that the above command will add is:
"data_viewer_max_columns":1000
Try fix(). It loads all your columns and rows. The only problem is that it might take long to load large data frames.
I'm not sure if this has been mentioned before but I found this interesting post from 2012:
https://support.rstudio.com/hc/en-us/community/posts/200669267-view-more-than-first-100-columns-.
This indexing allows you to at least check the other columns and if they even exist.
So just use: datafile[row-row, column-column].

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)

PgDn and PgUp Keys in R Graphics Viewer

I've never been able to figure out how to get the PgDn/PgUp keys to work in the R Graphics Viewer.
Even the demo() programs don't seem to support it.
Anyone able to point me to some code that shows how this can be implemented?
Dirk is right.
Another thing to look at are the following functions:
x <- recordPlot()
replayPlot(x)
From the R for Windows FAQ:
The graphics has a history mechanism. As README.R-2.9.2 says:
The History menu allows the recording of plots. When plots have been recorded they can be reviewed by and , saved and replaced. Recording can be turned on automatically (the Recording item on the list) or individual plots can be added (Add or the key). The whole plot history can be saved to or retrieved from an R variable in the global environment. The format of recorded plots may change between R versions. Recorded plots should not be used as a permanent storage format for R plots.
There is only one graphics history shared by all the windows devices.
The R console and graphics windows have configuration files stored in the RHOME\etc directory called Rconsole and Rdevga; you can keep personal copies in your HOME directory. They contain comments which should suffice for you to edit them to your preferences. For more details see ?Rconsole. There is a GUI preferences editor invoked from the Edit menu which can be used to edit the file Rconsole.
As I recall you have to turn 'recording' or 'History' on. This is platform-specific and I am not near a Windows machine.
On a mac, one can use the command key with the back arrow (and forward arrow) to cycle through plots.
Make sure you've plotted at least two plots to the same quartz device, then with the plotting window in focus, hit ⌘ ← to see the previous plot.

Resources