How to remove the % lines in xtable table output by Knitr - r

By using xtable and knitr, I add a table to my RMD document and export to PDF file.
```{r, results='asis'}
library(xtable)
xtable(matrixtable)
````
It looks great except there is a line
% latex table generated in R 3.1.0 by xtable 1.7-3 package % Wed Jun 25 13:34:57 2014
How can I remove this line. I tried to set message=FALSE but it doesn't work.

The inclusion of the comment in the final table is defined by the comment argument to print.xtable
the default value for this is getOption('xtable.comment',TRUE).
so, if you set
options(xtable.comment = FALSE)
then for any future tables this comment will not produced.

You can put in directly in the print statement like so:
xtb=xtable(...)
print(xtb, comment=FALSE)

Related

Remove note added by knitr in R markdown document

I have knitted an html document in R. The output is good; it's just that at the very end I see this note:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot
How can I remove this note from showing in the knitted document?
This doesn't appear in my version of Rmarkdown running the test document below:
---
title: R Markdown
output: html_document
---
This is an R Markdown document.
```{r}
summary(cars)
```
You can also embed plots:
```{r, echo=FALSE, message=FALSE}
plot(cars)
```
So I think there are two posibilities.
You accidentally added it (perhaps by copying this template), or
It was removed in a previous version, as the latest version of Rmarkdown (2.9) does not produce this warning.
I suspect it's case 1, as I've never seen this output automatically inserted before.

xTable doesn't produce a table in R markdown

I am trying to get xtable to insert a table into an R markdown pdf document I'm working on. For some reason, I can't even get the examples from the documentation to reproduce. Instead, the output puts each individual cell on a new line. I've included a screen shot of a portion of my pdf. What am I doing wrong?? Do I need to update a package somewhere? I downloaded xTable today.
```{r, echo=FALSE, results = "asis"}
library(xtable)
data(tli)
print(xtable(tli[1:10,]), type = "html", floating = FALSE, include.rownames = T)
```
Sample of bad pdf

how do you print xtable using knitr in word document

I am trying to print xtable in using knitr in word document as follows:
library(knitr)
library(xtable)
p1<-xtable(head(iris), include.rownames=FALSE, floating=FALSE)
print(p1)
I get this error, any ideas:
latex table generated in R 3.2.2 by xtable 1.7-4 package % Mon Oct 26 11:17:22 2015
I don't think you can do this with the standard word_document that comes with rmarkdown. xtable only produces output in HTML and LaTeX formats. To render a word document, your table has to be prepared in markdown format (so far as I know).
Some alternative options are to use knitr::kable or the pixiedust package.
If you're comfortable downloading things from GitHub, you may also use devtools::install_github("gforge/Grmd") and then replace output:word_document with output: Grmd::docx_document. Then using print.xtable with type = 'html' will render to an HTML document that can be opened as a docx.

Format text inside R code chunk

I am making some slides inside Rstudio following instructions here:
http://rmarkdown.rstudio.com/beamer_presentation_format.html
How do I define text size, colors, and "flow" following numbers into two columns?
```{r,results='asis', echo=FALSE}
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
cat(rd, sep = "\n")
```
Output is either HTML (ioslides) or PDF (Beamer)
Update:
Currently the code above will only give something like the following
6683209
1268680
8412827
9688104
6958695
9655315
3255629
8754025
3775265
2810182
I can't do anything to change text size, color or put them into a table. The output of R codechunk is just plain text. Maybe it is possible to put them in a table indeed, as mentioned at this post:
http://tex.aspcode.net/view/635399273629833626273734/dynamically-format-labelscolumns-of-a-latex-table-generated-in-rknitrxtable
But I don't know about text size and color.
Update 2:
The idea weaving native HTML code to R output is very useful. I haven't thought of that. This however only works if I want to output HTML. For PDF output, I have to weave the native Latex code with R output. For example, the code following works using "knitr PDF" output:
```{r,results='asis', echo=FALSE}
cat("\\textcolor{blue}{")
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
for (n in rd) {
cat(paste0(n, '\\newline \n')) }
cat("}")
```
You are using results='asis', hence, you can simply use print() and formatting markup. If you want your text to be red, simply do:
```{r,results='asis', echo=FALSE}
print("<div class='red2'>")
rd <- sample(x=1e6:1e7, size = 10, replace = FALSE)
cat(rd, sep = "\n")
print("</div>")
```
Hope it helps.
It sounds as if you want the output to be either PDF or HTML.
One possibility is the xtable package. It produces tables either in PDF or HTML format. There's no (output-independent) way to specify colour, however. Here's an example.
xt <- xtable(data.frame(a=1:10))
print(xt, type="html")
print(xt) # Latex default
Another option is the pandoc.table function from the pander package. You need the pandoc binary installed. If you have RStudio, you have this already. The function spits out some markdown which then can be converted to HTML or PDF by pandoc.
Here's how you could use this from RStudio. Create an RMarkdown document like this:
---
title: "Untitled"
author: "You"
date: "20 November 2014"
output: html_document
---
```{r, results='asis'}
library(pander)
tmp <- data.frame(a=1:10,b=1:10)
pandoc.table(tmp)
```
When you click "knit HTML", it will spit out a nice HTML document. If you change output to pdf_document, it will spit out a nice PDF. You can edit the options to change output - e.g.
pandoc.table(tmp, emphasize.strong.rows=c(2,4,6,8,10))
and this will work both in PDF or HTML. (Still no options to change colour though. Homework task: fix pandoc.table to allow arbitrary colours.)
Under the hood, knitr is writing markdown, and pandoc is converting the markdown to whatever you like.

Include latex tables generated by Hmisc latex in knitr slides

I am sorry, I am new to using knitr to make slides. I generally use the latex() function in Hmisc package to generate my tables based on R objects. I would like to produce a slide that shows the r code and then below it displays the properly formatted table. Something like:
``` {r}
latex(tabdat,file="tables/tabdat.tex",ctable=TRUE,caption="A basic table",caption.loc="bottom",label="tab:dat1",row.names=NULL,rowlabel="")
```
So that the finished slide displays the exact r code and the formatted table looking exactly as if I had run latex using \input{tabdat}
I would appreciate any advice on how to accomplish this.
Thanks!
I am a bit puzzled because you talk about PDF/LaTeX output but you are using R markdown tags.
Here are small examples for both cases, R Sweave, i.e. LaTeX output, and R markdown, i.e. HTML output. For creating the LaTeX code there are several packages available (xtable, Hmisc etc.) for HTML AFAIK only xtable.
The main point of how to include the raw output just like it appears in the console is the same for both output types and was already explained by Tyler Rinker above, i.e. by adding results="asis" to the chunk options.
PDF/LaTeX / Rnw-file
\documentclass{article}
\begin{document}
<<echo=FALSE, message=FALSE>>=
library(Hmisc)
library(xtable)
#
<<results='asis'>>=
latex(head(iris), file = '')
#
<<results='asis'>>=
xtable(head(iris))
#
\end{document}
HTML, Rmd-file
```{r echo=FALSE}
library(xtable)
```
```{r results='asis'}
tbl <- xtable(head(iris))
print(tbl, type="html")
```
Look here for more examples and options: http://www.stat.iastate.edu/centers/CCGS/slides/slides-Rtables.pdf

Resources