opened
vnijs
username_0
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).
R Session Watcher: Rmarkdown open HTML in webview
issue
created
andycraig
username_1
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`?
comment
created
vnijs
username_0
"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
comment
created
andycraig
username_1
Excellent!

One way of doing the selection preview this 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 a 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.
comment
created
vnijs
username_0
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.
comment
created
andycraig
username_1
@username_0 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!
comment
created
vnijs
username_0
[1] "<pre><code class=\"r\">print(x)\n</code></pre>\n\n<pre><code>## [1] 5\n</code></pre>\n"
```
comment
created
andycraig
username_1
@username_0 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.
comment
created
vnijs
username_0
Having `knitr` functionality as a starting point would be great, I agree.
comment
created
andycraig
username_1
@username_0 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
comment
created
vnijs
username_0
Thanks @username_1. 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 = \"$$\")"
    }
comment
created
andycraig
username_1
I’m not sure. @username_2 Any ideas?
comment
created
renkun-ken
username_2
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.
comment
created
vnijs
username_0
Would that mean putting the file in a specific location/folder that would map to a url?
comment
created
assignUser
username_3
@username_2 Shouldn't this be now possible (as long as the html is within the users workspace) with [Webview.asWebviewUri](https://code.visualstudio.com/api/extension-guides/webview#loading-local-content)?
comment
created
ElianHugh
username_4
We could probably support this by tweaking the new rmarkdown preview from #692
comment
created
ElianHugh
username_4
I've piggybacked on the rmarkdown preview function in order to preview selections: 
![image](https://user-images.githubusercontent.com/60372411/129665336-b1644f4e-5ed9-419a-8f65-c53360dbc64d.png)

However, as can be seen in the terminal, it relies on saving the workspace to a .RData file for loading in the child process. I'm not super happy with having the user's terminal spammed like that, but I'm not sure of a way to avoid it...
comment