Is it possible to use pander tables in jekyll page with kramdown - r

I tried to use pander tables in a markdown page that is generated from R-markdown files like Yihui describes in an example page.
In my _config.yml I have the markdown type set to markdown: kramdown. I know that some things are different in this markdown type compared to the markdown knitr uses per default. So if I compile my page in RStudio with knitr the table looks alright. If I compile the page with jekyll the table looks not formatted right.
```{r key}
library(pander)
LETTERSplus <- c(LETTERS, "_", ".", ",", "-")
key <- sample(LETTERSplus)
names(key) <- LETTERSplus
pander(key)
```
Compiled with knitr inside RStudio it looks alright:
Compiled with Jekyll it looks like this:
I don't want to use kable since it formats the table differently and there is now column wrap if the table is too long.
Do I have to set some special pandoc options to make this work? Options to define the markup type pandoc has to output? I don't see any other options inside the pander() function.

Related

Using huxtable labels in RMarkdown for PDF

I am using huxtable to display tables in RMarkdown for output: pdf_document.
When I put a label in the table
huxtable(mytable) %>%
set_label("tab:mylabel")
and then make a reference in the body text like
See Table \#ref(tab:mylabel) for more details
It displays "See Table #ref(tab:mylabel) for more details" rather than giving the table number.
This was covered for Bookdown in
Huxtable package for R: How to correctly reference huxtables in bookdown?
Do I need some special option or other setting to make it work for a regular PDF document?
Seems so. The rmarkdown guide says you will need "a bookdown format" to use cross-referencing in that way. However, there's an alternative: use embedded TeX, rather than this rmarkdown format. A MWE:
```{r}
hux("A huxtable") %>%
set_label("tab:foo") %>%
set_caption("You'll need a caption too")
```
Here's a reference to \ref{tab:foo}.

Render Rmd file with pipe formatting for tables

Problem
I have a .Rmd template file which I currently use to render PDFs using rmarkdown::render.
I need the option to also render the .Rmd as a markdown file. I have tried setting output_format to md_document in rmarkdown::render however this produces a markdown file containing lots of html tags for the tables etc. I don't want any html; I need it to be readable in a console, with tables formatted using the markdown pipe styling.
What I've tried
I tried setting output_format to
output_format(knitr = rmarkdown::knitr_options(opts_knit = list(knitr.table.format = "pipe")),
pandoc = rmarkdown::pandoc_options(to = rmarkdown::rmarkdown_format())
but this style produces a markdown file with html tags instead of piped tables.
How do I set the markdown render settings to render as I need?
Perhaps try adding results='asis' to knitr opts

Compile Bookdown to Markdown?

Is there any way to take a Bookdown project, and build it as Markdown instead of HTML or TeX?
I ask because I need to post-process the final Markdown output from Bookdown, in order to extract R and Python notebooks for download.
In more detail, I am using Bookdown to build a textbook that embeds notebooks to download, where the notebooks contain subsets of the code and text in the bookdown .Rmd files. For example, a single chapter could contain more than one notebook.
In order to do this, I put start and end comment markers in the RMarkdown input text to identify the section that will be a notebook, and then post-process the generated Markdown files to extract the notebook section. As in something like:
<!--- notebook: first_section.Rmd
-->
Some explanation, maybe using Bookdown extra markup such as #a_citation.
```{r}
a <- 1
a
```
<!--- end of notebook
-->
More markdown.
```{r}
# More code not in notebook.
b <- 2
```
Obviously I could use the input RMarkdown pages, but this would be ugly, because all the extended Bookdown markup such as citations, cross-references and so on, would appear in raw and ugly form in the generated notebook. So I'd really like to be able to get the final output Markdown, after merging, resolving of citations and cross references. Is there any way of doing that?
My question is similar to this as-yet unanswered question, but adds my motivation for an official solution to this problem.
With the latest version of bookdown on CRAN, you can use the output format bookdown::markdown_document2, e.g.,
output:
bookdown::markdown_document2:
base_format: rmarkdown::md_document
variant: gfm

How to replicate Knit HTML in a command line?

I know this question is similar to this one. But I couldn't get a solution there so posting it here again.
I want to get the exact same output as I get by clicking "Knit HTML" but via a command. I tryied using knit2html but it messes with the formatting and does not include the title, kable does not work etc.
Example:
This is my test.Rmd file,
---
title: "test"
output: html_document
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
library(knitr,quietly=T)
kable(summary(cars))
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Output:
Knit HTML
knit2html
The documentation tells us:
If you are not using RStudio then you simply need to call the rmarkdown::render function, for example:
rmarkdown::render("input.Rmd")
Note that in the case using the “Knit” button in RStudio the basic mechanism is the same (RStudio calls the rmarkdown::render function under the hood).
In essence, rmarkdown::render does a lot more setup than knitr::knit2html, although I don’t have an exhaustive list of all differences.
The most flexible way of rendering the output is, at any rate, to supply your own stylesheet to format the output according to your wishes.
Please note that you need to set up Pandoc manually to work with rmarkdown::render on the command line.
That said, here are two remarks that would improve the knitr::knit2hmtl output, and that are superior to using rmarkdown::render in my opinion:
To include the title, use a Markdown title tag, not a YAML tag:
# My title
To format tables, don’t use the raw kable function. In fact, this is also true when using rmarkdown::render: the alignment of the table cells is completely off. Rmarkdown apparently uses centering as the default alignment but this option is almost never correct. Instead, you should left-align text and (generally) right-align numbers. As of this writing, Knitr cannot do this automatically (as far as I know) but it’s fairly easy to include a filter to do this for you:
```{r echo=FALSE}
library(pander)
# Use this option if you don’t want tables to be split
panderOptions('table.split.table', Inf)
# Auto-adjust the table column alignment depending on data type.
alignment = function (...) UseMethod('alignment')
alignment.default = function (...) 'left'
alignment.integer = function (...) 'right'
alignment.numeric = function (...) 'right'
# Enable automatic table reformatting.
opts_chunk$set(render = function (object, ...) {
if (is.data.frame(object) ||
is.matrix(object)) {
# Replicate pander’s behaviour concerning row names
rn = rownames(object)
justify = c(if (is.null(rn) || length(rn) == 0 ||
(rn == 1 : nrow(object))) NULL else 'left',
sapply(object, alignment))
pander(object, style = 'rmarkdown', justify = justify)
}
else if (isS4(object))
show(object)
else
print(object)
})
```

knitr html to Word docx using pandoc

I have been saving some example R markdown html output to Word using pandoc. I actually only do this so I can add some page breaks for easier printing:
system("pandoc -s Exercise1.html -o Exercise1.docx")
Although the output is acceptable I was wondering if there is a way to keep the original syntax highlighting of the R chunks (just as they are in the original knit HTML document)?
Also, I seem to be loosing all images in the conversion process and have to stick them into Word by hand. Is that normal?
Using the rmarkdown package (baked into RStudio Version 0.98.682, the current preview release) it's very simple to convert Rmd to docx, and code highlighting is included in the docx file.
You just need to include this at the top of your markdown text:
---
title: "Untitled" # obviously you can change this
output: word_document # specifies docx output
---
However, it seems that page breaks are still not supported in this conversion.
Why not convert the markdown directly to Word format?
Anyway, Pandoc does not support syntax highlighting in Word: "Currently, the only output formats that uses this information are HTML and LaTeX."
About the images: the Word file would definitely include those if you'd convert the markdown to Word directly. I am not sure about the HTML source, but I suppose you might have a path issue.

Resources