I'm preparing some lecture slides using the rmarkdown / knitr / beamer chain in RStudio. I would like to walk through the output of a summary(fit) call, but the output extends off the slide (even if I make the font size very small as in this answer).
The solution I've come up with is to capture the output of summary as a list, then print those outputs I'm interested in consecutively:
```{r, echo=TRUE, results='hide'}
summary(fit)
```
```{r, echo=FALSE}
out <- summary(fit)
```
```{r, echo=FALSE}
out$call
```
Is there a better way?
Check out the DT package. DT interfaces with the DataTable.Js library enabling print HTML widget datatables with pagination.
If you aren't viewing the deck from a browser (PDF or .doc) then you can export the plot and embed it in the document as desired - manually or script.
Related
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 have a shiny app (radiant.data) that uses knitr to generate reports viewable inside the application (R > Report). Because the output is displayed inside a shiny app I need a render function for something like a DT table to be displayed (i.e., convert to shiny.render.function). This all works fine.
Now, however, I want to use a custom print method to handle rendering so I can just use DT::datatable(mtcars) with knitr and knit_print.datatables to generate a shiny.render.function.
Below some example R-markdown that includes the knit_print.datatables function i'm using. chunk1 and chunk2 show a shiny.render.function as intended but the chunk6 shows nothing. If screenshot.force = TRUE I get a screenshot from chunk6 but that is not what I want either.
Example R-markdown to be processed with knitr::knit2html
```{r chunk1}
knitr::opts_chunk$set(screenshot.force = FALSE)
DT::renderDataTable(DT::datatable(mtcars))
```
```{r chunk2}
knit_print.datatables <- function(object, ...) {
DT::renderDataTable(object)
}
```
```{r chunk3}
knit_print <- knitr::knit_print
knit_print.datatables(DT::datatable(mtcars))
```
```{r chunk4}
getS3method("knit_print", "datatables")
```
```{r chunk5}
class(DT::datatable(mtcars))
```
```{r chunk6}
DT::datatable(mtcars)
```
I realize the R-markdown above, although reproducible if you have knitr and DT installed, looks a bit weird but when I export the knit_print.datatables function properly I get the same result in my application (see example output below).
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 have a very large phylogenetic tree that I'd quite like to insert into a supplementary material I'm writing using Rmarkdown and knitr. I dislike splitting trees across pages and I doubt anybody would print this out anyway so I thought I'd just have a large page in the middle of the pdf I'm generating.
The question is how do I change page size for one page in an otherwise A4 document? I'm pretty new to knitr and I've found global paper size options but I'm struggling to find ways of setting up what would be the equivalent of sections in Word.
(Update) Hi does anybody else have a suggestion? I tried the pdfpages package but this seems to result in an equally small figure on a page the size of the pdf that is being pasted in i.e. if I make a 20in by 20in pdf figure then paste the page in using \includepdf then I get a 20in by 20in page with a much smaller figure on it (the same as the \eject example above). It seems like knitr or Latex is forcing the graphics to have a specific size regardless of page size. Any ideas? Here's a reproducible example:
---
title: "Test"
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{pdfpages}
---
```{r setup, include=FALSE}
require(knitr)
knitr::opts_chunk$set(echo = FALSE,
warning=FALSE,
message=FALSE,
dev = 'pdf',
fig.align='center')
```
```{r mtcars, echo=FALSE}
library(ggplot2)
pdf("myplot.pdf", width=20, height=20)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
dev.off()
```
#Here's some text on a normal page, the following page is bigger but has a tiny figure.
\newpage
\includepdf[fitpaper=true]{myplot.pdf}
You should be able to use \ejectpage like this:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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 cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
\eject \pdfpagewidth=20in \pdfpageheight=20in
```{r mtcars}
library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
```
\eject \pdfpagewidth=210mm \pdfpageheight=297mm
Back
(I can only remember the A4 height in mm for some reason)
I have just faced the same struggle when dealing with a large phylogenetic tree.
Based on hrbrmstr's answer, I came up with the snippet below.
The trick is to make knitr generate the figure but not include it in the intermediate .tex right away (hence fig.show="hide"). Then, because figure paths are predictable, you can insert it with some latex after the code chunk.
However, the new page height is not taken into account when positioning page numbers, so they tend to be printed over your image. I have tried to overcome this behavior multiple times, but in the end I simply turned them off with \thispagestyle{empty}.
---
output: pdf_document
---
```{r fig_name, fig.show="hide", fig.height=30, fig.width=7}
plot(1)
```
\clearpage
\thispagestyle{empty}
\pdfpageheight=33in
\begin{figure}[p]
\caption{This is your caption.}\label{fig:fig_name}
{\centering \includegraphics[height=30in, keepaspectratio]{main_files/figure-latex/fig_name-1} }
\end{figure}
\clearpage
\pdfpageheight=11in
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