I am using the Xaringan Theme for my RStudio Markdown presentations.
All works fine but I want to change the bibliography style from "authoryear" to the Chicago Manual of Style Author-Date Version.
Does anyone know how to do that?
Here is the code so far:
```{r, load_refs, include=FALSE, cache=FALSE}
library(RefManageR)
BibOptions(check.entries = FALSE,
bib.style = "authoryear",
cite.style = "authoryear",
style = "markdown",
hyperlink = FALSE,
dashed = FALSE)
lit <- ReadBib("pwaLiterature.bib", check = FALSE)
```
Related
this recently came up with the gt:: package but I also remember this from kableExtra as well iirc.
I'm trying to both use the packages title option but also RMarkdowns fig.cap.
Is there a way to enable both or do I have to work around it, for example with {{captioneer}} ?
edit: this question has been solved using gt::gt(caption = "xy")
Next: is it possible to place this caption at the bottom of the table?
Thanks!
---
title: "gt caption"
author: ""
date: "10 5 2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Header
```{r, fig.cap="I also want this fig.cap", echo = FALSE}
tab <- gt::gt(pressure, caption = "xy")
tab <- gt::tab_header(tab,
title = gt::md(
"Title via tab_header"))
tab
```
You could replace the caption argument in gt() with gt::tab_footnote()
tab <- gt::gt(pressure)
tab <- gt::tab_footnote(tab, footnote = "xy")
tab <- gt::tab_header(tab,
title = gt::md(
"Title via tab_header"))
tab
I'm looking for tidy way to add a hyperlink incorporating a fontawesome icon to an Rmarkdown table (kable) — for incorporation in an html bookdown page.
In other parts of my document, I've used the icon package, to render a hyperlinked fontawesome icon (outside of a table) using standard markdown syntax, e.g.:
`r icon::fa("file-pdf", size = 5)](https://www.google.com/){target="_blank"}`
But this approach doesn't work when I've attempted to incorporate it as part of a kable.
```{r}
library(icon)
library(knitr)
library(tidyverse)
## note this code throws the following error: Error in
## as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors =
## stringsAsFactors) : cannot coerce class "c("knit_asis",
## "knit_icon")" to a data.frame
link_location <- "www.google.com"
data_test_1 <- data.frame(
file = c('Version 1', 'Version 2', 'Version 3'),
last_updated = Sys.Date(),
pdf_logo = icon::fa("file-pdf")) %>%
mutate(pdf_logo = cell_spec(pdf_logo,
link = link_location)) %>%
kable("html", escape = F, align = "c")
data_test_1
```
So far I've come up with a workaround that involves downloading the .svg file from the fontawesome website and adding it as an image. It works... sort of, but I would prefer to have the ability to change the size of the icon and make it more easily reproducible.
This is the code for my current workaround.
```{r fontawesome_table ='asis'}
library(tidyverse)
library(kableExtra)
## download svg from location manually
https://fontawesome.com/icons/r-project?style=brands
data_test_2 <- data.frame(
file = c('Version 1', 'Version 2', 'Version 3'),
last_updated = Sys.Date(),
R_logo = "![](r-project-brands.svg)") %>%
mutate(R_logo = cell_spec(R_logo, link = "https://cran.r-
project.org/")) %>%
kable("html", escape = F, align = "c")
data_test_2
```
Which produces this output...
Does anyone have any ideas for how I could either, adjust the size the icon in the table, or call the icon from another package/css to create a more tidy solution?
Here is a way using the fontawesome package instead. I also had to use a custom link building function:
```{r, echo = F, message=F, warning=F}
library(fontawesome)
library(knitr)
library(tidyverse)
library(kableExtra)
## note this code throws the following error: Error in
## as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors =
## stringsAsFactors) : cannot coerce class "c("knit_asis",
## "knit_icon")" to a data.frame
link_location <- "www.google.com"
addLink <- function() {
paste0("", as.character(fa("file-pdf")), "")
}
data_test_1 <- data.frame(file = c('Version 1', 'Version 2', 'Version 3'),
last_updated = Sys.Date(),
pdf_logo = addLink())
kable(data_test_1, escape = F, align = "c")
```
Another solution is the icons package. With icons you cannot only use fontawesome but several icon packages incl. Material icons, e.g.
Install it with devtools::install_github("mitchelloharawild/icons").
Download the font you want to use, here: download_fontawesome().
Now access the icon with either fontawesome("rocket", style = "solid") or icons::fontawesome$solid$rocket.
In an Rmarkdown document like this:
```{r icon-chunk}
fontawesome("rocket", style = "solid")
```
or inline like this
`r icons::fontawesome("rocket", style = "solid")`
In some cases you may need backticks. E.g. for the R icon
fontawesome$brands$`r-project`
How do I evaluate all of the chunks in an Rmd document, without putting eval=TRUE at each chunk? The way I have it below, only the first chunk is evaluated.
```{r,eval=TRUE}
1+1
```
Some text
```
2+2
```
EDIT:
I'm trying to knit/compile to HTML.
```
require(knitr)
opts_chunk$set(eval = TRUE, tidy = TRUE, cache = FALSE, echo = FALSE, include = FALSE,
fig.path = 'figures/', dev = c("pdf"),
fig.width = 7, fig.height = 7)
```
some text
```
1+1
```
more text
```
2+2
```
eval=TRUE is the default behaviour for .Rmd chunks, so you shouldn't need to explicitly add it to your chunks' options.
However, you do need to include {r} after your opening fences in order for the chunk to be recognised as R code and evaluated accordingly. Chunks that do not open with ```{r} will not be run, hence the problem you're seeing.
A working example might be:
```{r}
1+1
```
Some text
```{r}
2+2
```
To insert a new, empty chunk with the appropriate fences and {r}, you can press Ctrl + Alt+i on Windows, or ⌘ + Option + i on Mac, or click this icon at the top right of the RStudio source pane (from memory, older versions of RStudio had an 'Insert' drop-down in that general area):
In your first chunk you can set knitr options globally.
opts_chunk$set(tidy = TRUE, cache = FALSE, echo = FALSE, include = FALSE,
fig.path = 'figures/', dev = c("pdf"),
fig.width = 7, fig.height = 7)
In any subsequent chunk, you can change these by the usual means but they only apply to that chunk.
EDIT. Here is a fuller example from K. Broman
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',
echo=FALSE, warning=FALSE, message=FALSE)
```
I'm trying to create separate Rmd files for different tables where each table gets rendered to separate pdfs. Because of some complex formatting issues, I'm using xtable to try and pull this off. I have some tables that I anticipate will fill up a full 8.5x11.0 page with 1 inch margins. But when I render them the first page of the pdf is blank and the second page has the entire, properly formatted table. I'm looking for help to get this to work right. Here's my minimal workable example...
---
output: pdf_document
tables: true
geometry: margin=1.0in
---
```{r results='asis', echo=FALSE, warning=FALSE, eval=TRUE}
require(xtable)
# set up data frame
df <- data.frame(Label=c(letters[1:26], letters[1:13]), Numbers=1:39)
strCaption <- "\\textbf{Supplementary Table 1. This table is just produced with some random data and does not mean anything.}"
# set up xtable output
print(xtable(df, caption = strCaption, label = ""),
size = "normalsize",
include.rownames = FALSE,
include.colnames = TRUE,
caption.placement = "top",
comment=FALSE
)
```
This gets saved as test.Rd and I render using...
library(rmarkdown)
render("test.Rmd"
If I change the margin sizes, it seems to only affect the left and right margin. If I shrink the size of the font, it will fit on one page, but I'd like to keep the font size as is and get rid of the blank first page. Ideas? I'm a bit of a latex newbie so apologies for missing something obvious.
I think if you add the floating = FALSE argument, it should solve the problem. I think this is the LaTex equivalent of the !h here argument when you define the table. I also separated the library call from the other code (and used library instead of require), but that's stylistic and picky.
---
output: pdf_document
tables: true
geometry: margin=1.0in
---
```{r echo=FALSE, warning=FALSE, eval=TRUE, results='hide'}
library(xtable)
```
```{r eval=TRUE, echo=FALSE, results='asis'}
# set up data frame
df <- data.frame(Label=c(letters[1:26], letters[1:13]), Numbers=1:39)
strCaption <- "\\textbf{Supplementary Table 1. This table is just produced with some random data and does not mean anything.}"
# set up xtable output
print(xtable(df, caption = strCaption, label = ""),
include.rownames = FALSE,
include.colnames = TRUE,
caption.placement = "top",
comment=FALSE,
floating=FALSE
)
```
Thanks to the posting from ted-dallas, I was able to figure out that the table.placement option did what I want without having to turn off the floating...
---
output: pdf_document
tables: true
geometry: margin=1.0in
---
```{r results='asis', echo=FALSE, warning=FALSE, eval=TRUE}
require(xtable)
# set up data frame
df <- data.frame(Label=c(letters[1:26], letters[1:13]), Numbers=1:39)
strCaption <- "\\textbf{Supplementary Table 1. This table is just produced with some random data and does not mean anything.}"
# set up xtable output
print(xtable(df, caption = strCaption, label = ""),
size = "normalsize",
include.rownames = FALSE,
include.colnames = TRUE,
caption.placement = "top",
comment=FALSE,
table.placement = "!ht"
)
```
This generates the output I was looking for.
I have read a bunch of different posts on justifying xtable tables left but I cannot find details/work out how to make the caption justify left as well. Below is a reproducible example which justifies the table left but leaves the caption centred.
\documentclass{article}
\begin{document}
<<echo = F, results = "asis">>=
df = data.frame(x = c(1,2), y = c(4,6))
library(xtable)
print(xtable(df,digits=0, caption="Caption Left?"), include.colnames=TRUE, size = "small", comment=FALSE, latex.environments="flushleft")
#
\end{document}
I have found out how to do this. Simply import the LaTex Caption package and use the caption setup argument:
\captionsetup{justification = raggedright, singlelinecheck = false}
This will justify the caption to the left. The caption can be returned to its default centred position for additional tables or figures by repeating the function with the following modification before additional tables/figures.
\captionsetup{justification = centering, singlelinecheck = false}
The answered solution is:
\documentclass{article}
\usepackage{caption}
\begin{document}
\captionsetup{justification = raggedright, singlelinecheck = false}
<<echo = F, results = "asis">>=
df = data.frame(x = c(1,2), y = c(4,6))
library(xtable)
print(xtable(df,digits=0, caption="Caption Left?"),include.colnames=TRUE, size = "small", comment=FALSE, latex.environments="flushleft")
#
\end{document}
Which returns:
This operation is critical for anyone wishing to produce Markdown files in APA format since table captions are typically left-justified.
I'm going to clarify Robin's answer a little bit because, because totally new to the Markdown/Latex interface, it took me some time to figure out.
Upload the "caption" package (documentation available here) by including the
\usepackage{caption}
command in YAML (header) part of the document, preceded by
header-includes:
So an an entire YAML section at the header of the document might look like this:
---
title: "Supplementary Materials"
author: ""
date: "3/30/2018"
output:
pdf_document: default
editor_options:
chunk_output_type: inline
header-includes:
- \usepackage{caption}
---
Then, at any point in the document, before a code chunk (in its own white space), you can insert the code:
\captionsetup{justification = raggedright, singlelinecheck = false}
To change the settings back again, you can re-insert this code at any point in white space of the Markdown file (not in a code chunk!)
E.g.
\captionsetup{justification = centering, singlelinecheck = false}
If you don't mind using an alternative package (my own):
library(huxtable)
ht <- as_huxtable(df)
caption_pos(ht) <- "bottomleft"
print_latex(ht) # or just do `ht` if within a knitr or Rmd document