显示多个相关selectInput的DataTable输出

时间:2017-11-03 13:20:35

标签: r shiny shinydashboard

我发现显示与selectInput选择器相关的输出时遇到问题。我制作了两个相关的selectInput选择器: Location Year 。我想要的是使用dataTableOutput显示我的数据框的相应选择。

例如,选择位置" 2"和年" 2016"显示凯蒂和威廉的记录。

这是我简化的 ui 部分:

library(DT)
library(shiny)
library(shinydashboard)

Location = c("1","1","1","2","2","2","3","3","3") 
Year = c(2015,2014,2016,2015,2016,2016,2017,2016,2014) 
Person = c("John", "Ann", "Katy", "Ann", "Katy", "William", "Henry", "Luke", "Luke")
mockup = data.frame(Location, Year, Person)

ui <- dashboardPage(
                    dashboardHeader(),
                    dashboardSidebar(
                      sidebarMenu(
                        menuItem("Selection", tabName = "selection")                  
                                  )
                    ),
                    dashboardBody(
                      tabItems(
                        tabItem(tabName = "selection",
                                fluidRow(
                                  box(width = 5, 
                                      title = "TITLE", "Choose something",
                                      collapsible = FALSE, 
                                      htmlOutput("Location_selector"),
                                      htmlOutput("Year_selector")
                                  ),
                                  mainPanel(
                                    DT::dataTableOutput("selection")
                                  )
                                 ))             

                                        )
                      )
                      )

这是我的服务器部分:

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

  output$Location_selector = renderUI({
    selectInput(inputId = "Location",
                label = "Location:",
                choices = as.character(unique(mockup$Location)))
  })
  output$Year_selector = renderUI({

    data_available = mockup[mockup$Location == input$Location, "Year"]

    selectInput(inputId = "Year",
                label = "Year:",
                choices = unique(data_available),
                selected = unique(data_available)[1])
  })

  output$selection <-  DT::renderDataTable({

})
}

shinyApp(ui, server)

我知道我错过了关于下一部分的内容,但到目前为止我无法解决这个问题:

output$selection <-  DT::renderDataTable({

    })

希望你们能帮助我。提前谢谢!

1 个答案:

答案 0 :(得分:0)

这两项建议都有效:

subset(mockup, Location == input$Location & Year == input$Year)

with(mockup, mockup[Year==input$Year & Location==input$Location,])