Insert list of figures / tables using the captioner package (R) - r

I use the package captioner to automatically reference tables, figures, supplementary data, codes, etc in my normal work. Althoug I can use it efficiently, I would like to create a section in the RMarkdown document which contains the list (index) of all the elements of the same style.
Let's say I have 2 tables in a RMarkdown document.
---
title: "Untitled"
author: "Mario Modesto Mata"
date: "6/7/2020"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
library(captioner)
table_number <- captioner(prefix = "Table")
```
The data frame of mtcars is in `r table_number("table1", display="cite")`.
```{r}
data(mtcars)
print(mtcars)
```
`r table_number(name = "table1", caption = "Caption of table1 (mtcars)")`
The data frame of mtcars is in `r table_number("table2", display="cite")`.
```{r}
data(iris)
print(iris)
```
`r table_number(name = "table2", caption = "Caption of table2 (iris)")`
I would like to include at the beginning an index of the tables that were defined with the function table_number().
Is there a way to do this? I would prepare later the equivalent index for figures and other topics...

Related

When I knit a flextable to html there is no output with the table

The table prints nicely in markdown but is not present in the knitted html file. I noticed that it is classified as a list but don't know how to change it to an acceptable file type. The knitted output is not formatted as a table. I appreciate the help.
library("crosstable") #important package crosstable() function
library('dplyr')
library("flextable")
tbl1 = crosstable(mtcars2, c(1), by = 2) %>%
as_flextable(keep_id=FALSE)
print(tbl1)
According to ?print.flextable
Note also that a print method is used when flextable are used within R markdown documents. See knit_print.flextable().
Therefore, if we want to print in Rmarkdown, either use knitr::knit_print or remove the print as the ?knit_print.flextable documentation shows
You should not call this method directly. This function is used by the knitr package to automatically display a flextable in an "R Markdown" document from a chunk.
---
title: "Testing"
author: "akrun"
date: "09/12/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo = FALSE}
suppressPackageStartupMessages(library("crosstable")) #important package crosstable() function
suppressPackageStartupMessages(library('dplyr'))
suppressPackageStartupMessages(library("flextable"))
tbl1 = crosstable(mtcars2, c(1), by = 2) %>%
as_flextable(keep_id=FALSE)
# either use knit_print or remove the print wrapper
#knitr::knit_print(tbl1)
tbl1
```
-output

Cross-referencing in R bookdown makes the figure name incorrect

