I'm looking to access a value from a tibble using inline code in an R Markdown document.
The following code gives me a 1 x 2 tibble.
#library(tidyverse)
#library(knitr)
# Minimal working example
df <- tibble(
Event = c("Swimming","Swimming","Swimming","Camping","Hiking","Hiking")
)
df
# Creates 1 row x 2 column
df %>% count(Event) %>% slice(which.max(n))
If I create an object in a code chunk, I can select [1,1] and report that inline:
temp <- df %>% count(Event) %>% slice(which.max(n))
# The most popular event was `r temp[1,1]`.
# which becomes "The most popular event was Swimming."
Is there a way to do that within my dplyr pipes without creating a new object? Ideally, I would be able to do it all via inline code, something like:
`r df %>% count(Event) %>% slice(which.max(n)) %>% df[1,1]` # fails
Stepping back, I'm looking for a generalizable technique to use to select particular rows and cells from within a series of dplyr commands without creating new objects along the way.
r df %>% count(Event) %>% slice(which.max(n)) %>% {.[1,1]}. Well, if you include this in a sentence, you might want to add%>% tolower