Shiny: 过滤功能不太好用,下载功能也有问题。

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

Shiny: Filtering not working as well as download

问题

我有一个非常简单的应用程序,其中包含一个包含一个分组变量和一个值列的数据框。默认情况下,该应用程序应该显示未经过滤的数据。用户应该能够对数据框进行筛选,并将筛选后的数据框保存/下载为CSV/Excel文件。筛选和下载功能不起作用。我有以下代码:

  1. library(shiny)
  2. library(shinyWidgets)
  3. library(tidyverse)
  4. library(DT)
  5. state <- c("CA", "CA", "PA", "PA", "PA", "CA")
  6. total <- c(100, 111, 120, 130, 11, 200)
  7. dataf <- data.frame(state, total)
  8. ui <- fluidPage(
  9. sidebarLayout(
  10. sidebarPanel(
  11. h2("Output data"),
  12. selectInput(inputId = "state", label = "State",
  13. choices = c("All", "CA", "PA"),
  14. selected = "All"),
  15. sliderInput("total", "Total Number",
  16. value = c(mean(dataf$total, na.rm = TRUE), max(dataf$total, na.rm = TRUE)),
  17. min = min(dataf$total, na.rm = TRUE),
  18. max = max(dataf$total, na.rm = TRUE)),
  19. downloadButton(outputId = "download_data", label = "Download"),
  20. ),
  21. mainPanel(
  22. DT::dataTableOutput(outputId = "table")
  23. )
  24. )
  25. )
  26. server <- function(input, output, session) {
  27. df <- reactive({
  28. if (input$state == "All") {
  29. dataf
  30. } else {
  31. dataf %>% filter(state == input$state)
  32. }
  33. })
  34. output$table <- renderDataTable(
  35. df()
  36. )
  37. }
  38. output$download_data <- downloadHandler(
  39. filename = "download_data.csv",
  40. content = function(file) {
  41. write.csv(df(), file, row.names = FALSE)
  42. }
  43. )
  44. shinyApp(ui, server)

这是一个修复后的代码,现在应该能够正常工作。主要的更改包括:

  1. ui中的selectInput中更正了inputId,将"label"更正为"label"。

  2. server中使用了响应式函数df()来处理数据筛选,如果选择了"All",则显示全部数据,否则根据选择的状态进行筛选。

请尝试这个修复后的代码,看看是否满足您的需求。希望这能帮助您解决问题。

英文:

I have this very simple app where I have a data frame containing one grouping variable and a value column. By default, the app should display the data without filtering. The user should be able to filter the dataframe and save/download the filtered data frame in a csv/excel file. The filtering and download isn't working. I have the following code,

  1. library(shiny)
  2. library(shinyWidgets)
  3. library(tidyverse)
  4. library(DT)
  5. state &lt;- c(&quot;CA&quot;, &quot;CA&quot;, &quot;PA&quot;, &quot;PA&quot;, &quot;PA&quot;, &quot;CA&quot;)
  6. total &lt;- c(100, 111, 120, 130, 11, 200)
  7. dataf &lt;- data.frame(state, total)
  8. ui &lt;- fluidPage(
  9. sidebarLayout(
  10. sidebarPanel(
  11. h2(&quot;Output data&quot;),
  12. selectInput(inputId = &quot;state&quot;, label = &quot;Stat&quot;,
  13. choices = c(&quot;All&quot;, &quot;CA&quot;, &quot;PA&quot;),
  14. selected = &quot;All&quot;),
  15. sliderInput(&quot;total&quot;, &quot;Total Number&quot;,
  16. value = c(mean(dataf$total, na.rm = TRUE), max(dataf$total, na.rm = TRUE)),
  17. min = min(dataf$total, na.rm = TRUE),
  18. max = max(dataf$total, na.rm = TRUE)),
  19. downloadButton(outputId = &quot;download_data&quot;, label = &quot;Download&quot;),
  20. ),
  21. mainPanel(
  22. DT::dataTableOutput(outputId = &quot;table&quot;)
  23. )
  24. )
  25. )
  26. server &lt;- function(input, output, session) {
  27. df &lt;- reactive({
  28. dataf %&gt;%
  29. filter(total == input$total)
  30. })
  31. output$table &lt;- renderDataTable(
  32. dataf
  33. )
  34. }
  35. output$download_data &lt;- downloadHandler(
  36. filename = &quot;download_data.csv&quot;,
  37. content = function(file) {
  38. write.csv(df, file, row.names = FALSE)
  39. }
  40. )
  41. shinyApp(ui, server)

