Search code examples
rkableextra

General way to display or render html in RStudio (just as though it were in a browser)?


Suppose we have some HTML, obviously we could write it to a file and open it in a browser, but is there a simpler way to render it inside RStudio?

my_html <- "<h1>Here's some HTML</h1>
<p>Here's a paragraph</p>
</br>
</br>"

render(my_html)

I would hope a function like render(my_html) (or similar) could accept the HTML string and display it? But I cannot find any such function.

Although it should be possible since packages like kableExtra render HTML for tables


Solution

  • Try

    dir <- tempfile()
    dir.create(dir)
    htmlFile <- file.path(dir, "index.html")
    writeLines("<h1>Here's some HTML</h1>
    <p>Here's a paragraph</p>
    </br>
    </br>", con = htmlFile)
    # (code to write some content to the file)
    rstudioapi::viewer(htmlFile)
    
    
    

    Also see ?viewer