英文:
How can I prepare a reactive dataset for reuse in downstream chunks in a shiny 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 = ", "))
<details>
<summary>英文:</summary>
In the example below I have set up a `selectizeInput()` element which I would like to use to alter `my_data` and return an object that can be used in additional chunks of the Rmd file.
````r
---
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 = ", "))
</details>
# 答案1
**得分**: 0
代码部分不需要翻译,以下是代码以外的内容的翻译:
The filtering needs to happen within a `reactive` consumer while the return value of this consumer can be written to a variable. Downstream use of this variable is then enabled by addressing it via its name and `()`.
标题: "一个闪亮的报告"
运行时: 闪亮(shiny)
输出:
bookdown::html_document2:
目录: true
浮动目录: true
节号: false
图题: true
链接引用: 是
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(shiny)
# 数据
my_data <- mtcars %>%
mutate(name = rownames(mtcars))
```
```{r, results="asis"}
selectizeInput("carsID",
"汽车:",
selected = "Honda Civic",
multiple = TRUE,
choices = my_data$name)
```
### 显示选择
```{r, results="asis"}
renderUI(paste0(input$carsID, collapse = ", "))
```
```{r}
my_data_filt <- reactive({
my_data %>%
filter(name %in% input$carsID)
})
```
### 动态表格
```{r, results="asis"}
renderTable(my_data_filt())
```
<details>
<summary>英文:</summary>
The filtering needs to happen within a `reactive` consumer while the return value of this consumer can be written to a variable. Downstream use of this variable is then enabled by addressing it via its name and `()`.
````r
---
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))
```
```{r, results="asis"}
selectizeInput("carsID",
"Cars:",
selected = "Honda Civic",
multiple = TRUE,
choices = my_data$name)
```
### Show selection
```{r, results="asis"}
renderUI(paste0(input$carsID, collapse = ", "))
```
```{r}
my_data_filt <- reactive({
my_data %>%
filter(name %in% input$carsID)
})
```
### Dynamic table
```{r, results="asis"}
renderTable(my_data_filt())
```
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论