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
Related
I just found this awesome technique to put the code used in the .Rmd file in the appendix (of that same file).
However, I am using R Sweave and not R Markdown and I would like to know if there exists a similar way to put all the code at the end in a unique chunk. The code to do that in Markdown does not work in Sweave. I precise that, unlike this post, I do not have a separate .R file where the calculations are made. Everything is done in the .Rnw file.
Does anybody know how to do it?
Edit : a reproducible example
\documentclass[11pt, twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<reg2, echo=FALSE, print=FALSE>>=
head(mtcars)
#
<<reg3, echo=FALSE, print=FALSE>>=
head(iris)
#
\section*{Appendix}
% the place where I could like to put the whole code
\end{document}
This chunk works to include the code:
<<echo=FALSE, eval=TRUE>>=
filename <- tempfile(fileext=".R")
Stangle("test.Rnw", output = filename, quiet = TRUE)
cat(readLines(filename), sep = "\n")
#
When I include that in your example file, I see this:
I think it's possible to modify the format a bit; see ?Rtangle for some details. Similar things are possible with knitr, but it's more flexible. I suspect the best method would be similar to the one you found for RMarkdown.
Is it possible to place a table generated with the xtable (or alternatively the pander) package and a generated plot side-by-side in R markdown knitting to pdf while the rest of the document is not in columns? The following simple example hopefully illustrates the idea:
\begin{multicols}{2}
```{r}
plot(cars)
```
```{r, results='asis'}
library('xtable')
print(xtable(head(cars,5)), type = "latex")
```
\end{multicols}
However, this does not produce the plot. I know that solutions exist using knitr (e.g. here) and for R markdown knitting to HTML (e.g. here) but I don't get them to work for R markdown to pdf.
the gridExtra package works for this without having to go into LaTeX hell. Use the grid.arrange function for side by side charts and what-not.
Works on html and PDF outputs.
Thank you #nycrefugee this already opens some more opportunities. However, there seem to be some problems with xtable outputs. If I use the following code:
library('gridExtra')
library('grid')
library('xtable')
library('lattice')
p = xyplot(1~1)
t = textGrob( print(xtable(head(cars,5)),
type = "latex", label = "test")
)
grid.arrange(p, t, ncol=2)
it produces the following output when compiled to pdf, i.e. the table is shown above the two grobs:
PDF output
I use ProjectTemplate and Knitr to produce reports. Most of the analysis is stored in the src directory, whilst the report contains the presentation R markdown.
I would like the main text to include only the results of the analysis, and the document appendix to contain some code chunks from the analysis. The only way I have found to achieve this is as follows:
First, run the actual analysis in the main body of the document:
```{r runanalysis, warning=FALSE, message=FALSE}
# run the analysis code to generate the objects
source('../src/rf-model-caret.R')
```
Secondly, in the appendix, two knitr chunks are needed. The first reads in the actual code (and executes it). The second displays the code.
```{r analysis, eval=TRUE, echo=FALSE}
knitr::read_chunk('../src/rf-model-caret.R')
```
```{r analysis2, ref.label="analysis", eval=FALSE, echo=TRUE}
```
This works but seems very inefficient because:
The analysis has to be run twice - firstly in the source in the main document, and again in the appendix just to produce the code.
reading a knitr chunk and then referencing it again immediately to display the code
Is there a better way to achieve the goal of executing external source in the main document and printing the code in the appendix?
You may try this:
In the main body:
```{r runanalysis, code=readLines('../src/rf-model-caret.R'), echo=FALSE, eval=TRUE}
```
In the appendix:
```{r runanalysis, code=readLines('../src/rf-model-caret.R'), echo=TRUE, eval=FALSE}
```
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.
I have a .Rnw file that looks like this:
\documentclass[a4paper,11pt]{article}
\begin{document}
\title{}
\author{me}
\date{\today}
\maketitle
\section{Header}
<<table_mtcars, results = "asis">>=
stargazer(rbind(mtcars, mtcars, mtcars), summary = F)
#
\FloatBarrier
\end{document}
I can use knitr::knit to produce a .tex file and then convert this to a PDF. The resultant PDF looks like this:
Note how the table disappears off the end of page 2 and doesnt resurface on page 3. How can I get the table to continue on page 3 of the PDF using stargazer?
As far as I know this is not currently supported by stargazer. I would recommend emailing the developer, who is usually very open to suggestions. The issue is that you need to use LaTeX's longtable environment instead of tabular, but this is the part that Stargazer doesn't seem to support.
You can create a workaround by doing:
\documentclass[a4paper,11pt]{article}
\usepackage{longtable}
\begin{document}
\title{}
\author{me}
\date{\today}
\maketitle
\section{Header}
\Sexpr{library(knitr);gsub('tabular','longtable',knit(text="<<results = 'asis'>>=\nlibrary(stargazer)\nstargazer(rbind(mtcars, mtcars, mtcars), summary = FALSE, float = FALSE)\n#"))}
\end{document}
Basically, you're calling knitr within your .Rnw file in order to build the table, and gsub the tabular environment to longtable. When I run this, it comes out as three pages long.