如何准备一个React式数据集,以便在一个出色的Rmd中的下游块中重用?

qf9go6mv  于 2023-04-03  发布在  React
关注(0)|答案(1)|浏览(107)

在下面的例子中,我设置了一个selectizeInput()元素,我想用它来改变my_data并返回一个可以在Rmd文件的其他块中使用的对象。

---
title: "A shiny Report"
runtime: shiny
output:
  bookdown::html_document2:
    toc: true
    toc_float: true
    number_sections: false
    fig_caption: true
link-citations: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(tidyverse)
library(shiny)

# data
my_data <- mtcars %>%
  mutate(name = rownames(mtcars))
selectizeInput("carsID",
               "Cars:",
               selected = "Honda Civic",
               multiple = TRUE,
               choices = my_data$name)

Show selection

renderUI(paste0(input$carsID, collapse = ", "))
abithluo

abithluo1#

过滤需要在reactive消费者中进行,而该消费者的返回值可以写入变量。然后通过其名称和()对其进行寻址,从而启用该变量的下游使用。

---
title: "A shiny Report"
runtime: shiny
output:
  bookdown::html_document2:
    toc: true
    toc_float: true
    number_sections: false
    fig_caption: true
link-citations: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(tidyverse)
library(shiny)

# data
my_data <- mtcars %>%
  mutate(name = rownames(mtcars))
selectizeInput("carsID",
               "Cars:",
               selected = "Honda Civic",
               multiple = TRUE,
               choices = my_data$name)

Show selection

renderUI(paste0(input$carsID, collapse = ", "))
my_data_filt <- reactive({
  my_data %>%
    filter(name %in% input$carsID)
})

Dynamic table

renderTable(my_data_filt())

相关问题