【问题标题】:rmarkdown user input to select from a listrmarkdown 用户输入以从列表中选择
【发布时间】: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


    【解决方案1】:

    您可以使用shiny 运行时,它允许创建selectInput 并使用renderPlot 对此输入的更改做出反应:

    ---
    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
    
    runtime: shiny   
       
    #bibliography: bibliography.bib
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    set.seed(1234)
    library(knitr)
    library(kableExtra)
    library(ggplot2)
    library(dplyr)
    library(purrr)
    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)))
    ```
    ```{r}
    selectInput("selector",label = "Selector",
          choices = names(freqsByYear),
          selected = 1)
    
    renderPlot(freqsByYear %>% 
      pluck(., input$selector) %>% 
      ggplot(aes(x = !!sym(input$selector), y = n, fill = n)) +
      geom_col() +
      facet_wrap(~YEAR))
    ```
    

    【讨论】:

    • 谢谢 我试图让闪亮的版本工作,但我做不到,谢谢!一个问题。如果我将 html 文档发送到另一台计算机,选择器仍然可以工作吗?即它不需要在后台运行闪亮的服务器?
    • 这需要在后台运行闪亮的服务器(您自己的 R 会话或专用服务器),see Shiny Docs deployement
    猜你喜欢
    • 2015-08-21
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2016-07-23
    • 2015-12-31
    • 1970-01-01
    • 2017-03-06
    相关资源
    最近更新 更多