我想创建一个闪亮的应用程序,用户可以在其中查看来自两个不同数据集的数据:“新”和“中央”。
我使用下面的代码:
# Define UI for application
fluidPage(
titlePanel("Public services"),
#tell shiny where to display the objects
#let the user choose the dataset they want to interact with
sidebarLayout(
sidebarPanel(
selectInput("dataset", "Choose dataset:",
choices = c("Local government", "Central Government")),
uiOutput('name'),
uiOutput('type'),
uiOutput('service')),
#display an output object on the main manel
mainPanel(
# Output: Verbatim text for data summary ----
h4("Summary"),
verbatimTextOutput("summary"),
h4("Observations"),
DT::dataTableOutput("table")
))
)
# Define UI for application that
function(input, output) {
# A reactive expression to return the dataset corresponding to the
user choice
datasetInput <- reactive({
switch(input$dataset,
"Central Government" = central,
"Local Government" = local)
})
#Filter data based on selections
#name
output$name = renderUI({
selectInput('name', h5('Department or Local authority name'),
choices = names(datasetInput()))
})
output$type = renderUI({
selectInput('type', h5('Service type'), choices =
names(datasetInput()))
})
output$service = renderUI({
selectInput('service', h5('Service'), choices =
names(datasetInput()))
})
output$table <- DT::renderDataTable({
datasetInput()
})
}
在数据框中仅查看“中央”数据集,并且输入选项仅在“中央”数据集中可见。