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:
To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.
Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.
R Markdown is an extension of the markdown syntax. R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.
The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.
I can think of three ways without messing with your toolchain, all of them are kind of hacky.
Just output the <style>
right in the body. This isn't technically valid, but it will work fine in any major browser.
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.
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.
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.brew
ed 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
, bothpander
. See installation details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With