Skip to content

R Session Watcher: Rmarkdown open HTML in webview #155

Closed

Description

@vnijs

Follow up on #103 (comment)

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).

Activity

  1. andycraig commented on Jan 8, 2020

    @andycraig
    Collaborator

    @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?

  2. vnijs commented on Jan 9, 2020

    @vnijs
    Author

    "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

  3. andycraig commented on Jan 9, 2020

    @andycraig
    Collaborator

    @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.

  4. vnijs commented on Jan 9, 2020

    @vnijs
    Author

    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.

  5. andycraig commented on Jan 11, 2020

    @andycraig
    Collaborator

    @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!

  6. vnijs commented on Jan 11, 2020

    @vnijs
    Author

    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"
    
  7. andycraig commented on Jan 11, 2020

    @andycraig
    Collaborator

    @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.

  8. vnijs commented on Jan 11, 2020

    @vnijs
    Author

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

  9. andycraig commented on Apr 16, 2020

    @andycraig
    Collaborator

    @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

  10. vnijs commented on Apr 18, 2020

    @vnijs
    Author

    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 = \"$$\")"
    }
    
  11. andycraig commented on Apr 18, 2020

    @andycraig
    Collaborator

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

    I’m not sure. @renkun-ken Any ideas?

  12. renkun-ken commented on Apr 18, 2020

    @renkun-ken
    Member

    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.

  13. vnijs commented on Apr 28, 2020

    @vnijs
    Author

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

  14. assignUser commented on Aug 13, 2020

    @assignUser
    Contributor

    @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 ^-^

  15. ElianHugh commented on Jul 21, 2021

    @ElianHugh
    Collaborator

    We could probably support this by tweaking the new rmarkdown preview from #692

  16. ElianHugh commented on Aug 17, 2021

    @ElianHugh
    Collaborator

    I've piggybacked on the rmarkdown preview function in order to preview selections:
    image

    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...

  17. renkun-ken commented on Oct 25, 2021

    @renkun-ken
    Member

    Live Preview should be useful to serve HTML in WebView.

  18. github-actions commented on Oct 26, 2022

    @github-actions

    This issue is stale because it has been open for 365 days with no activity.

  19. github-actions commented on Nov 10, 2022

    @github-actions

    This issue was closed because it has been inactive for 14 days since being marked as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions