R markdown hangs, but all code chunks work - r-markdown

R markdown gets stuck on chunk 1, which is repeated below:
---
title: "RMark1"
author: "df"
date: "11/19/2017"
output:
html_document:
toc: true
---
```{r setup}
library(dplyr)
library(knitr)
library(rmarkdown)
library(ggplot2)
library(plotly)
library(readr)
knitr::opts_chunk$set(echo = TRUE)
print("loaded libraries")
```
what am I missing here?

Related

adding numbers to code blocks in RMD output

I am trying to add line numbers to code blocks, but it's not working. I'm using the code attr.source='.numberLines' with Pandoc 2.11.4. but the output files omit the code numbers.
I've tried the options referenced in this question and also the documentation from bookdown, but the attr.source='.numberLines' is being ignored and it doesn't show up as an autofill option, like say echo= would.
What I want is something that looks like this (just the number part)
Thanks! I've tried this in two different applications of R studio, and it's not working in either.
An example of the code I'm using:
---
title: "example"
author: "plover"
date: '2022-12-29. Version: `r Sys.Date()`'
output:
html_document:
code_folding: show
highlight: tango
number_sections: false
df_print: kable
theme: flatly
toc: yes
toc_float: yes
toc_depth: '3'
word_document:
toc: no
pdf_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Don't evaluate this code
```{r abc, eval = FALSE }
a <-1+1
b <-2+1
c = a + b
```
Don't evaluate this code, but show me the lines
```{r def, eval = FALSE, attr.source = '.numberLines' }
d <-3+1
e <-4+2
f = d + e
```
Evaluate this code, and show me the lines
```{r ghi, attr.source = '.numberLines' }
g <-3+1
h <-4+2
i = g + h
```
Try this. (Actually did nothing but comment out the yaml option code_folding: show)
---
title: "example"
author: "plover"
date: '2022-12-29. Version: `r Sys.Date()`'
output:
pdf_document:
toc: yes
word_document:
toc: no
html_document:
# code_folding: show
highlight: tango
number_sections: false
df_print: kable
theme: flatly
toc: yes
toc_float: yes
toc_depth: '3'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Don't evaluate this code
```{r abc, eval = FALSE }
a <-1+1
b <-2+1
c = a + b
```
Don't evaluate this code, but show me the lines
```{r def, eval = FALSE, attr.source=".numberLines"}
d <-3+1
e <-4+2
f = d + e
```
Evaluate this code, and show me the lines
```{r ghi, eval=FALSE, attr.source=".numberLines"}
g <-3+1
h <-4+2
i = g + h
```

How to print special Latin characters in Markdown pdfs

I need to knit my Markdown script into a pdf file, but when knitted, the pdf file does not show special Latin characters, such as those used in the International Phonetic Alphabet (IPA).
---
title: "MarkdownIPA"
date: '2022-07-06'
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
`Print IPA`
```{r}
'[aɪ.pʰiː.ɛɪ]'
```
Which rends:
How can I fix this?
You'll need to use a font that supports those glyphs. A (IMHO) rather nice one is Source Sans Pro and the monospace variant Source Code Pro.
To use Source Code Pro, you can add the respective LaTeX code directly:
---
title: "MarkdownIPA"
date: '2022-07-06'
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{sourcecodepro}
---
Result:
The sourcecodepro package is likely already installed; otherwise you'd have to install it first.

Flextable label not created in rmarkdown

I am using flextable to produce tables for a pdf document rendered with rmarkdwown. I want to cross-reference the tables, but they do not produce the label needed for the cross reference to work.
My Minimum Reproducible Example
What follows is the content of the file test.Rmd:
---
title: "document title"
author: "author here"
date: "2022-01-20"
output:
pdf_document:
fig_caption: yes
includes:
in_header: header.tex
number_sections: yes
toc: yes
toc_depth: 4
latex_engine: xelatex
keep_tex: true
---
```{r, include = FALSE}
library(flextable)
library(bookdown)
opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, out.width = "85%", fig.align = "center")
```
```{r, tab.id = "anyTable", tab.cap = "Invented data", tab.lp = "tab:"}
a = data.frame(id = LETTERS[1:3], x = 1:3)
flextable(a) |> theme_vanilla()
```
```{r secondTable}
b = data.frame(id = LETTERS[4:6], x = 4:6)
flextable(b) |> theme_vanilla() |> set_caption(caption = "This is secondTable")
```
# Standard rmarkdown crossreference
Trying anyTable: \ref{tab:anyTable}.
Trying secondTable: \ref{tab:secondTable}.
# Bookdown crossreference
Trying anyTable: \#ref(tab:anyTable).
Trying secondTable: \#ref(tab:secondTable).
Contents of header.tex:
\usepackage{caption}
\renewcommand{\contentsname}{Contenidos}
\captionsetup[table]{name=Tabla}
\captionsetup[figure]{name=Figura}
%\usepackage{float} #use the 'float' package
%\floatplacement{figure}{H}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{xcolor}
\usepackage{multicol}
\usepackage{pdflscape}
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}
When I do rmarkdown::render("test.Rmd", output_file = "test.pdf") the file is rendered in pdf, but the following warning comes out:
Output created: test.pdf
Warning message:
LaTeX Warning: Reference `tab:anyTable' on page 1 undefined on input line 198.
LaTeX Warning: Reference `tab:secondTable' on page 1 undefined on input line 20
0.
LaTeX Warning: There were undefined references.
If I check the .tex file, I can see that the captions are there, but they do not have the labels. The relevant lines of the intermediate test.tex file:
lines 106 to 109
\begin{longtable}[c]{|p{0.75in}|p{0.75in}}
\caption{Invented data
}\\
lines 153 to 156
\begin{longtable}[c]{|p{0.75in}|p{0.75in}}
\caption{This is secondTable
}\\
I expected that the caption line in the tex file was something like \caption{This is secondTable}\label{tab:secondTable}\\
What I've tried
Changing latex engines (lualatex, pdflatex, xelatex).
Editing the tex file (adding the label after the caption), but it fails to convert to dvi with multiple errors (mostly "undefined control sequence")
What am I doing wrong?
How can I cross-reference the tables produced by flextable?
Alternatively, I am open to other packages you may suggest. As some of the headers in my real data are somewhat complex (multi-span headers and the like), I would like to steer away from kable and kableExtra if at all possible.
You need to use a format from bookdown to get cross references, see https://bookdown.org/yihui/rmarkdown-cookbook/cross-ref.html
---
title: "document title"
author: "author here"
date: "2022-01-20"
output:
bookdown::pdf_document2:
fig_caption: yes
number_sections: yes
toc: yes
toc_depth: 4
latex_engine: xelatex
keep_tex: true
---
```{r, include = FALSE}
library(flextable)
library(knitr)
library(bookdown)
opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, out.width = "85%", fig.align = "center")
```
```{r, tab.id = "anyTable", tab.cap = "Invented data"}
a = data.frame(id = LETTERS[1:3], x = 1:3)
flextable(a) |> theme_vanilla()
```
```{r secondTable}
b = data.frame(id = LETTERS[4:6], x = 4:6)
flextable(b) |> theme_vanilla() |> set_caption(caption = "This is secondTable")
```
# Bookdown crossreference
Trying anyTable: \#ref(tab:anyTable).
Trying secondTable: \#ref(tab:secondTable).

Rmarkdown with html, how to reference figures?

Using RMarkdown I always generate pdf documents via Rmd -> pandoc -> TeX -> pdflatex -> pdf and accomplish figure references using \\label{something} in the fig.cap as in the follow example:
---
title: "Foo"
author: "Mark Andrews"
date: "10 January 2019"
output: pdf_document
---
See Figure \ref{figfoo} and see Figure \ref{figbar}.
```{r fig1, fig.cap='A figure\\label{figfoo}'}
plot(rnorm(10))
```
```{r fig2, fig.cap='Another figure\\label{figbar}'}
plot(rnorm(10))
```
If I change output: pdf_document to output: html_document, that does not work, understandably because it is relying on LaTeX's cross referencing system.
So how do Figure references work with html_document in RMarkdown?
The following does not work:
---
title: "Foo"
author: "Mark Andrews"
date: "10 January 2019"
output: html_document
---
See Figure \#ref(fig:fig1) and see Figure \#ref(fig:fig2).
```{r fig1, fig.cap='A figure}'}
plot(rnorm(10))
```
```{r fig2, fig.cap='Another figure'}
plot(rnorm(10))
```
But the following does work:
---
title: "Foo"
author: "Mark Andrews"
date: "10 January 2019"
output: bookdown::html_document2
---
See Figure \#ref(fig:fig1) and see Figure \#ref(fig:fig2).
```{r fig1, fig.cap='A figure}'}
plot(rnorm(10))
```
```{r fig2, fig.cap='Another figure'}
plot(rnorm(10))
```
Does that mean that the only way to cross-reference figures when producing html from Rmarkdown is to use output: bookdown::html_document2. That's fine, if so, but am I missing something?
Having heard from Yihui Xie, I think we can take it for granted that yes, the only way to do figure cross references in html_document in rmarkdown is to do
---
output: bookdown::html_document2
---
in the header.

Running code chunk in R-markdown using exam document class

I would like to run a code chunk under a question level in the exam document class, but I keep receiving errors. I am assuming this is because it believes the output from the R-code is Latex code.
---
output: pdf_document
documentclass: exam
header-includes: \usepackage{float}
---
\begin{questions}
\question Answer question...
```{r}
iris%>%
group_by(Species)%>%
summarize(Total=n())
```
\end{questions}
Sometimes \begin{"some environment") ... \end{"some environment") doesn't play well with R chuncks. One work around is to define a new environment.
For example, I defined a file preamble.tex with the following information:
preamble.tex
\usepackage{float}
\newcommand{\bQ}{\begin{questions}}
\newcommand{\eQ}{\end{questions}}
Then, I ran the following.
exam.Rmd
---
documentclass: exam
geometry: margin=.5in
output:
pdf_document:
highlight: haddock
includes:
in_header: preamble.tex
before_body: doc-prefix.tex
after_body: doc-suffix.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\bQ
## Including Plots
\question You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r cars}
summary(cars)
```
\question We can keep the pound signs.
\eQ
Here is the resulting output.
Output
I managed to get something working. It needs some libraries and uses knitr to create the output.
---
output:
pdf_document:
keep_tex: true
documentclass: exam
header-includes: \usepackage{float}
---
```{r setup, include=TRUE,echo=FALSE,message=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(knitr)
```
\begin{questions}
\question Answer question...
```{r, echo=F, comment=NA}
summary = iris %>% group_by(Species) %>% summarize(Total=n())
kable(summary, format='latex')
```
\end{questions}
The problem seems to be the inclusion of # in the output from R for processing by Latex. I avoid this by using kable.