使 R 的 View() 自动在新窗口中打开 [英] Make R's View() open in a new window automatically

查看:36
本文介绍了使 R 的 View() 自动在新窗口中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在 R 中使用由 View() 调用的源代码编辑器/数据查看器.我使用多个监视器,当我在 RStudio 主窗口中编码时,在侧监视器上打开一两个查看器真的很棒.如果要查看多个数据帧,必须先执行 View(df) 然后单击要查看的每个数据帧的在新窗口中显示"按钮,这有点不方便.

我想知道是否有某种包装我可以放在一起,或者某些设置隐藏在某处,这样当我调用 View() 时,查看器会自动在新窗口中打开.有什么想法吗?

解决方案

您可以考虑覆盖查看器选项.

options(viewer = function(url, height = NULL){if (!is.character(url) || (length(url) != 1))stop("url 必须是单元素字符向量.", call. = FALSE)如果(相同(高度,最大化"))高度 <- -1if (!is.null(height) && (!is.numeric(height) || (length(height) != 1)))stop("高度必须是单元素数值向量或'最大化'.", call. = FALSE)invisible(.Call("rs_showPageViewer", url, title = "RStudio", self_contained = FALSE))})

说明:

查看器选项的代码可以在这里找到:

编辑 11.2019:

(这个小技巧可能不再适用 - 请参阅 https://github.com/Timag/viewerWindow/issues/1)

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?

解决方案

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.

Edit 11.2019:

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

这篇关于使 R 的 View() 自动在新窗口中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