hypertext

R-CMD-check CRAN status Lifecycle: stable

html element construction for R.

hypertext provides a deterministic, framework-agnostic DSL for building html nodes and rendering them to a string. it does not implement templating, dependency management, widgets or framework integrations.

heavily inspired by {htmltools}.

installation

install the stable version from CRAN:

install.packages("hypertext")

or get the development version from github:

devtools::install_github("sigflux/hypertext")

quick start

library(hypertext)

page <- tags$html(
  tags$head(
    tags$title("hypertext")
  ),
  tags$body(
    tags$h1("Hello"),
    tags$p(
      class = c("lead", "mb-2"),
      "Server-side HTML."
    ),
    tags$input(
      type = "text",
      placeholder = "enter your nickname"
    ),
    tags$button(
      "Click"
    )
  )
)

render(page)

rendering

render() takes a tag tree and returns a single HTML string:

x <- tags$p(
  class = "lead",
  "hello"
)

# `x` contains the tag tree
class(x)
#> [1] "hypertext.tag"

# rendering produces an HTML string:
render(x)
#> [1] "<p class=\"lead\">hello</p>"

creating html files

library(hypertext)

page <- tags$html(
  tags$head(
    tags$title("hypertext")
  ),
  tags$body(
    tags$h1("Hello"),
    tags$p(
      class = c("lead", "mb-2"),
      "Server-side HTML."
    ),
    tags$input(
      type = "text",
      placeholder = "enter your nickname"
    ),
    tags$button(
      "Click"
    )
  )
)

content <- render(page)

writeLines(text = content, con = "index.html")

usage in frameworks