如何为在Shiny Rmd中下游块中重复使用的响应式数据集做准备?

huangapple go评论60阅读模式
英文:

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: &quot;A shiny Report&quot;
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 &lt;- mtcars %&gt;%
  mutate(name = rownames(mtcars))
selectizeInput(&quot;carsID&quot;,
               &quot;Cars:&quot;,
               selected = &quot;Honda Civic&quot;,
               multiple = TRUE,
               choices = my_data$name)

Show selection

renderUI(paste0(input$carsID, collapse = &quot;, &quot;))

</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 &lt;- mtcars %&gt;%
  mutate(name = rownames(mtcars))
```

```{r, results=&quot;asis&quot;}
selectizeInput(&quot;carsID&quot;,
               &quot;汽车:&quot;,
               selected = &quot;Honda Civic&quot;,
               multiple = TRUE,
               choices = my_data$name)
```

### 显示选择
```{r, results=&quot;asis&quot;}
renderUI(paste0(input$carsID, collapse = &quot;, &quot;))
```

```{r}
my_data_filt &lt;- reactive({
  my_data %&gt;%
    filter(name %in% input$carsID)
})
```

### 动态表格
```{r, results=&quot;asis&quot;}
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: &quot;A shiny Report&quot;
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 &lt;- mtcars %&gt;%
  mutate(name = rownames(mtcars))
```

```{r, results=&quot;asis&quot;}
selectizeInput(&quot;carsID&quot;,
               &quot;Cars:&quot;,
               selected = &quot;Honda Civic&quot;,
               multiple = TRUE,
               choices = my_data$name)
```

### Show selection
```{r, results=&quot;asis&quot;}
renderUI(paste0(input$carsID, collapse = &quot;, &quot;))
```

```{r}
my_data_filt &lt;- reactive({
  my_data %&gt;%
    filter(name %in% input$carsID)
})
```

### Dynamic table
```{r, results=&quot;asis&quot;}
renderTable(my_data_filt())
```

huangapple
  • 本文由 发表于 2023年3月31日 19:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定