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

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

Shiny: Filtering not working as well as download

问题

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

library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)

state <- c("CA", "CA", "PA", "PA", "PA", "CA")
total <- c(100, 111, 120, 130, 11, 200)
dataf <- data.frame(state, total)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      h2("Output data"),
      selectInput(inputId = "state", label = "State",
                  choices = c("All", "CA", "PA"),
                  selected = "All"),
      sliderInput("total", "Total Number",
                  value = c(mean(dataf$total, na.rm = TRUE), max(dataf$total, na.rm = TRUE)),
                  min = min(dataf$total, na.rm = TRUE),
                  max = max(dataf$total, na.rm = TRUE)),
      
      downloadButton(outputId = "download_data", label = "Download"),
    ),
    mainPanel(
      DT::dataTableOutput(outputId = "table")
    )
  )
)

server <- function(input, output, session) {
  df <- reactive({
    if (input$state == "All") {
      dataf
    } else {
      dataf %>% filter(state == input$state)
    }
  })

  output$table <- renderDataTable(
    df()
  )
}

output$download_data <- downloadHandler(
  filename = "download_data.csv",
  content = function(file) {
    write.csv(df(), file, row.names = FALSE)
  }
)

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,

library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)

state &lt;- c(&quot;CA&quot;, &quot;CA&quot;, &quot;PA&quot;, &quot;PA&quot;, &quot;PA&quot;, &quot;CA&quot;)
total &lt;- c(100, 111, 120, 130, 11, 200)
dataf &lt;- data.frame(state, total)

ui &lt;- fluidPage(
  sidebarLayout(
    sidebarPanel(
      h2(&quot;Output data&quot;),
      selectInput(inputId = &quot;state&quot;, label = &quot;Stat&quot;,
                  choices = c(&quot;All&quot;, &quot;CA&quot;, &quot;PA&quot;),
                  selected = &quot;All&quot;),
      sliderInput(&quot;total&quot;, &quot;Total Number&quot;,
                  value = c(mean(dataf$total, na.rm = TRUE), max(dataf$total, na.rm = TRUE)),
                  min = min(dataf$total, na.rm = TRUE),
                  max = max(dataf$total, na.rm = TRUE)),
      
      downloadButton(outputId = &quot;download_data&quot;, label = &quot;Download&quot;),
    ),
    mainPanel(
      DT::dataTableOutput(outputId = &quot;table&quot;)
    )
  )
)

server &lt;- function(input, output, session) {
  df &lt;- reactive({
    dataf %&gt;%
      filter(total == input$total)
  })
  
  output$table &lt;- renderDataTable(
    dataf
  )
}

output$download_data &lt;- downloadHandler(
  filename = &quot;download_data.csv&quot;,
  content = function(file) {
    write.csv(df, file, row.names = FALSE)
  }
)

shinyApp(ui, server)

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

答案1

得分: 1

以下是代码的翻译部分:

library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)

state <- c("CA", "CA", "PA", "PA", "PA", "CA")
total <- c(100, 111, 120, 130, 11, 200)
dataf <- data.frame(state, total)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      h2("Output data"),
      selectInput(inputId = "state", label = "State",
                  choices = c("CA", "PA"),
                  selected = " "),
      sliderInput("total", "Total Number",
                  value = c(mean(dataf$total, na.rm = TRUE), max(dataf$total, na.rm = TRUE)),
                  min = min(dataf$total, na.rm = TRUE),
                  max = max(dataf$total, na.rm = TRUE)),
      h5(''),
      downloadButton(outputId = "downloadData", label = "Download"),
    ),
    mainPanel(
      DT::dataTableOutput(outputId = "table")
    )
  )
)

server <- function(input, output, session) {
  df <- reactive({
    dataf %>%
      filter(total %in% c(input$total[1]:input$total[2]), state == input$state)
  })
  
  output$table <- renderDataTable(
    df()
  )

  output$downloadData <- downloadHandler(
    filename = function() {
      paste("download_data", ".csv", sep="")
    },
    content = function(file) {
      write.csv(df(), file)
    }
  )

}

shinyApp(ui, server)

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

英文:

Please try the updated code below

library(shiny)
library(shinyWidgets)
library(tidyverse)
library(DT)

state &lt;- c(&quot;CA&quot;, &quot;CA&quot;, &quot;PA&quot;, &quot;PA&quot;, &quot;PA&quot;, &quot;CA&quot;)
total &lt;- c(100, 111, 120, 130, 11, 200)
dataf &lt;- data.frame(state, total)

ui &lt;- fluidPage(
  sidebarLayout(
    sidebarPanel(
      h2(&quot;Output data&quot;),
      selectInput(inputId = &quot;state&quot;, label = &quot;Stat&quot;,
                  choices = c(&quot;CA&quot;, &quot;PA&quot;),
                  selected = &quot; &quot;),
      sliderInput(&quot;total&quot;, &quot;Total Number&quot;,
                  value = c(mean(dataf$total, na.rm = TRUE), max(dataf$total, na.rm = TRUE)),
                  min = min(dataf$total, na.rm = TRUE),
                  max = max(dataf$total, na.rm = TRUE)),
      h5(&#39;&#39;),
      downloadButton(outputId = &quot;downloadData&quot;, label = &quot;Download&quot;),
    ),
    mainPanel(
      DT::dataTableOutput(outputId = &quot;table&quot;)
    )
  )
)

server &lt;- function(input, output, session) {
  df &lt;- reactive({
    dataf %&gt;%
      filter(total %in% c(input$total[1]:input$total[2]), state == input$state)
  })
  
  output$table &lt;- renderDataTable(
    df()
  )

  output$downloadData &lt;- downloadHandler(
    filename = function() {
      paste(&quot;download_data&quot;, &quot;.csv&quot;, sep=&quot;&quot;)
    },
    content = function(file) {
      write.csv(df(), file)
    }
  )

}

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:

确定