Giter VIP home page Giter VIP logo

mustachewidget's Introduction

mustachewidget

This is an R package for creating htmlwidgets that are html mustache templates populated with data.

When would I use this?

If you are making a one-off web page, it is better to just build the page than to use this tool. But if you making several versions of a static web page with each having the same layout but being populated with different data, then this can be a good tool to use. This package was developed particularly for use with Trelliscope, where often we want to design panels that are composed of custom html content.

You can learn more about mustache templates here.

Installation

You can install the development version of mustachewidget with the following:

remotes::install_github("hafen/mustachewidget")

Examples

Here are a few examples of how to use the package.

Simple

library(mustachewidget)

mustache("Hi, {{name}}", data = list(name = "Bob"))

This will output an htmlwidget web page that says "Hi, Bob".

Nested Values

mustache("
  Hello, {{name.first}} <b>{{name.last}}</b>!
  ",
  data = list(name = list(first = "Bob", last = "Smith"))
)

Lists

mustache("
  <h1>Alphabet</h1>
  <ul>
    {{#values}}
    <li>{{.}}</li>
    {{/values}}
  </ul>
  ",
  data = list(values = letters)
)

Conditional Content

mustache("
  <h1>{{title}}</h1>
  {{#show}}
    <p>{{content}}</p>
  {{/show}}
  {{^show}}
    (no content)
  {{/show}}
  ",
  data = list(title = "My Title", show = TRUE, content = "My Content")
)

Including Plots

If you want to include a locally-created plot in an mustachewidget output, this package has a utility function particularly for ggplot2, ggsave_base64(), that will save a plot as a base64 encoded string.

library(ggplot2)
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p64 <- ggsave_base64(p)

mustache("
  <h1>My plot</h1>
  <img width='500' src='{{plot}}' />
  ",
  data = list(plot = p64)
)

Specifying a Template File + Dependencies

Instead of specifying the template as a string, you can also specify a file path to a template html file.

Also, you may have CSS or JavaScript dependencies that you want to include in the output. You can specify these as well.

asset_dir <- system.file("example", package = "mustachewidget")

mustache(
  file.path(asset_dir, "template.html"),
  list(name = "Bob"),
  dependencies = htmltools::htmlDependency(
    name = "template",
    version = "1",
    src = c(file = asset_dir),
    script = "template.js",
    stylesheet = "template.css"
  )
)

mustachewidget's People

Contributors

hafen avatar

Stargazers

Jimmy Briggs avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.