I use dynamic values inside RMarkdown to insert numbers inside my documents. Often I need to print a number contained inside a data.frame. I would like to know what the most efficient way to do this is (I have tried tidyverse and which
with limited success). Below I have an example.
df <- data.frame(
exclusion = c("Conference", "English"),
n = c(196, 310)
)
In the document I would use some version of the backtick to get those numbers but I am having limited success.
If I use the following tidyverse code, it will return the column name "n" alongside the number "196" but I don't want the column name to be displayed.
df %>% filter(exclusion=="Conference") %>% select(n)
Below is an example of the code I would use in the RMD document.
A total of `r` articles were excluded with n=`r df %>% filter(exclusion==Conference")` for conferences and n=____ for not being in English
Instead of selecting, you need to pull
. And I think it would be neater to define a function for your case.
---
title: ""
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r include=FALSE}
library(dplyr, quietly = TRUE)
df <- data.frame(
exclusion = c("Conference", "English"),
n = c(196, 310)
)
pull_n <- function(x) df %>% filter(exclusion == x) %>% pull(n)
```
A total of `r pull_n("Conference") + pull_n("English")` articles were
excluded with n = `r pull_n("Conference")` for conferences and
n = `r pull_n("English")` for not being in English.