I am producing plotly charts in Rmarkdown and give them name and caption. but as soon as I cross reference them in the document, their name is ruined.
Here is the code:
---
title: "Preliminary Statistics"
author: "Babak"
date: "6/28/2021"
output:
bookdown::html_document2:
fig_caption: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
fig.width=9,
fig.height=5 ,
message=FALSE,
warning=FALSE)
```
```{r Area_duplicate, fig.cap = "Area of duplicated and unique properties by municipality"}
<PLOT CODES>
```
Plot \#ref(fig:Area_duplicate) shows the area od duplicated properties in the dataset grouped by municipality.
result:
screenshot of the caption
What should I do to resolve this issue?
Replace the underscore in the name with a "-".

Is there a way to add latex specification in 'column_spec' function in R kable?

I am trying to modify the appearance of a table using kable and kableExtra packages in R Markdown knitting to PDF output.
In particular, I would like to remove all borders from the table and add only one horizontal line under the header and one vertical line next to the row names.
At the moment, I have this code:
---
header-includes: \setmainfont[Path = C:/Windows/Fonts/]{Arial}
\usepackage{colortbl}
\arrayrulecolor{white}
output:
pdf_document:
latex_engine: xelatex
word_document: default
---
```{r echo=FALSE, message=FALSE, warning=F, paged.print=TRUE}
library(kableExtra)
library(magrittr)
DATA<- data.frame(jen=c(1,2,3,4,5), feb=c(2,3,4,5,3), mar=c(0,2,4,1,2))
rownames(DATA)<-c("first","second","third","fourth","fifth")
kable(DATA, "latex")
```
Which generates this table:
Table
To delete all borders, I set default borders to be white as the answer to this question suggested.
Now, I would like to add an horizontal blue line under the header and a vertical blue line at the right side of the row names.
I can manage to get the horizontal line with the following:
---
header-includes: \setmainfont[Path = C:/Windows/Fonts/]{Arial}
\usepackage{colortbl}
\arrayrulecolor{white}
output:
pdf_document:
latex_engine: xelatex
---
```{r echo=FALSE, message=FALSE, warning=F, paged.print=TRUE}
library(kableExtra)
library(magrittr)
DATA<- data.frame(jen=c(1,2,3,4,5), feb=c(2,3,4,5,3), mar=c(0,2,4,1,2))
rownames(DATA)<-c("first","second","third","fourth","fifth")
kable(DATA, "latex") %>%
row_spec(0, extra_latex_after = "\\arrayrulecolor[rgb]{0,.275,.725}") %>%
row_spec(1:nrow(DATA), extra_latex_after = "\\arrayrulecolor{white}")
```
Obtaining this.
I would like to do the same with a vertical line but there is no such command as 'extra_latex_after' in the function 'column_spec'. It only accepts 'extra_css' commands which obviously do not work for a PDF output.
My goal is to get something like this.
I know I can get the same result with other packages for tables, but I am wondering if it is possible to get something like this using kable.
Does anyone know if there is a solution for this?
This is indeed not supported so far. You could make a feature request on github.
You can hack this using a regular expression to replace the alignment options in your tabular:
---
title: "Test"
date: 2019-02-13
output: pdf_document
---
```{r header, echo= FALSE, include = T, warning=F}
library(knitr)
tbl <- kable(mtcars[1:4, 1:3], format = "latex")
# here we search for the begin command and its options and replace them
gsub(pattern = "(begin\\{tabular\\})(\\{.*?\\})",
repl = "\\1{l|r !{\\\\color{red}\\\\vrule width 1pt} r|r|}",
x = tbl)
```

rMarkdown trouble loading csv in R

I am trying to create an R markdown using the csv NFL_DATA.csv. However when I try to use knitr I get the error:
Error in nrow(NFL_DATA): object 'NFL_DATA' not found Calls:
... in_dir -> inline_exec -> withVisible -> eval -> eval ->
nrow Execution halted
Here is the beginning of my code:
title: "XXXXXX"
author: "XXXXXX"
date: "December 11, 2017"
output: html_document
font-family: "Arial"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
---
## Introduction
XXXXXXXXXXXXX.
---
## Data
We used the site data.world.com to find my data. The dataset can be retrieved at [NFL_DATA.csv](https://data.world/alice-c/nfl-fines-and-suspensions/workspace/file?filename=All+Penalties.csv).
There are `r nrow(NFL_DATA)` observations in the NFL data set and `r length(NFL_DATA)` variables. The variables are:
```{r echo=FALSE, comment=""}
names(NFL_DATA)
```
When I run it the knitr does not work and gives an error message. However when I just run the last piece of code for names(NFL_DATA) it does work and it displays all the variable names. How can I resolve this?
Try this after editing the code to point to wherever you stored the csv file locally.
---
title: "NFL Fines and Suspensions Analysis"
author: "anAuthor"
date: "12/8/2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Read Data
Here is example code to read the nfl data
```{r readData,echo=FALSE}
nfldata <- read.csv("./data/All Penalties.csv",header=TRUE)
```
There are `r nrow(nfldata)` observations in the NFL data set and `r length(nfldata)` variables. The variables are:
```{r}
names(nfldata)
```

How to generate headers functionally?

How can I generate a narrative functionally in RMarkdown? For instance, say I want to generate 3 headers like
##First
##Second
#Third
from a vector c('First', 'Second', 'Third'), then knit to pdf
If you're trying to knit to pdf, this solution calls on LaTeX within an R code chunk to create the headers as you requested. Just add this into an .Rmd file:
---
title: "Untitled"
author: "Ryan Runge"
date: "8/30/2017"
output: pdf_document
---
```{r eval=TRUE, results='asis' }
headers <- c('First', 'Second', 'Third')
cat(paste("\\section{",headers,"}"))
```
The output is:
And if you don't want the R code chunk to show in the pdf, just supply echo=FALSE in the code chunk options.
You can reference a pre-defined list of headers in RMarkdown as follows:
```{r}
list_of_headers <- c("Title 1", "Title 2", "Title 3")
```
This is an example.
### `r list_of_headers[1]`
Stuff under the first header
### `r list_of_headers[2]`
Etc
### `r list_of_headers[3]`

Resources