Preview tables in rmarkdown - r

The preview of the tables in a rmarkdown document within R does not work anymore since the last update.
Even installing a prior version of rmarkdown and of R itself did not solve the problem.
Usually, the preview of the output is created in multiple preview windows. However, this does not work anymore. All dataframes are complied within one preview and do not look as nice as before.
What I want: A preview somehow like this where you can flip through the colums and rows:
https://bookdown.org/yihui/rmarkdown/images/paged.png
What I get:
No nice format
Or even:
No nice format2
Does anyone have a solution?
I used this code:
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
summary(cars)
iris
Thank you!
Edit: the preview magically appears if I convert the data frames to tibbles.

What you are wanting is called a paged data frame in an HTML document in RMarkdown. You would need to add the df_print: paged to your YAML header.
---
title: "iris paged Data Frame"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r chunk2}
iris
```
You can control the rows printed globally with knitr::opts_chunk$set(echo = TRUE, rows.print=25) inside your setup chunk.

Related

Can R Markdown be made to preview from anywhere other than the start of the document?

I'm currently writing something in R Markdown. Whenever I Knit the document, RStudio's preview takes me back to the very beginning of the document. Is there a way to make this new preview display a location closer to where I've been working by default? For example, can I make it preview at a location near where my cursor for typing is?
The comments have suggested a number of workarounds. So far, my best is to just type the section number of the section where I'm working in to the search bar that RStudio's preview window provides. I'd click on the relevant entry in the table of contents, but I use output: github_document: toc: true number_sections: true, which is waiting on a patch to its numbered tables of contents.
Not quite as simple as you had in mind, but it is possible to use javascript to control what section of an html document is displayed:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{js}
location.hash = "#goto";
```
```{r, results='asis'}
cat(sprintf("# %s\n\nSection text here.\n\n", 1:10), sep = "")
```
# GOTO
Scroll Here
```{r, results='asis'}
cat(sprintf("# %s\n\nSection text here.\n\n", 11:20), sep = "")
```

Adjusting flextable position in rmarkdown

