2.1 Writing HTML tags from R

  • Install the htmltools package, which allows us to manipulate, combine and rearrange HTML elements directly from R:

    # CRAN
    install.packages("htmltools")
    # development version
    remotes::install_github("rstudio/htmltools")
  • Create an HTML element via the htmltools package:

    library(htmltools)
    tag <- div("Hello world")
    tag
    <div>Hello world</div>
  • Inside the tag function call, named elements become attributes; and, unnamed elements, children:

    div(
      id = "r-create-div",
      class = "r",
      `data-numChildren` = 1,
      div("Child")
    )
    <div id="r-create-div" class="r" data-numChildren="1">
      <div>Child</div>
    </div>