【发布时间】:2016-01-23 14:42:32
【问题描述】:
考虑如果y = TRUE 和浏览器中y = FALSE 的任一功能(对于rstudio)将在查看器中打开某些内容。您可以通过options(viewer = NULL) 强制whatever 在浏览器中打开(然后您需要重置为之前),但我无法使用普通的on.exit 方法使其在函数内部工作。在 windows 和 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,并以退出时的方式返回我的选项,以便我可以控制要查看结果的位置。
或者对于 html 和小部件有更好的方法吗?我试过DT::datatable 中的options 参数无济于事,但这对htmlTable::htmlTable 的情况没有帮助。
我能想到的唯一其他方法是将所有代码写入临时文件并使用if (rstudio) rstudio::viewer(tempfile) else browseURL(tempfile),我认为这对于看似如此简单的事情来说是很多工作。
【问题讨论】:
-
如果有人可以查看我的编辑历史并告诉我差异如何知道我以两种不同的方式删除了相同的字符,则加分
-
我向 Ushey 发送了“蝙蝠信号”。希望他在西雅图看云。