Vscode-r: R Session Watcher: Rmarkdown open HTML in webview

Created on 21 Dec 2019  路  14Comments  路  Source: Ikuyadeu/vscode-R

Follow up on https://github.com/Ikuyadeu/vscode-R/issues/103#issuecomment-567054476

rmarkdown::run(file) works great but I don't think there is a vscode-R command for it yet. Also, a keyboard shortcut would be nice.

It would also be convenient to be able to render selected chunks of text + math + code and see them in a web viewer (i.e., if a section of a document is selected the rmarkdown::run processes only processes that part).

feature-request

All 14 comments

@vnijs

rmarkdown::run(file) works great but I don't think there is a vscode-R command for it yet. Also, a keyboard shortcut would be nice.

This by itself is easy, but if it needs to take into account the currently selected text then it would require some more work.

It would also be convenient to be able to render selected chunks of text + math + code and see them in a web viewer (i.e., if a section of a document is selected the rmarkdown::run processes only processes that part).

For variables defined above the section that has been selected, would their values be taken from the current R session, or by running all the above code but only displaying the selected part? For example, imagine this R markdown file:

```
x <- 1
```

Select from here...
```
x
```
...to here

If the current R session had x set to 2, would the rendering show x as 1 or 2?

"would their values be taken from the current R session". Yes. You can have Knitr / Rmarkdown run in a specified environment (e.g., Global).

There is already a keyboard short cut for running the whole file, although it doesn't open the html in preview. You can run also R-code from a code-chunk. However, you can't (yet) select a part of (large) Rmd document and preview that. I think that would be a great addition

@vnijs

You can have Knitr / Rmarkdown run in a specified environment (e.g., Global).

Excellent!

One way of doing the selection preview would be to have vscode-R create a temporary Rmd file using the current file's YAML header, and just the selected text, and then render that using the Global environment.

My understanding is that the goal here is to have the whole file re-render when it is saved, and if some text is selected (without any save) then just that section is rendered. If that's correct, then I'm not sure we'd be able to use rmarkdown::run as that wouldn't be able to handle the selection bit. But we should be able to trigger a rmarkdown::render whenever the file is saved, and create and render a temporary file whenever a selection is made.

I really like R markdown but there's a lot about it I don't know, so if you have a simpler approach to what I've described above please let me know! Also let me know if I'm describing something a bit different from the feature you want.

That sounds great. knitr has the option to us text directly which is easy for a selection. `rmarkdown requires a (temp) file but is also more general. I haven't use rmarkdown::run but updating on each save seem like it could be rather computationally expensive. Happy to help with the R-part of this if I can.

@vnijs I had a quick try with the text argument of knitr::knit:

knitr::knit(text = "```\nx <- 1\n```")

and it returned the string "```\nx <- 1\n```", so I'm presumably not using it right. If you know what the correct way to handle a text argument is in order to get a rendered document could you let me know? Thank you!

You were missing the {r}. Below two examples. FYI knit2html returns its output invisibly. The first option return a fully html page. The second returns just a "fragment" that you can embed in an html template that you provide.

html <- knitr::knit2html(text = "```{r}\nx <- 1\nprint(x)\n```")
html <- knitr::knit2html(text = "```{r}\nx <- 1\nprint(x)\n```", fragment.only = TRUE, stylesheet = "")

An example of how this could work with the global environment is shown below.

x <- 5
html <- knitr::knit2html(text = "```{r}\nprint(x)\n```", envir = .GlobalEnv, fragment.only = TRUE)
> html                                                  
[1] "<pre><code class=\"r\">print(x)\n</code></pre>\n\n<pre><code>## [1] 5\n</code></pre>\n"

@vnijs Whoops, that was a stupid omission on my part! Thank you for those examples, they will make a very good reference.

vscode-R has a function runSelectionOrWord which wraps the selected with specified strings and opening and closing parentheses, and then sends that to the console. This could almost be used to pass the selected text into the knitr::knit2html call you've provided. It won't quite work as-is because it can't wrap the selected text in quote marks (although I suppose something could be done with Non-Standard Evaluation ...) The option to surround text in quote marks could be added very easily.

The advantages of that approach would be that it can be done without much modification to vscode-R, and it wouldn't need to use temp files. The disadvantages would be that it would need to send all the selected text to the console, and that the user would need to perform a specific command to render the selection rather than having it render automatically whenever a selection is made. Since it would be easier to implement, I'm tempted to try that to start with.

Having knitr functionality as a starting point would be great, I agree.

@vnijs I haven't tried it yet but rendering the selection should now be possible using the knit2html code you provided earlier and the runCommandWithSelectionOrWord command. Example of how to use runCommandWithSelectionOrWord here: https://github.com/Ikuyadeu/vscode-R#creating-keybindings-for-r-commands

Thanks @andycraig. I think this could work (see below). Is there a convenient way to have the result from args shown in a vscode webview?

{
    "description": "knit to html",
    "key": "ctrl+i",
    "command": "r.runCommandWithSelectionOrWord",
    "when": "editorTextFocus",
    "args": "knitr::knit2html(text = \"$$\")"
}

Is there a convenient way to have the result from args shown in a vscode webview?

I鈥檓 not sure. @renkun-ken Any ideas?

If the output is a plain html file, then I don't know a simple way to show it in webview since it requires an HTTP server to provide a URL like the R html help.

Would that mean putting the file in a specific location/folder that would map to a url?

@renkun-ken Shouldn't this be now possible (as long as the html is within the users workspace) with Webview.asWebviewUri? I will try to test it but I am very new to vsc extension development ^-^

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JimmyZJX picture JimmyZJX  路  3Comments

renkun-ken picture renkun-ken  路  3Comments

michaelHL picture michaelHL  路  3Comments

Victsz picture Victsz  路  5Comments

andycraig picture andycraig  路  4Comments