【问题标题】:Dynamic filtering in ShinyAppShinyApp 中的动态过滤
【发布时间】:2021-12-31 03:36:19
【问题描述】:

我刚刚开始学习 ShinyApp,正在尝试创建一个可以动态过滤值的表。

我想要的结果

<filter>GlassSupplier Supplier1
WindowType        AverageBreakageRate
Aluminum                  3.63
Wood                      7.22  

我得到的结果

<filter>GlassSupplier Supplier1
WindowType        AverageBreakageRate
Aluminum                  2.815
Vinyl                     6.165
Wood                      7.22  

enter image description here

我的代码创建了一个表,但没有根据选择输入选择进行过滤。还有一种方法可以添加一个操作按钮,因此只有在点击操作按钮时,表格才会反映由于新的选择输入参数而导致的变化?

任何帮助将不胜感激!

library(shiny)
library(dplyr)
library(readxl)


ui <- fluidPage(
  titlePanel("title panel"),
  sidebarLayout(position = "left",
                sidebarPanel("sidebar panel",
                             selectInput(inputId = "table",
                                         label = "Choose a Supplier",
                                         "Names"),
                ),
                mainPanel("main panel",
                          tableOutput("myTable")
                )))

server <- function(input, output,session) 
{
  GlassSupplier <- c('Supplier 1','Supplier 2','Supplier 1','Supplier 4','Supplier 2')
  WindowType <- c('Wood','Vinyl','Aluminum','Aluminum','Vinyl')
  BreakageRate <- c(7.22,6.33,3.63,2,6)
  df<- data.frame(GlassSupplier,WindowType,BreakageRate)
   data <- reactive({
    req(input$table)
    dframe <- df %>%  group_by(WindowType) %>% summarise(BrkRate = mean(BreakageRate))
  })
  
  #Update SelectInput Dynamically
  observe({
    updateSelectInput(session, "table", choices = df$GlassSupplier)
  })
  
  output$myTable = renderTable({
    data()
  })
}
shinyApp(ui,server)

【问题讨论】:

  • 数据图像对您没有帮助。如果您希望有人帮助您,请在问题中发布数据,以便有人可以复制和使用您的代码。您可以使用dput() 在控制台中获取数据并将其发布到问题中。
  • 感谢您的反馈我已对代码和问题进行了更改。

标签: r shiny shinydashboard reactive


【解决方案1】:

您只需要filter。要使actionButton 工作,只需将reactive() 更改为eventReactive() 对象。试试这个

library(shiny)
library(dplyr)
library(readxl)

ui <- fluidPage(
  titlePanel("title panel"),
  sidebarLayout(position = "left",
                sidebarPanel("sidebar panel",
                             selectInput(inputId = "table",
                                         label = "Choose a Supplier",
                                         "Names"),
                             actionButton(inputId = "btn",label="Update")
                ),
                mainPanel("main panel",
                          tableOutput("myTable")
                )))

server <- function(input, output,session)
{
  GlassSupplier <- c('Supplier 1','Supplier 2','Supplier 1','Supplier 4','Supplier 2')
  WindowType <- c('Wood','Vinyl','Aluminum','Aluminum','Vinyl')
  BreakageRate <- c(7.22,6.33,3.63,2,6)
  df<- data.frame(GlassSupplier,WindowType,BreakageRate)
  data <- eventReactive(input$btn, {
    req(input$table)
    df %>% dplyr::filter(GlassSupplier %in% input$table) %>%
      group_by(WindowType) %>%
      dplyr::summarise(BrkRate = mean(BreakageRate))
  })

  #Update SelectInput Dynamically
  observe({
    updateSelectInput(session, "table", choices = df$GlassSupplier)
  })

  output$myTable = renderTable({
    data()
  })
}
shinyApp(ui,server)

【讨论】:

  • 有没有办法添加一个操作按钮,这样表格的值就不会在更改过滤器时改变?
  • 嘿,非常感谢。我想出了那部分。您能告诉我如何删除使用操作按钮创建的对象吗?
  • 由于此问题没有actionButton,请修改您的问题或提出新问题以显示您要删除的对象。
  • 嗨 YBS,我刚刚发布了一个问题 stackoverflow.com/questions/70059229/multiple-tabs-in-shinyapp>。你能帮我解决一下吗?
猜你喜欢
  • 2018-05-20
  • 2019-05-24
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2017-05-09
相关资源
最近更新 更多