Giter VIP home page Giter VIP logo

htmlcreator's Introduction

Renjin html creator

Renjin R package to create html

This package provides a simple way to create html content. Here is an example:

  library('se.alipsa:htmlcreator')

  html.new("<html><body>")
  html.add("<h2>A Sample report with a table and an image</h2>")
  html.addPlot(
    {
      plot(mtcars$mpg ~ mtcars$hp, pch=23, col="orange", bg="orange", cex=1.5, lwd=2)
      abline(h = mean(mtcars$mpg), col="green")
    }, 
    width=300, 
    height=400, 
    htmlattr=list(alt="mtcars mpg ~ hp", id="plot1")
  )
  html.add(mtcars)
  html.add("</html></body>")
  # save the html to a file
  outFile <- tempfile("plot", fileext = ".html")
  write(html.content(), outFile)
  print(paste("Wrote", outFile))

To be able to do this, add the dependency to your pom.xml as follows:

<dependency>
  <groupId>se.alipsa</groupId>
  <artifactId>htmlcreator</artifactId>
  <version>1.4.1</version>
</dependency>

As you can see, the main method is the overloaded html.add. It can take

  1. strings (charvectors) as parameters (which are treated as raw html),
  2. a data.frame (which is converted into a html table), or
  3. a plot function (which converts the plot into an img tag). Notice that the plot function is passed in enclosed with { }, this to allow the plot function to be executed by the html.addPlot method (which converts the result of the plot to an image and base64 encodes it into a string which is then made part of the img tag) rather than executed before the function is called.

In addition to html.add(x,...) and html.addPlot(x, ...), there is the html.clear() function which resets the underlying html object (clears the content). The html.new(x, ...) is an alias for html.clear() followed by html.add(x,...) and is a good way to start the script (especially if you run multiple scripts in the same session).

html attributes can be set by setting the parameter htmlattr to a list of attribute, e.g:

# add id and class to a table:
html.add(mtcars, htmlattr=list(id="cardetails", class="table table-striped"))

# add alt attribute to an img:
html.addPlot({
  plot(
    cars,
    main="Cars speed and dist",
    col=c("darkblue")
  )
  abline(h = mean(cars$dist), col="red")
  abline(v = mean(cars$speed), col="red")
  },
  htmlattr = list(alt="a cars plot")
)

It is also possible to use the underlying reference class (Html) and the specific html creating methods directly. The underlying html creating methods are:

  • html.table - converts a data.frame to a table
  • html.imgPlot - converts a plot to an img tag
  • html.imgPlotComplex - converts a series of plot commands enclosed with { } to an img tag
  • html.imgFile - converts a file to an img tag
  • html.imgUrl - creates an img tag

Version history

1.4.2

1.4.1

  • upgrade maven enforcer plugin
  • Add addPlot function for better plotting capabilities
  • Make sure get content is enclosed in <html> </html>

1.4

  • add html.new(...) as an alias for html.clear() followed by html.add(...)
  • fix broken tests

1.3

  • use a dedicated env to avoid accidental overwrites
  • Remove space in base 64 file encoding (no space works in all browsers whereas the space caused problems in (at least) Firefox)

1.2

  • Fixes to img handling, rename internal functions for clarity

1.1

  • bugfix for matrix's
  • add ability to add element attributes (e.g class, id etc)

1.0 initial release

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.