23

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?

Scarabee
  • 5,437
  • 5
  • 29
  • 55
lost
  • 1,483
  • 1
  • 11
  • 19
  • `utils::View(...)`, using the original function, not RStudio's overriding function. – r2evans Mar 02 '18 at 04:55
  • I much prefer RStudio's version, mainly because it updates as you run code. – lost Mar 02 '18 at 05:07
  • 2
    I don't think you're going to find much. RStudio hasn't put effort (yet?) into allowing user R code to directly modify the IDE behavior. It's an interesting idea, similar to emacs and elisp (though RStudio doesn't run *on* R), but not one I see on their immediate priority list. – r2evans Mar 02 '18 at 05:14
  • 1
    Thanks. Do you know of any packages that would offer similar viewer functionality but also allow control through R code? – lost Mar 02 '18 at 06:50
  • I have nothing to suggest. – r2evans Mar 02 '18 at 07:49
  • 4
    This is a great feature request. If you make it, please link it here, I'll be happy to back it up. – emilBeBri Mar 22 '18 at 10:11
  • I'd support this request also. I loose time Viewing + clickin on "Show in new window". – alvaropr Oct 17 '18 at 08:43
  • The View behavior is just like opening up another R script or tab, so you can just click on the view tab and drag to the screen you want to see it on, IMO this is the best it's gonna get really, since even if it popped out automatically you'd still have to tell it where to pop out via some manual conditioning for each window – kana May 03 '19 at 10:29
  • would you prefer to "always open in new window" and dont use the viewer pane anymore or switching in between both options within a session? Both options would require some effort, but the latter one is possible for sure, see my answer below. Its hacky for sure, but could be of interest,... – Tonio Liebrand May 03 '19 at 14:24
  • please add your +1 to this request on rstudio forum https://community.rstudio.com/t/native-support-for-multi-monitor-wide-spectrum-of-features-state-your-wishes-here/49619 – userJT Jan 16 '20 at 17:44

2 Answers2

13

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)

Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • couldn't get it to work earlier, will try again now – MattV May 10 '19 at 18:48
  • installed and selected 'viewerWindow', still opening in a Pane. Any idea on debugging this? – MattV May 10 '19 at 18:51
  • That's odd. Tested on Windows and Linux. Which OS r u using? Do u get any error messages ? – Tonio Liebrand May 11 '19 at 00:14
  • @TonioLiebrand Hi, I found that this R Viewer overwriting may be related with my issue here, can you help me give some points? https://stackoverflow.com/q/61463631/8848227, thank you :) – Jovan Apr 28 '20 at 06:26
  • ad hoc i would say, it is more of a request for highcharter. Sthg along: https://www.highcharts.com/forum/viewtopic.php?t=35149,...as it will be html and the html will be displayed in rstudio. But i am not sure, i will give it some thought,... – Tonio Liebrand Apr 28 '20 at 08:01
  • 1
    How to have a shortcut for viewing data in console or in new window as desired? – Alvaro Morales Apr 18 '21 at 02:16
  • Both solutions do not work – Julien Jul 21 '22 at 08:29
8

This opens data frame in a new window right away:

edit(df) 
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
PsychometStats
  • 340
  • 1
  • 7
  • 19