繁体   English   中英

在闪亮的应用程序中将actionButton与updateSelectInput一起使用

[英]Use actionButton with updateSelectInput in a shiny app

我有一个使用UpdateselectInput的闪亮应用程序,我想添加一个actionButton,因为单独使用updateselectInput时存在一些错误。 它似乎不起作用,我只想在操作按钮“我的应用类似于此”时显示该表:

library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

  titlePanel("Title"),

  sidebarLayout(
    sidebarPanel(width=3,
                 selectInput("filter1", "Filter 1", multiple = TRUE, choices = c("All", LETTERS)),
                 selectInput("filter2", "Filter 2", multiple = TRUE, choices = c("All", as.character(seq.int(1, length(letters), 1)))),
                 selectInput("filter3", "Filter 3", multiple = TRUE, choices = c("All", letters)),
                 actionButton("go_button", "GO !")),

    mainPanel(
      DT::dataTableOutput("tableprint")
    )
  )
)

server <- function(input, output, session) {

  output$tableprint <- DT::renderDataTable({
    input$go_button
    # Data
    df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52),
                 letters = paste(LETTERS, Numbers, sep = ""))

    df1 <- df

    if("All" %in% input$filter1){
      df1
    } else if (length(input$filter1)){
      df1 <- df1[which(df1$LETTERS %in% input$filter1),]
    }

    # Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
    updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1)
    updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2)



    if("All" %in% input$filter2){
      df1
    } else if (length(input$filter2)){
      df1 <- df1[which(df1$Numbers %in% input$filter2),]
    }
    updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3)

    if("All" %in% input$filter3){
      df1
    } else if (length(input$filter3)){
      df1 <- df1[which(df1$letters %in% input$filter3),]
    }
    datatable(df1)

  })
}

# Run the application 
shinyApp(ui = ui, server = server)

感谢帮助 !

您是否正在寻找这样的东西? 仅当您单击“执行Go按钮时,表格才会立即显示。 过滤器的工作方式是相同的。

library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

  titlePanel("Title"),

  sidebarLayout(
    sidebarPanel(width=3,
                 selectInput("filter1", "Filter 1", multiple = TRUE, choices = c("All", LETTERS)),
                 selectInput("filter2", "Filter 2", multiple = TRUE, choices = c("All", as.character(seq.int(1, length(letters), 1)))),
                 selectInput("filter3", "Filter 3", multiple = TRUE, choices = c("All", letters)),
                 actionButton("go_button", "GO !")),

    mainPanel(
      DT::dataTableOutput("tableprint")
    )
  )
)

server <- function(input, output, session) {


  goButton <- eventReactive(input$go_button,{
    # Data
    df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52),
                 letters = paste(LETTERS, Numbers, sep = ""))

    df1 <- df

    if("All" %in% input$filter1){
      df1
    } else if (length(input$filter1)){
      df1 <- df1[which(df1$LETTERS %in% input$filter1),]
    }

    # Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
    updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1)
    updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2)



    if("All" %in% input$filter2){
      df1
    } else if (length(input$filter2)){
      df1 <- df1[which(df1$Numbers %in% input$filter2),]
    }
    updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3)

    if("All" %in% input$filter3){
      df1
    } else if (length(input$filter3)){
      df1 <- df1[which(df1$letters %in% input$filter3),]
    }
    datatable(df1)
  })

  output$tableprint <- DT::renderDataTable({
    goButton()

  })
}

# Run the application 
shinyApp(ui = ui, server = server)

我已将过滤器代码移至eventReactive函数。 因此,当您单击该按钮时,它将基于过滤器来子集数据。 output$tableprint函数调用此反应函数,因此只有在单击按钮时,您才能看到该表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM