Vscode-r: Show produced HTML pages in webview

Created on 2 Nov 2019  路  11Comments  路  Source: Ikuyadeu/vscode-R

Some R functions may produce HTML output (e.g. htmlwidgets, interactive plots, reprex, profvis, covr report, rmarkdown document). In RStudio, if an html output is produced, the Viewer pane will show the web page. In plain terminal or in VSCode terminal, the html file will be open in the web browser.

However, this does not seem to work with Remote Development. For example,

DT::datatable(mtcars)
> Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/run-mailcap line 528.
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
Warning: program returned non-zero exit code #1
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening '/tmp/RtmpQ8hZEA/viewhtml13bb47e17a2a2/index.html'

If vscode-R could capture the produced HTML file and present it in a new tab of WebView, it would be very useful.

All 11 comments

Each time the browser is triggered, the following command is called:

$BROWSER $TMPFILE

where TMPFILE is the path of the html file.

To trigger a webview in VSCode, we can override BROWSER with a shell script that tells vscode-R that a html file is produced and a webview should be created for that.

More specifically, I think we could use the approach suggested at https://github.com/Ikuyadeu/vscode-R/issues/51 so that a wide range of scenarios are supported (including Remote Development, user started R sessions in any screen or tmux window, etc.)

@renkun-ken This would be a fantastic addition to vscode-R! If it's possible to use options to check whether this feature is available, it would help developers integrate support for it into their R projects.

RStudio's IDE uses options to set a viewer function, so that getOption("viewer") is NULL by default when using standalone R, but returns a function that eventually calls rs_viewer if you're using RStudio.

If setting an option makes sense here, it might be good to call it vsviewer or webview/WebView to avoid breaking code that relies on getOption("viewer") being NULL when code is not executed from within an RStudio session.

@rpkyle I'm not sure if there's a built-in option to control the viewer but ultimately it has to set environment variable BROWSER to take effect.

If you create a bash script like the following:

#! /bin/bash
echo $1 >> $HOME/browser.log

Then store it in your PATH, named e.g. browser, then

Sys.setenv(BROWSER = "browser")

And then running DT::datatable(mtcars) will append the tmp html file path in ~/browser.log.

If an R option of viewer function is defined, since it is not built-in, some user code must take action to put the function into a script so that $BROWSER $TMPFILE will call the function and trigger the viewer to show in the IDE.

@renkun-ken It would probably make things a bit easier for novice users if this were controlled by an option within the VSCode-R settings and set when R is launched. From inside R, it鈥檚 possible to use Sys.setenv, but a developer would likely still need some way to know that their code is executing with R from a VSCode session.

Just thinking aloud; however it鈥檚 implemented in the end, will be a great feature for users. Happy to help test if I can.

BROWSER environment variable only works at remote. I'm not sure how we can control the browser behavior in local.

Just re-read the code on how browser is triggered. Yes, @rpkyle, htmlwidgets:::print.htmlwidget defines the behavior using getOption("viewer") as the viewer function. Only if viewer options is not defined, the browser will be triggered with utils::browseURL.

This makes it eaiser to work with. We just need a way to tell vscode-R the URL just produced.

https://github.com/renkun-ken/vscode-R/commit/170524fea221e7c28ae6526458f2c31d4f790757 is a first implementation of showing htmlwidgets in WebView based on the WIP of @andycraig.

Not sure if this fits here but ... would it be possible to have Rmarkdown (knitr) output shown in a webview? The user could then could select, for example, a chunk of text + math + R code and have it rendered to HTML by Rmarkdown and opened in a webview in VSCode.

I have some R-code in a shiny app that I'd be happy to contribute if that might be useful but I'd need help with the interaction with VSCode.

@vnijs That鈥檚 a good idea! Could you create a new issue for it? Thank you!

@andycraig Seems like @renkun-ken already has something in play for this: https://github.com/Ikuyadeu/vscode-R/issues/103#issuecomment-567054476

I added some clarification about what I'm hoping for here: https://github.com/Ikuyadeu/vscode-R/issues/103#issuecomment-567212113

Close this as we have #150 merged.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LePeti picture LePeti  路  4Comments

andycraig picture andycraig  路  4Comments

awellis picture awellis  路  5Comments

ShortArrow picture ShortArrow  路  4Comments

gcambray picture gcambray  路  4Comments