在一个闪亮的应用程序中使用actionButton和updateSelectInput

时间:2017-02-27 11:13:16

标签: r shiny

我有一个使用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)

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

你正在寻找这样的东西吗?只有当您单击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函数调用此反应函数,因此只有在单击按钮时才会看到该表。