【发布时间】:2021-06-13 14:11:30
【问题描述】:
我正在尝试生成 RMarkdown 文档。我有一个列表freqsByYear,我希望用户从下拉菜单(或一些类似的方法)中进行选择,这将从这里存储为Q 我可以将它传递给ggplot 函数并制作如下图。
Q = "FNB1"
freqsByYear %>%
pluck(., Q) %>%
ggplot(aes(x = !!sym(Q), y = n, fill = n)) +
geom_col() +
facet_wrap(~YEAR)
但是,我不确定如何允许用户选择 Q 以使其更新。由于所有内容都已存储在列表中,因此无需在后台进行计算,因此只需用户选择要显示的绘图。
降价:
---
title: "title"
author: Name
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
theme: flatly # default, cerulean, journal, flatly, darkly, readable, spacelab, united, cosmo, lumen, paper, sandstone, simplex, and yeti
highlight: tango # default, tango, pygments, kate, monochrome, espresso, zenburn, haddock, breezedark, and textmate
smart: true
toc: true
toc_depth: 2
toc_float:
collapsed: false
smooth_scroll: false
numbers_sections: true
fig_width: 7
fig_height: 6
fig_caption: true
df_print: paged
code_folding: hide
#bibliography: bibliography.bib
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
set.seed(1234)
library(knitr)
library(kableExtra)
```
```{r}
Q = "AA1"
freqsByYear %>%
pluck(., Q) %>%
ggplot(aes(x = !!sym(Q), y = n, fill = n)) +
geom_col() +
facet_wrap(~YEAR)
```
数据:
freqsByYear <- list(LG1 = structure(list(YEAR = c(1L, 1L, 1L, 1L, 2L, 2L, 2L
), LG1 = c(2L, 3L, 4L, 5L, 3L, 4L, 5L), n = c(1L, 26L, 64L, 25L,
13L, 33L, 36L)), class = "data.frame", row.names = c(NA, -7L)),
AA1 = structure(list(YEAR = c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L), AA1 = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L), n = c(31L,
44L, 30L, 11L, 28L, 30L, 15L, 8L, 1L)), class = "data.frame", row.names = c(NA,
-9L)), FNB1 = structure(list(YEAR = c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L), FNB1 = c(2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L), n = c(16L, 12L, 68L, 20L, 1L, 2L, 9L, 35L, 35L)), class = "data.frame", row.names = c(NA,
-9L)), RE1 = structure(list(YEAR = c(1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L), RE1 = c(1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L), n = c(24L, 58L, 26L, 7L, 1L, 24L, 29L, 23L,
5L, 1L)), class = "data.frame", row.names = c(NA, -10L)))
编辑:
不发光的“解决方案”
我创建了一个新的标题如下:
## My header {.tabset .tabset-dropdown}
然后在列表中创建 ggplots。
```{r}
plots <- vars %>%
map(., ~freqsByAge %>%
pluck(., .x) %>%
ggplot(aes(x = !!sym(.x), y = n, fill = n)) +
geom_col() +
facet_wrap(~AGE)
...
```
现在我们可以调用列表了
### myPlot1
```{r, echo = FALSE}
plots[[1]]
```
### myPlot2
```{r, echo = FALSE}
plots[[2]]
```
由于我们之前设置了{.tabset .tabset-dropdown},所有子标题都将通过下拉菜单访问。用户现在可以选择他们想要显示的情节。
【问题讨论】:
标签: r shiny r-markdown