Skip to content
David Springate edited this page Jun 3, 2013 · 1 revision

The DSL is based on the Hiccup library for Clojure. You can use it to render html and generic xml. The functions can be nested inside one another and are designed to be combined to easily build valid html in R.

The central function is m() # for Markup:
m("p") # The first argument is the html tag:
## [1] "<p />"
m("p", "This is a Sentence.", " So is this") # Any strings after form the content of the tag:
## [1] "<p>This is a Sentence. So is this</p>"
m("span",  "bar", opts = list(class = "foo")) # The opts list defines html tag attributes
## [1] "<span class=\"foo\">bar</span>"
m("span", opts = list(id = "foo", class = "bar"), "baz") 
## [1] "<span id=\"foo\" class=\"bar\">baz</span>"
m("p", 
     "Goodbye", 
     m("strong", "cruel"), 
     "world")# Tags can be nested inside of other tags
## [1] "<p>Goodbye<strong>cruel</strong>world</p>"
m("p#my-p", m("span.pretty", "hey")) # CSS-style shortcuts for ID and class
## [1] "<p id=\"my-p\"><span class=\"pretty\">hey</span></p>"
m("p", m("script", "Do something evil", escape.html.p = TRUE)) # Escape a tag using escape.html.p = TRUE
## [1] "<p>&lt;script&gt;Do something evil&lt;/script&gt;</p>"
There are also wrappers to generate a range of common html elements...
javascript.tag("Some javascript")  # To wrap the script string in script tags and a CDATA section
## [1] "<script>//<![CDATA[\nSome javascript\n//]]></script>"
link.to("www.google.com", "Google")  # To wrap content in an HTML hyperlink with the supplied URL
## [1] "<a href=\"www.google.com\">Google</a>"
# To wrap content in html hyperlink with the supplied email address.  If
# no content provided the email address is supplied as content:
mail.to("[email protected]")
## [1] "<a href=\"mailto:[email protected]\">[email protected]</a>"
mail.to("[email protected]", "email me")
## [1] "<a href=\"mailto:[email protected]\">email me</a>"
mail.to("[email protected]", "email me", subject = "you are great")
## [1] "<a href=\"mailto:[email protected]?Subject=you%20are%20great\">email me</a>"
# To wrap a list of strings into an unordered list:
elements = list("apples", "oranges", "bananas")
unordered.list(elements)
## [1] "<ul><li>apples</li><li>oranges</li><li>bananas</li></ul>"
ordered.list(elements)  # Ordered list
## [1] "<ol><li>apples</li><li>oranges</li><li>bananas</li></ol>"
image.link("www.beautifulthings.com/12538675", opts = list(alt = "A lovely picture of something"))  # link to an image
## [1] "<img src=\"www.beautifulthings.com/12538675\" alt=\"A lovely picture of something\" />"
head("My first page")
## [1] "My first page"
body("Hello world!")
## Error: object 'Hello world!' of mode 'function' was not found
...and functions to include css and js...
cat(include.css(c("mysheeet.css", "sheet2.css", "sheet3.css")))
## <link type="text/css" href="mysheeet.css" rel="stylesheet" />
## <link type="text/css" href="sheet2.css" rel="stylesheet" />
## <link type="text/css" href="sheet3.css" rel="stylesheet" />
cat(include.js(c("script1.js", "script2.js", "script3.js")))
## <script type="text/javascript" src="script1.js"></script>
## <script type="text/javascript" src="script2.js"></script>
## <script type="text/javascript" src="script3.js"></script>
Full pages with doctypes and outer html tags can be generated with the webdoc function
webdoc("html5",
       head("My first page"),
       body("Hello world",
            unordered.list(elements)))

Contents

Introduction

Installation and setup

HTML generation

Building a new static site

Posts and Pages

The Samatha engine

RSS Feeds

Testing your site

Samatha Online

Clone this wiki locally