强制 rstudio 使用浏览器而不是查看器
Force rstudio to use browser instead of viewer
考虑其中一个函数(对于 rstudio)如果 y = TRUE
会在查看器中打开一些东西,如果 y = FALSE
会在浏览器中打开一些东西。您可以通过 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
并在退出时使用 return 我的选项,这样我就可以控制我想在哪里查看结果。
或者对于 html 和小部件是否有更好的方法?我已经尝试了 DT::datatable
中的 options
参数但无济于事,但这对 htmlTable::htmlTable
案例没有帮助。
我能想到的唯一其他方法是将所有代码写入一个临时文件并使用 if (rstudio) rstudio::viewer(tempfile) else browseURL(tempfile)
,我认为这对于看起来如此直接的事情来说需要做很多工作。
虽然这不是修复,但我认为它说明了正在发生的事情。尝试在 on.exit()
处理程序中添加 Sys.sleep()
调用:
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 不会'decide'如何处理 DT::datatable()
调用的结果,直到 on.exit()
处理程序已完成执行。这意味着,当 RStudio 想要弄清楚如何处理结果时,查看器已经恢复!奇怪的是,RStudio 等到 R 不再 'busy' 来决定如何显示结果内容,到那时临时更改 viewer
选项已经太迟了。
请注意,这并不能解释 htmlTable
行为。我最好的猜测是存在某种竞争条件;丢失的 viewer
选项似乎随着战略性放置的 Sys.sleep()
调用而消失...
不幸的是,解决这个问题意味着避免使用 on.exit()
调用——当然,除非我们能想办法在 RStudio 中处理这个问题。
您可以通过将代码写入临时文件并使用 browseURL
或任何您喜欢的方式来获得此功能。
f
和 g
的要点是相同的,因此我想您可以使用一个函数来处理任何类型的 html 代码或小部件。可能小部件需要 selfcontained = TRUE
.
f <- function(x, y = TRUE) {
x <- if ((inherits(x, 'iplot'))) x else DT::datatable(x)
if (!y) {
htmlFile <- tempfile(fileext = '.html')
htmlwidgets::saveWidget(x, htmlFile, selfcontained = TRUE)
utils::browseURL(htmlFile)
} else x
}
g <- function(x, y = TRUE) {
x <- htmlTable::htmlTable(x)
if (!y) {
htmlFile <- tempfile(fileext = '.html')
writeLines(x, con = htmlFile)
utils::browseURL(htmlFile)
} else x
}
## opens in viewer
g(mtcars)
## opens in browser
g(mtcars, FALSE)
## same for widgets
f(mtcars)
f(mtcars, FALSE)
f(qtlcharts::iplot(1:5, 1:5), FALSE)
## and my options haven't changed
getOption('viewer')
# function (url, height = NULL)
# ...
请注意,这实际上是让 htmlTable::htmlTable
使用不同查看器的正确方法,但 g
应该适用于任何 html。
library('htmlTable')
print(htmlTable(mtcars), useViewer = utils::browseURL)
考虑其中一个函数(对于 rstudio)如果 y = TRUE
会在查看器中打开一些东西,如果 y = FALSE
会在浏览器中打开一些东西。您可以通过 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
并在退出时使用 return 我的选项,这样我就可以控制我想在哪里查看结果。
或者对于 html 和小部件是否有更好的方法?我已经尝试了 DT::datatable
中的 options
参数但无济于事,但这对 htmlTable::htmlTable
案例没有帮助。
我能想到的唯一其他方法是将所有代码写入一个临时文件并使用 if (rstudio) rstudio::viewer(tempfile) else browseURL(tempfile)
,我认为这对于看起来如此直接的事情来说需要做很多工作。
虽然这不是修复,但我认为它说明了正在发生的事情。尝试在 on.exit()
处理程序中添加 Sys.sleep()
调用:
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 不会'decide'如何处理 DT::datatable()
调用的结果,直到 on.exit()
处理程序已完成执行。这意味着,当 RStudio 想要弄清楚如何处理结果时,查看器已经恢复!奇怪的是,RStudio 等到 R 不再 'busy' 来决定如何显示结果内容,到那时临时更改 viewer
选项已经太迟了。
请注意,这并不能解释 htmlTable
行为。我最好的猜测是存在某种竞争条件;丢失的 viewer
选项似乎随着战略性放置的 Sys.sleep()
调用而消失...
不幸的是,解决这个问题意味着避免使用 on.exit()
调用——当然,除非我们能想办法在 RStudio 中处理这个问题。
您可以通过将代码写入临时文件并使用 browseURL
或任何您喜欢的方式来获得此功能。
f
和 g
的要点是相同的,因此我想您可以使用一个函数来处理任何类型的 html 代码或小部件。可能小部件需要 selfcontained = TRUE
.
f <- function(x, y = TRUE) {
x <- if ((inherits(x, 'iplot'))) x else DT::datatable(x)
if (!y) {
htmlFile <- tempfile(fileext = '.html')
htmlwidgets::saveWidget(x, htmlFile, selfcontained = TRUE)
utils::browseURL(htmlFile)
} else x
}
g <- function(x, y = TRUE) {
x <- htmlTable::htmlTable(x)
if (!y) {
htmlFile <- tempfile(fileext = '.html')
writeLines(x, con = htmlFile)
utils::browseURL(htmlFile)
} else x
}
## opens in viewer
g(mtcars)
## opens in browser
g(mtcars, FALSE)
## same for widgets
f(mtcars)
f(mtcars, FALSE)
f(qtlcharts::iplot(1:5, 1:5), FALSE)
## and my options haven't changed
getOption('viewer')
# function (url, height = NULL)
# ...
请注意,这实际上是让 htmlTable::htmlTable
使用不同查看器的正确方法,但 g
应该适用于任何 html。
library('htmlTable')
print(htmlTable(mtcars), useViewer = utils::browseURL)