2

I want to create a shiny app where the user can view data from two different datasets: 'new' and 'central'.

I use the below code:

# 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()
   })
   }

Only the 'central' dataset is viewed in the dataframe, and the input options are only visible from the 'central' dataset.

  • Welcome to Stack Overflow! Since you're new here, I recommend reading ["How do I ask a good question?"](https://stackoverflow.com/help/how-to-ask) for some tips. Your question is lacking sufficient detail to help the community help you. – Marc LaFleur Jan 27 '18 at 16:12

1 Answers1

0

The following code, which is just your code with some pieces left out and a different dataset, runs fine for me. Without a reproducible example it is a bit difficult to help you, since I am unable to reproduce the erroneous behavior. It would be great if you could add a reproducible example, for tips on that see for example here or here.

# Define UI for application
ui<- 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("mtcars2", "mtcars10"))),
    mainPanel(
      DT::dataTableOutput("table")
    ))
)

# Define UI for application that 

server<-function(input, output) {

  # A reactive expression to return the dataset corresponding to the 
  datasetInput <- reactive({
    switch(input$dataset,
           "mtcars2" = head(mtcars,2),
           "mtcars10" = head(mtcars,10))
  })

  output$table <- DT::renderDataTable({
    datasetInput()
  })
}
shinyApp(ui,server)
Florian
  • 24,425
  • 4
  • 49
  • 80