更改inputSelect后更改数据表

时间:2017-11-04 11:30:48

标签: r dataframe data.table shiny

我有一个侧边栏,我有一个inputSelect列表中的项目。 在Shiny的主要部分,我希望当我在inputSelect中更改我的选择时,表格会发生变化。 我设法用标题中的普通文本执行此操作。 任何一个

#App
library(shiny)

ui <- fluidPage(
  headerPanel(" "),
  sidebarLayout(
    sidebarPanel(

      h4 ("Selecteer een klant uit lijst"),

      selectInput("select", 
                  label = "Kies een klant",
                  choices = list.Hikerlocaties,
                  selected = 0),
      # checkboxInput("Student", "Alleen studenten", value = TRUE),
      actionButton("zoek", "Zoek")

    ),
    mainPanel(
      textOutput("select"),
      datatable(SelectedKlant, options = list(pageLength = 20, dom = 'tip', order = list(2,'desc')), rownames = FALSE, width = 500, colnames = c('Naam', 'Klant', 'Aantal'), elementId = "results")
      )))

server <- function(input, output) {
  output$select <- renderText(input$select)
  SelectedKlant <- reactive({
    a <- subset(freq3, Klantref == input$select)
    return(a)
  })
  output$results <- renderDataTable(SelectedKlant, options = list(pageLength = 20, dom = 'tip', order = list(2,'desc')), rownames = FALSE, width = 500, colnames = c('Naam', 'Klant', 'Aantal'), elementId = "results")
} 

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

1 个答案:

答案 0 :(得分:0)

  1. 您提供的示例不是自包含的。您应该提供一些数据,或者甚至更好地创建更简单的minimal working example

  2. 反应式表达与功能类似,因此您需要致电SelectedKlant()以获得答案a。请参阅here

  3. 在用户界面中,您只需拨打this simple example

  4. 中的DT::dataTableOutput("mytable")
  5. 总而言之,这总结了上述内容:

    library(shiny)
    library(DT)
    
    ui <- basicPage(
      sidebarLayout(
        sidebarPanel(
          selectInput("select", 
                  label = "Select species",
                  choices = c('setosa','versicolor','virginica'))
    ),
    mainPanel(
      textOutput("selected_text"),
      DT::dataTableOutput("results")
        )
      )
    )
    
    server <- function(input, output) {
      output$selected_text <- renderText(input$select)
    
      SelectedKlant <- reactive({
        a <- subset(iris, Species == input$select)
        return(a)
      })
    
      output$results <- DT::renderDataTable(SelectedKlant(), options = list(pageLength = 20, dom = 'tip', order = list(2,'desc')), rownames = FALSE, width = 500, elementId = "results")
    } 
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    
  6. 我希望这能回答你的问题!