Forzar a rstudio a usar el navegador en lugar del visor
Considere cualquiera de las funciones que (para rstudio) abrirán algo en el visor siy = TRUE
y en tu navegador siy = FALSE
. Puedes forzar elwhatever
para abrir en su navegador a través deoptions(viewer = NULL)
(y luego necesita restablecerlo antes), pero no puedo hacer que esto funcione dentro de las funciones usando el normalon.exit
acercarse a, aproximarse. Probado en windows y 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
Parece que el espectador no respeta mis opciones dentro del entorno de la función con solo un poco de html (g
) o un widget (f
) Pensé que ambos usaríanviewer = NULL
dentro de la función y devolver mis opciones como estaban al salir para que pueda controlar dónde quiero ver el resultado.
¿O hay una mejor manera de hacerlo tanto para html como para widgets? He probado eloptions
argumento enDT::datatable
fue en vano, pero esto no ayudaría para elhtmlTable::htmlTable
caso.
El único otro enfoque que puedo pensar es escribir todo el código en un archivo temporal y usarif (rstudio) rstudio::viewer(tempfile) else browseURL(tempfile)
lo cual creo que es mucho trabajo para algo aparentemente tan directo.