9

I previously asked a question about how to export a HTML table in R and have control over line borders.

I'm used to LaTeX where when you create a table, the formatting of the table is largely determined by the text and markup that appears at that point. This works well for Sweave, because your R code chunk can output LaTeX table markup at that point. I also understand that there are tools like xtable that can produce HTML markup for a table.

However, control over HTML tables seems to rely on style sheets, which are meant to appear in the header of the document and not in the location where the R code chunk is placed. Of course, I could just put content in the style sheet, but in scientific applications often there can be some quite specific table formatting that varies in some respects from table to table.

Thus, my question:

  • In general, how do you format an HTML table with literate programming like R Markdown or even from raw HTML if formatting of the output requires output to be created in a separate place in the document (i.e., CSS for the table in the header) to where the R code chunk is placed (i.e.,the table itself in the body)?
Community
  • 1
  • 1
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
  • By giving the table a class, and having class-specific css. Please add a counter-sample if this solution should not solve the problem. – Dieter Menne Jun 01 '12 at 06:24
  • @Dieter I realise that that is a solution. The question I have is how do you get the class-specific css from the R code block into the header in an R Markdown or HTML Sweave document? I'm interested in how you could do it at all, and even more interested in how to do it in an elegant and straightforward way. – Jeromy Anglim Jun 01 '12 at 06:26
  • a) Quick and dirty: with cat. b) For serious work: with a brew template. – Dieter Menne Jun 01 '12 at 06:40
  • Thanks, I'll check out brew template. But how do I cat to the header of the HTML produced by an R Markdown file? And does this need to appear in a separate code block? That sounds like it would be awkward from a "don't repeat yourself" perspective. – Jeromy Anglim Jun 01 '12 at 07:24

5 Answers5

5

I can think of three ways without messing with your toolchain, all of them are kind of hacky.

  1. Just output the <style> right in the body. This isn't technically valid, but it will work fine in any major browser.

  2. Emit JavaScript that creates a <style> block at runtime and appends it to the head (here's one way). This will look a little gross in the HTML source and in the R code, but it will work and it will validate.

  3. Use a scoped style block. This would be exactly what you are looking for, except that the scope attribute is new to HTML5 and not yet implemented in any major browser. However, if you base your styles on uniquely generated IDs (i.e. your rules are written such that even if they apply to the whole document they won't mess anything up), I imagine the browsers will just ignore the "scoped" attribute and everything will work properly--then this becomes effectively a version of option 1 that happens to validate!

(I would go with #3, personally.)

If you haven't already, it'd be worth starting a thread at the RStudio support forum about this; even though it's not strictly an RStudio issue, we're obviously doing a lot of work on the end-to-end scenario of publishing reports in R Markdown and would love to find out more about your specific examples. Tables are clearly going to be a big part of what people do with this reports and we know this is a weak spot right now, one that we do want to address in future versions.

Community
  • 1
  • 1
Joe Cheng
  • 8,001
  • 42
  • 37
2

An alternative method would be to use pander as R markdown backend (sorry for this marketing-like answer, but I do think my Pandoc.brew function could be really handy for this purpose).

It is similar to knitr (parsing/evaling R commands in a markdown formatted file) but using brew syntax for R code blocks (e.g. <%...%> for general R code - like loops etc. and <%=...%> for returning results in a block). But differs from brew as Pandoc.brew does not only cat results in a code block, but runs my pander generic method which transforms (quite q wide variety of) R objects to (IMHO) pretty Pandoc's markdown format.

So running Pandoc.brew on a markdown formatted file would result in a clean markdown file with all R code blocks run - and you do not have to deal with xtable and other tweaks (not even with plots as all R code blocks resulting in an image is rendered to a png file and linked in the markdown text file).

And about why I started to answer here: with pander you can pass special options to pandoc, e.g. adding a custom CSS stylesheet (or JS etc.) to your generated HTML's header, see details on Pandoc's homepage. Based on that you could easily add your CSS file(s) or even just a bunch of style parameter. This could be done in pander with Pandoc.convert's option. BTW you do not even have to use my forked brew function, you can generate your markdown file with e.g. knitr and call Pandoc with the above function.

pander adds some CSS/JS to generated HTML files, which would generate (IMHO) quite pretty output, but you can easily customize that and adding your own files there.

For example: you would get this HTML file based this markdown by default which was Pandoc.brewed from this quite short markdown syntax brew file. BTW my github page was also generated/automatically styled by my markdown parser. I would really appreciate if you would try it :)


NOTE: to try the above calls you would need Pandoc pre-installed, also you'd need an up-to-date version of both rapport, both pander. See installation details.

daroczig
  • 28,004
  • 7
  • 90
  • 124
2

One option which doesn't completely solve the problem is to use gvisTable:

Here is a basic gvisTable:

```{r message=FALSE}
# install.packages("googleVis")
library(googleVis)
library(MASS)
data(Animals)
```

```{r results='asis'}
tab1 <- gvisTable(Animals, 
                   options = list(width = 600, height = 650, 
                                  page = "enable",
                                  pageSize = nrow(Animals)))
print(tab1, "chart")
```
  • ?print.gvis explains some of the options for printing the gvis object.
  • In partiuclar the tag="chart" option is required for R Markdown documents as this means that the output is just what is required for the object, rather than a complete HTML page as is the default.
  • See the output of this and a little more here
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
1

Ok, hope I got it now. You should set some additional knitr options, and you could cat() the css dynamically if you like.

<!DOCTYPE html>
<head>
<style type="text/css">
.greenback {
  background-color: teal;
    color: white;
}
.greenback td {
  border: dotted gray;
}
.bluescreen {
    background-color: blue;
    color: white;
}
.bluescreen td {
  border: thick solid;
  padding:2px;
  margin:2px;
}

</style>
</head>

<body>
<table class="greenback">
<tr><td>Hello</td><td>Mars</td><tr>
<tr><td>World</td><td>Moon</td><tr>
</table>

Could use some xtable code here instead.

<!--begin.rcode
cat('
<table class="bluescreen">
<tr><td>Hello</td><td>Mars</td><tr>
<tr><td>World</td><td>Moon</td><tr>
</table>
')
    end.rcode-->

</body>
</html>
Dieter Menne
  • 10,076
  • 44
  • 67
  • +1 Thanks for this. I guess I'm looking for a way to include the style information in the body so that an R function could write both the style information and the table text in an R Markdown document. gvisTable seems to do a bit of this using scripts. – Jeromy Anglim Jun 05 '12 at 06:50
1

Neil Saunders has a tutorial on customising CSS for HTML generated using RStudio. It shows how you can modify the built in style file and source this alternative file.

Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
  • That seems very nice but I don't understand how to do the file `custom.css`. Where do we have to include the modifications of the `markdown.css` file ? – Stéphane Laurent Jan 21 '13 at 16:51
  • From reading the tutorial it sounds like you (a) create a new css file in the working directory, (b) use the `stylesheet` argument in the `markdownToHTML` function with the value of the css file name. – Jeromy Anglim Jan 22 '13 at 00:42
  • Yes but the tutorial says that we have to modify the `markdown.css` file. It shows these modifications but I d'ont know where to include these modifications. At the beginning of the file ? At the end ? The language of these modifications don't look like the language of the `markdown.css`file hence I don't even understand why they are called modifications. – Stéphane Laurent Jan 22 '13 at 08:59
  • Ok I have understand now. But the output is a little ugly when including rownames. – Stéphane Laurent Jan 24 '13 at 12:16
  • Moreover my accentued letters are destroyed when using this way. – Stéphane Laurent Jan 24 '13 at 12:25