Smich7 Smich7 - 1 year ago 84
LaTeX Question

Eval LaTeX code in R chunk (Knitr)

I'm actually creating a shiny app. In that app, there is a download button which download a PDF file that depends of user's input.

So I use a

file to do generate that PDF document. I just want to do a table (with tabular) which have a number of row that depends of app user's input.

So in my R chunck, i'd like to do something like that :


for (index in 1:nrow(myData))
SomethingThatRunLaTeXCode(paste0("\hline ",
"\Sexpr{",myData[index,1],"}"," % ","\Sexpr{",myData[index,2],"}"))

Answer Source

As suggested by sebastian-c, a much better way to make such a table is to use the xtable package together with Knitr. To make the Knitr chunks understand TeX, use the chunk option results='asis'.

Since your data is a data.frame, it is straight-forward:

<<echo = FALSE, results = "asis">>= 

## test data
df <- data.frame(Gaussian = rnorm(10), Exponential = rexp(10))

cap = paste("My caption can span multiple lines and",
            "can be arbitrarily long.")
xtable(df,caption = cap)

For full customization, use the function print.xtable on your xtable object.

<<echo = FALSE, results = "asis">>=
print.xtable(xtable(df),table.placement = "")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download