Saturday, October 27, 2012

Viewing large dataframes in RStudio


UPDATE: March 2015.
Well here is a lot easier way. utils::View brings up the good old unlimited viewer in a separate window.

I am doing more of my work in RSudio. It has some nice features.
But for large dataframes, the View function is limited to about 1000 rows and 100 columns.
In the standard R Console, View handles large dataframes well.
For when I do want to look at the whole dataframe in RStudio, I wrote this function to write the dataframe to a temporary .csv file and open it with the default application – for me Excel2010, which handles plenty of rows and columns.
myView <- function(dframe) {
    # RStudio does not have a good viewer for large data frames.  This
    # function writes a dataframe to a temporary .csv and then opens it,
    # presumably in excel (if that is the file association).
    csvName <- paste0(tempdir(), "\\myView-", substitute(dframe),
         format(Sys.time(), "%H%M%S"), ".csv")
    write.csv(dframe, file = csvName)
    shell.exec(csvName)
}
It is slower to open the file in Excel, but sometimes worth the wait to view the whole thing.

Note that shell.exec is Windows only, so this needs to be modified for other systems.