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

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

How can I prepare a reactive dataset for reuse in downstream chunks in a shiny Rmd?

问题

以下是您要翻译的内容:

  1. ---
  2. title: "A shiny Report"
  3. runtime: shiny
  4. output:
  5. bookdown::html_document2:
  6. toc: true
  7. toc_float: true
  8. number_sections: false
  9. fig_caption: true
  10. link-citations: yes
  11. ---
  12. ```{r setup, include=FALSE}
  13. knitr::opts_chunk$set(echo = FALSE)
  14. library(tidyverse)
  15. library(shiny)
  16. # data
  17. my_data <- mtcars %>%
  18. mutate(name = rownames(mtcars))
  1. selectizeInput("carsID",
  2. "Cars:",
  3. selected = "Honda Civic",
  4. multiple = TRUE,
  5. choices = my_data$name)

Show selection

  1. renderUI(paste0(input$carsID, collapse = ", "))
  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. ````r
  5. ---
  6. title: &quot;A shiny Report&quot;
  7. runtime: shiny
  8. output:
  9. bookdown::html_document2:
  10. toc: true
  11. toc_float: true
  12. number_sections: false
  13. fig_caption: true
  14. link-citations: yes
  15. ---
  16. ```{r setup, include=FALSE}
  17. knitr::opts_chunk$set(echo = FALSE)
  18. library(tidyverse)
  19. library(shiny)
  20. # data
  21. my_data &lt;- mtcars %&gt;%
  22. mutate(name = rownames(mtcars))
  1. selectizeInput(&quot;carsID&quot;,
  2. &quot;Cars:&quot;,
  3. selected = &quot;Honda Civic&quot;,
  4. multiple = TRUE,
  5. choices = my_data$name)

Show selection

  1. renderUI(paste0(input$carsID, collapse = &quot;, &quot;))
  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. 代码部分不需要翻译,以下是代码以外的内容的翻译:
  5. 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 `()`.
  6. 标题: "一个闪亮的报告"
  7. 运行时: 闪亮(shiny)
  8. 输出:
  9. bookdown::html_document2:
  10. 目录: true
  11. 浮动目录: true
  12. 节号: false
  13. 图题: true
  14. 链接引用: 是
  15. ```{r setup, include=FALSE}
  16. knitr::opts_chunk$set(echo = FALSE)
  17. library(tidyverse)
  18. library(shiny)
  19. # 数据
  20. my_data &lt;- mtcars %&gt;%
  21. mutate(name = rownames(mtcars))
  22. ```
  23. ```{r, results=&quot;asis&quot;}
  24. selectizeInput(&quot;carsID&quot;,
  25. &quot;汽车:&quot;,
  26. selected = &quot;Honda Civic&quot;,
  27. multiple = TRUE,
  28. choices = my_data$name)
  29. ```
  30. ### 显示选择
  31. ```{r, results=&quot;asis&quot;}
  32. renderUI(paste0(input$carsID, collapse = &quot;, &quot;))
  33. ```
  34. ```{r}
  35. my_data_filt &lt;- reactive({
  36. my_data %&gt;%
  37. filter(name %in% input$carsID)
  38. })
  39. ```
  40. ### 动态表格
  41. ```{r, results=&quot;asis&quot;}
  42. renderTable(my_data_filt())
  43. ```
  44. <details>
  45. <summary>英文:</summary>
  46. 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 `()`.
  47. ````r
  48. ---
  49. title: &quot;A shiny Report&quot;
  50. runtime: shiny
  51. output:
  52. bookdown::html_document2:
  53. toc: true
  54. toc_float: true
  55. number_sections: false
  56. fig_caption: true
  57. link-citations: yes
  58. ---
  59. ```{r setup, include=FALSE}
  60. knitr::opts_chunk$set(echo = FALSE)
  61. library(tidyverse)
  62. library(shiny)
  63. # data
  64. my_data &lt;- mtcars %&gt;%
  65. mutate(name = rownames(mtcars))
  66. ```
  67. ```{r, results=&quot;asis&quot;}
  68. selectizeInput(&quot;carsID&quot;,
  69. &quot;Cars:&quot;,
  70. selected = &quot;Honda Civic&quot;,
  71. multiple = TRUE,
  72. choices = my_data$name)
  73. ```
  74. ### Show selection
  75. ```{r, results=&quot;asis&quot;}
  76. renderUI(paste0(input$carsID, collapse = &quot;, &quot;))
  77. ```
  78. ```{r}
  79. my_data_filt &lt;- reactive({
  80. my_data %&gt;%
  81. filter(name %in% input$carsID)
  82. })
  83. ```
  84. ### Dynamic table
  85. ```{r, results=&quot;asis&quot;}
  86. renderTable(my_data_filt())
  87. ```

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:

确定