【问题标题】:Change Datatable after changing inputSelect更改输入选择后更改数据表
【发布时间】:2017-11-04 11:30:47
【问题描述】:

我有一个侧边栏,其中有一个 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)

【问题讨论】:

    标签: r dataframe data.table shiny


    【解决方案1】:
    1. 您提供的示例不是独立的。您应该提供一些数据,或者更好地创建一个更简单的minimal working example

    2. 反应式表达式就像函数,所以你需要调用SelectedKlant()来得到答案a。见here

    3. 在 UI 中,您只需要调用DT::dataTableOutput("mytable") ,如this simple example

    4. 总而言之,总结以上几点:

      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)
      

    我希望这能回答你的问题!

    【讨论】:

    • 你的工作对我来说做得很完美@Javier!感谢您帮助我,最重要的是,让我学到一些东西!
    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2013-09-27
    • 2016-10-29
    • 1970-01-01
    • 2013-06-19
    相关资源
    最近更新 更多