Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make R's View() open in a new window automatically

Tags:

I like to use the Source Editor / data viewer invoked by View() in R. I use multiple monitors and it's really nice to have a viewer or two open on a side monitor while I code in the main RStudio window. It's a bit inconvenient to have to do View(df) and then click the "show in new window" button for each dataframe I want to view if I want to view several dataframes.

I'm wondering if there's some kind of wrapper I could put together, or maybe some setting tucked away somewhere, that can make it so when I invoke View() the viewer opens in a new window automatically. Any ideas?

like image 831
lost Avatar asked Mar 02 '18 04:03

lost


2 Answers

You could consider overwriting the viewer options.

options(viewer = function(url, height = NULL)
{
  if (!is.character(url) || (length(url) != 1))
    stop("url must be a single element character vector.", call. = FALSE)

  if (identical(height, "maximize"))
    height <- -1

  if (!is.null(height) && (!is.numeric(height) || (length(height) != 1)))
    stop("height must be a single element numeric vector or 'maximize'.", call. = FALSE)

  invisible(.Call("rs_showPageViewer", url, title = "RStudio", self_contained = FALSE))  
})

Explanation:

The code of viewer options can be found here: https://github.com/rstudio/rstudio/blob/master/src/cpp/r/R/Options.R.

Your desired functionality (open in new window) is the page_viewer, see here: https://github.com/rstudio/rstudio/blob/779baf9ceb99b6d2455345bcbae3c4e57e164425/src/cpp/r/R/Options.R#L45

The current default behaviour is to open viewer not page_viewer. The code of the viewer option is here https://github.com/rstudio/rstudio/blob/779baf9ceb99b6d2455345bcbae3c4e57e164425/src/cpp/r/R/Options.R#L28.

It is a bit hacky, but you could overwrite the viewer option and let it open a new window instead of displaying the content in the viewer pane, see my code snippet above.

Integrate it in your workflow:

(Note that running the code above will only yield the desired functionality during the current session. Running it each time upon starting a new session would be too much effort).

  1. If you are sure you never want to use the viewer pane again, you could consider using the code above and place it in your .RProfile Locate the ".Rprofile" file generating default options. I did not find out yet how to do this as "rs_showPageViewer" is not a method of the base namespace (?). Not sure how to reference the method,... [could make an edit, if you prefer this option].

  2. Write a small addin. Downside that it is kind of an overkill to introduce an extra addin for this. Upside would be that you can change in between both options (window vs. pane) during one session via click / keyboard shortcut.

Addin:

I uploaded it on my github: https://github.com/Timag/viewerWindow.

Install per devtools::install_github('Timag/viewerWindow').

And then select the addin

  • ViewerWindow: Open all viewer in new window from now on.

  • ViewerPane: Open all viewer in new pane from now on.

or assign keyboard shortcuts to it.

enter image description here

Edit 11.2019:

(this little hack might not work anymore - see https://github.com/Timag/viewerWindow/issues/1)

like image 106
Tonio Liebrand Avatar answered Sep 29 '22 15:09

Tonio Liebrand


This opens data frame in a new window right away:

edit(df) 
like image 44
PsychometStats Avatar answered Sep 29 '22 13:09

PsychometStats