A little guide to solve the problems would be greately appreciated.

答案1

得分: 1

以下是代码的翻译部分:

  1. library(shiny)
  2. library(shinyWidgets)
  3. library(tidyverse)
  4. library(DT)
  5. state <- c("CA", "CA", "PA", "PA", "PA", "CA")
  6. total <- c(100, 111, 120, 130, 11, 200)
  7. dataf <- data.frame(state, total)
  8. ui <- fluidPage(
  9. sidebarLayout(
  10. sidebarPanel(
  11. h2("Output data"),
  12. selectInput(inputId = "state", label = "State",
  13. choices = c("CA", "PA"),
  14. selected = " "),
  15. sliderInput("total", "Total Number",
  16. value = c(mean(dataf$total, na.rm = TRUE), max(dataf$total, na.rm = TRUE)),
  17. min = min(dataf$total, na.rm = TRUE),
  18. max = max(dataf$total, na.rm = TRUE)),
  19. h5(''),
  20. downloadButton(outputId = "downloadData", label = "Download"),
  21. ),
  22. mainPanel(
  23. DT::dataTableOutput(outputId = "table")
  24. )
  25. )
  26. )
  27. server <- function(input, output, session) {
  28. df <- reactive({
  29. dataf %>%
  30. filter(total %in% c(input$total[1]:input$total[2]), state == input$state)
  31. })
  32. output$table <- renderDataTable(
  33. df()
  34. )
  35. output$downloadData <- downloadHandler(
  36. filename = function() {
  37. paste("download_data", ".csv", sep="")
  38. },
  39. content = function(file) {
  40. write.csv(df(), file)
  41. }
  42. )
  43. }
  44. shinyApp(ui, server)

希望这个翻译对您有帮助。如果您有任何其他问题,请随时提出。

英文:

Please try the updated code below

  1. library(shiny)
  2. library(shinyWidgets)
  3. library(tidyverse)
  4. library(DT)
  5. state &lt;- c(&quot;CA&quot;, &quot;CA&quot;, &quot;PA&quot;, &quot;PA&quot;, &quot;PA&quot;, &quot;CA&quot;)
  6. total &lt;- c(100, 111, 120, 130, 11, 200)
  7. dataf &lt;- data.frame(state, total)
  8. ui &lt;- fluidPage(
  9. sidebarLayout(
  10. sidebarPanel(
  11. h2(&quot;Output data&quot;),
  12. selectInput(inputId = &quot;state&quot;, label = &quot;Stat&quot;,
  13. choices = c(&quot;CA&quot;, &quot;PA&quot;),
  14. selected = &quot; &quot;),
  15. sliderInput(&quot;total&quot;, &quot;Total Number&quot;,
  16. value = c(mean(dataf$total, na.rm = TRUE), max(dataf$total, na.rm = TRUE)),
  17. min = min(dataf$total, na.rm = TRUE),
  18. max = max(dataf$total, na.rm = TRUE)),
  19. h5(&#39;&#39;),
  20. downloadButton(outputId = &quot;downloadData&quot;, label = &quot;Download&quot;),
  21. ),
  22. mainPanel(
  23. DT::dataTableOutput(outputId = &quot;table&quot;)
  24. )
  25. )
  26. )
  27. server &lt;- function(input, output, session) {
  28. df &lt;- reactive({
  29. dataf %&gt;%
  30. filter(total %in% c(input$total[1]:input$total[2]), state == input$state)
  31. })
  32. output$table &lt;- renderDataTable(
  33. df()
  34. )
  35. output$downloadData &lt;- downloadHandler(
  36. filename = function() {
  37. paste(&quot;download_data&quot;, &quot;.csv&quot;, sep=&quot;&quot;)
  38. },
  39. content = function(file) {
  40. write.csv(df(), file)
  41. }
  42. )
  43. }
  44. shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年6月27日 20:25:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564843.html
匿名

发表评论

匿名网友

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

确定