msubbaiah
msubbaiah

Reputation: 350

R Markdown - SparkTables not rendering

I am trying to render a sparktable using Rmarkdown. But the output always comes out in raw html or tex format. This depends on whether I am rendering a PDF or an HTML. Not sure what to do here?

library(sparkTable)
data("AT_Soccer")
content <- list(
   function(x) {sum(x)},
   function(x) {round(sum(x),2)},
   function(x) {round(sum(x), 2)},
   newSparkLine(lineWidth = 2,pointWidth = 6),
   newSparkBar()
)
names(content) <- c("Points","ShotGoal","GetGoal","GoalDiff","winLose")
vars <- c("points","shotgoal","getgoal","goaldiff","wl")
stab <- newSparkTable(AT_Soccer,content,vars)
export(stab, outputType = "html") ### For HTML R-Markdown files
export(stab, outputType = "tex") #### For PDF R-Markdown files

My output (for html files) looks like:

Output

The pdf output is:

PDF output

I am trying to get the actual sparktable. I have been able to render the actual table like this:

 showSparkTable(stab)

However, that opens the spark table within the Shiny framework. I'm trying to produce multiple rmarkdown documents with spark tables.

I took this example from: https://journal.r-project.org/archive/2015-1/templ-kowarik-meindl.pdf. Page 29.

Solution for HTML

Setting this worked for me. Thanks to Martin. Still stuck on the pdf one though.

   knitr::opts_chunk$set(results = 'asis')

HTML Fix

Upvotes: 3

Views: 671

Answers (1)

Martin Schmelzer
Martin Schmelzer

Reputation: 23889

After studying the documentation a bit I summarize what I learned about including sparkTables inside Rmd documents:

1. For HTML documents (outputType = 'html'):

Just as I said use the chunk option results = 'asis'.


2. For PDF documents (outputType = 'tex'):

You also need the option above in the case of PDF documents. BUT if you dont use it, you will see the plain LaTeX that is generated by export(). At the very bottom of that output you will find an important hint:

## Information: please do not forget to add the following command before \begin{document} in your tex-fi
##
## \newcommand{\graph}[3]{ \raisebox{-#1mm}{\includegraphics[height=#2em]{#3}}}

So what we have to do here is to

  • include that line of LateX in our preamble,
  • add results = 'asis' to the code chunk,
  • and set the argument infonote of export() to FALSE.

The last point prevents another error that the LaTeX compiler would throw (namely that we already have defined the command \graph).

What follows is a working example for a PDF document:

---
title: "Plotting Plots Under Code"
author: "Martin"
date: "February 1, 2017"
output: pdf_document
header-includes:
  - \newcommand{\graph}[3]{ \raisebox{-#1mm}{\includegraphics[height=#2em]{#3}}}
---


```{r setup, echo = F, warning = F, message = F, results = 'asis'}
library(sparkTable)
data('AT_Soccer')
content <- list(
   function(x) {sum(x)},
   function(x) {round(sum(x), 2)},
   function(x) {round(sum(x), 2)},
   newSparkLine(lineWidth = 2, pointWidth = 6),
   newSparkBar()
)
names(content) <- c('Points', 'ShotGoal', 'GetGoal', 'GoalDiff', 'winLose')
vars <- c('points', 'shotgoal', 'getgoal', 'goaldiff', 'wl')
stab <- newSparkTable(AT_Soccer, content, vars)
export(stab, outputType = 'tex', infonote = F)
```

enter image description here

Upvotes: 4

Related Questions