I'm using Flextable to make pretty tables in a Word doc that gets created in rmarkdown. The tables are all aligned in the center of the doc. I'd like them aligned on the left.
I know body_add_flextable has an align argument, but that function appears to be for inserting a flextable into an existing doc, and not for using within an rmarkdown file creating the doc.
I also have a reference_docx file where I can adjust styles, but tables get inserted as "normal" style, and I can't adjust the table without adjusting everything else in the doc with the "normal" style.
Here's an example rmarkdown file:
---
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
```
```{r cars}
exampleDF <- data.frame("Col1" = c(1:3), "Col2" = c(4:6))
example.flex <- flextable(exampleDF)
example.flex
```
Also, I'm fairly new to using both rmarkdown and Flextable, and this is my first StackOverflow question. So all kinds of apologies if this is a stupid/poorly phrased question.
Dunno if this is a new feature, but you can do this following the online vignette, by adding ft.align = ... to the chunk options.
I think the same holds true for word documents. I am posting this with html as an answer so I can share a screenshot as a result (don't use word) and also the question was not specifically for word (in the title).
---
output: html_document
---
```{r setup, include=FALSE}
library(flextable)
```{r cars, ft.align = "left"}
exampleDF <- data.frame("Col1" = c(1:3), "Col2" = c(4:6))
example.flex <- flextable(exampleDF)
example.flex

How can I change the number of columns several times in an R Markdown pdf document?

I am creating a .pdf using R Markdown. I would like to have a section of text in two column format, and then follow that with a graph (or table, photo, etc.) that takes up the entire width of the page, and then return to two column text. I am new at Markdown / LaTex / Pandoc and I cannot figure out how to do it.
This answer by #AlisonShelton appears to be what I want, but when I run it I get this error in the RStudo R Markdown console:
! Undefined control sequence.
l.87 \btwocol
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
I have successfully used this method by #scoa to make a two column .pdf, but I don't know how go back and forth between one and two columns using this.
Here is some sample code for testing purposes
---
title: "Test"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Two columns of text
This seciton should be in two column format.
Here are a bunch of ? to make it longer: ???????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????
## Once column section.
This part should be the whole page width
```{r plot}
plot(rnorm(20),rnorm(20))
```
## Now 2 columns again
This section should go back to two columns !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
What you can do is first, to add -- as you stated -- pandoc_args: ... into your YAML header. Second, there are a few LaTeX solutions around (like this or this one) which won't work for RMarkdown. The only way I found so far is to use \onyecolumn / \twocolumn -- just with the drawback of the page breaks. But perhaps you can live with it until there's a better solution.
---
title: "Test"
output:
pdf_document:
pandoc_args: [
"-V", "classoption=twocolumn"
]
html_document: default
header-includes:
- \usepackage{lipsum} # just used for producing example text in document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Two columns of text
\lipsum[1-7]
\onecolumn
## Once column section.
This part should be the whole page width
```{r plot}
plot(rnorm(20),rnorm(20))
```
\lipsum[1]
\twocolumn
## Now 2 columns again
This section should go back to two columns
\lipsum
\begin{table*}
This is nice, but won't work with R chunks or headers. And you'll have to format with LaTeX code (e.g. \textbf{Foo blaah}).
\lipsum[1]
\end{table*}
\lipsum

Excluding part of the R markdown (html_notebook) from the final html output

I am writing a relatively long report using R Notebook that makes use of R markdown language which combines text and code in the same document and generates html output.
I would like to be able to exclude some of the analysis (both text and R code) from showing in the final HTML. This is very useful if I want to create two versions of the report - a full/detailed version, as well as a shorter version with main graphs and conclusions.
Obviously, I can create separate Rmd file for each type of report (or comment out pieces of the report that need to be excluded for the shorter version), but I was wondering if there is a more elegant way to do it.
Something like this:
if (Version == "full_text"){
Full analysis goes here
```{r}
R code goes here (could be multiple chunks)
```
}
else {
The shorter version goes here
```{r}
R code goes here
```
}
Place the "detailed" part of the report in a knitr child document which you call optionally from the main document.
Detailed content can then be switched on by calling the child document and it can be switched off by setting the variable child_docs to NULL. For example here are a main and a child document below.
Child document
Save this document under knitr-child.Rmd
---
title: "knitr child"
output: html_document
---
# Details from the child document
Hi, there. I'm a child with a plot and as many details as necessary.
```{r test-child}
plot(cars)
```
Main document - full/detailed version
---
title: "Report"
output: html_document
---
# Summary
```{r setup}
child_docs <- c('knitr-child.Rmd')
# child_docs <- NULL
```
```{r test-main, child = child_docs}
```
# Conclusion
Main document shorter version
---
title: "Report"
output: html_document
---
# Summary
```{r setup}
# child_docs <- c('knitr-child.Rmd')
child_docs <- NULL
```
```{r test-main, child = child_docs}
```
# Conclusion

Setting knitr to output separate images

I'm trying to get the PNG plots from knitr output to write to disk as separate files without doing it manually.
What I tried
The dev = 'png' setting from http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/ (also mentioned in this question)
self_contained: no from knitr: include figures in report *and* output figures to separate files.
Neither worked. The folder the knitting process ran in has no extra files, and the HTML document has base64 embedded images in its source.
Environment
RStudio 0.99.903
R 3.2.3
knitr 1.15.1
MWE: The RStudio RMarkdown file, with abovementioned options added:
---
title: "Untitled"
output: html_document
self_contained: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(dev="png",
dev.args=list(type="cairo"),
dpi=96)
```
## 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.
self_contained is an option for html_document, not a top-level YAML setting. The document below works using just that. PNG is the default figure type, so you don't need to specify that.
---
title: "Untitled"
output:
html_document:
self_contained: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```

Resources