强制 rstudio 使用浏览器而不是查看器 [英] Force rstudio to use browser instead of viewer

查看:78
本文介绍了强制 rstudio 使用浏览器而不是查看器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个函数(对于 rstudio),如果 y = TRUE 会在查看器中打开一些东西,如果 y = FALSE 会在浏览器中打开一些东西.您可以通过 options(viewer = NULL) 强制 whatever 在浏览器中打开(然后您需要重置为之前),但我无法做到这一点使用正常的 on.exit 方法在函数内部工作.在 windows 和 osx 上测试过.

Consider either function which (for rstudio) will open something in the viewer if y = TRUE and in your browser if y = FALSE. You can force the whatever to open in your browser via options(viewer = NULL) (and then you need to reset to before), but I can't get this to work inside functions using the normal on.exit approach. Tested on windows and osx.

f <- function(x, y = TRUE) {
  if (y) {
    oo <- getOption('viewer')
    on.exit(options(viewer = oo))
    options(viewer = NULL)
  } else options(viewer = NULL)
  print(getOption('viewer'))
  DT::datatable(x)
}

g <- function(x, y = TRUE) {
  if (y) {
    oo <- getOption('viewer')
    on.exit(options(viewer = oo))
    options(viewer = NULL)
  } else options(viewer = NULL)
  print(getOption('viewer'))
  htmlTable::htmlTable(x)
}

## in rstudio, returns the viewer function
getOption('viewer')
# function (url, height = NULL) 
# ...

## opens in viewer despite `options(viewer = NULL)`
g(mtcars)
# NULL

## again returns the function, ie, reset my options to before g call successfully
getOption('viewer')
# function (url, height = NULL) 
# ...

## opens in browser but leaves `options(viewer = NULL)` after exiting
g(mtcars, FALSE)
# NULL

getOption('viewer')
# NULL

似乎查看器不尊重我在函数环境中的选择,仅使用一些 html (g) 或小部件 (f).我认为两者都会在函数内使用 viewer = NULL 并以退出时的方式返回我的选项,以便我可以控制我想要查看结果的位置.

It seems like the viewer isn't respecting my options within the function environment with either just some html (g) or a widget (f). I thought both would use viewer = NULL inside the function and return my options the way they were upon exiting so that I can control where I want to view the result.

或者对于 html 和小部件有更好的方法吗?我已经尝试了 DT::datatable 中的 options 参数无济于事,但这对 htmlTable::htmlTable 情况没有帮助.

Or is there a better way of doing this for both html and widgets? I have tried the options argument in DT::datatable to no avail, but this wouldn't help for the htmlTable::htmlTable case.

我能想到的唯一其他方法是将所有代码写入临时文件并使用 if (rstudio) rstudio::viewer(tempfile) else browseURL(tempfile) 我认为是为看似如此简单的事情做了很多工作.

The only other approach I can think of is to write all the code to a temp file and use if (rstudio) rstudio::viewer(tempfile) else browseURL(tempfile) which I think is a lot of work for something seemingly so straight-forward.

推荐答案

虽然这不是解决方案,但我认为它说明了正在发生的事情.尝试在 on.exit() 处理程序中添加一个 Sys.sleep() 调用:

Although this isn't a fix, I think it illustrates what's going on. Try adding a Sys.sleep() call in the on.exit() handler:

f <- function(x) {
  viewer <- getOption("viewer")
  on.exit({
    print("Restoring viewer...")
    Sys.sleep(3)
    options(viewer = viewer)
  }, add = TRUE)
  options(viewer = NULL)
  DT::datatable(x)
}

## opens in viewer despite `options(viewer = NULL)`
f(mtcars)

您会注意到 RStudio 不会决定"如何处理 DT::datatable() 调用的结果,直到 after on.exit() 处理程序已完成执行.这意味着,当 RStudio 想要弄清楚如何处理结果时,查看器已经恢复了!奇怪的是,RStudio 会等到 R 不再忙"来决定如何显示结果内容,然后临时更改 viewer 选项为时已晚.

You'll notice that RStudio doesn't 'decide' what to do with the result of DT::datatable() call until after the on.exit() handler has finished execution. This means that, by the time RStudio wants to figure out to do with the result, the viewer has already been restored! Odds are, RStudio waits until R is no longer 'busy' to decide how to display the resulting content, and by then is too late for temporary changes to the viewer option.

请注意,这并不能解释 htmlTable 的行为.我最好的猜测是存在某种竞争条件;丢失的 viewer 选项似乎随着战略性放置的 Sys.sleep() 调用而消失...

Note that this doesn't explain the htmlTable behaviour. My best guess is that there is some kind of race condition going on; the lost viewer option seems to go away with strategically placed Sys.sleep() calls...

不幸的是,解决这个问题意味着避免使用 on.exit() 调用——当然,除非我们能在 RStudio 中解决这个问题.

Unfortunately, working around this means avoiding the use of on.exit() call -- unless we can figure out to handle this in RStudio, of course.

这篇关于强制 rstudio 使用浏览器而不是查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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