0

I have a question in regards to Shiny. I have four data sets and depending on the user's input, I want to use one of the four data sets for the computations. I tried using an if-statement, but it doesn't work. I also tried using a reactive expression, but could not make that work. Is it technically possible what I want to do? And if so, how?

ui <- fluidPage(
  
    theme = shinytheme("cerulean"),  

    titlePanel("Some Data"),

    sidebarLayout(

      sidebarPanel(
            
            checkboxGroupInput("checkGroup", label = h3("Choose your tea brand:"), 
                               choices = list("Solar" = 1, 
                                              "Green" = 2, 
                                              "Cat" = 3, 
                                              "Conservation" = 4)),

           selectInput("tea", "Choose your tea:", c("Green Tea", "Black Tea", "Blue Tea")
            
        ),

        mainPanel(
        
          tabsetPanel(
            tabPanel("Titel",
              
            h4("Subtitel"),
            verbatimTextOutput("demographicsTable"),
            
            h4("Subtitel"),
            tableOutput("nbhsizeTable"),
            ))))

server <- function(input, output) {

      if(input$checkGroup == "Solar"){
        Data <- read.csv("SolarData.csv")
      } else if(input$checkGroup == "Green"){
        Data <- read.csv("GreenData.csv")
      } else if(input$checkGroup == "Conservation"){
        Data <- read.csv("ConservationData.csv")
      } else if(input$checkGroup == "Cat"){
        Data <- read.csv("CatData.csv")
      }

  output$demographicsTable <- renderPrint({
              
      dat <- Data %>% filter(grepl(input$tea, cookies))  
        
        ### age
        age <- summary(dat$age)
        
        ### gender
        gender <- table(dat$gender)
        
        dat <- list(Age = c(age[1], age[4], age[6]), 
                    Gender = gender)
        dat

    })
    
    output$nbhsizeTable <- renderTable({
              
        dat <- Data %>% filter(grepl(input$tea, cookies))
        
        table(dat$size_of_teacup)

    })
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Tabea
  • 47
  • 5

1 Answers1

0

There's a few things going on here.

First, I think the biggest piece is that the input$checkGroup returns the values in the selection list, not the labels/names displayed to the user. It also returns this value as a string, i.e. when the user selects "Solar", the server will see an input value of "1".

Second, your Data variable needs to be a reactive. Right now your if/else statement will be executed at launch and never again. Since you don't specify an initial selection, the input value will be NULL not matching any of the conditional statements, and no value will be assigned to Data.

I would do something along the lines of this. Load your data sets into a static list (this will prevent reads from disk every time the user adds/removes checks)

teaData <- list(
  read.csv("SolarData.csv"),
  read.csv("GreenData.csv"),
  read.csv("CatData.csv"),
  read.csv("ConservationData.csv")
)

Then create a reactive that subsets this list by coercing the input values and combines them based on selections

Data <- reactive({
  dplyr::bind_rows(teaData[as.integer(input$checkGroup)])
})

I can't really test the solution without sample data, but I think the solution is straightforward enough this should work.

Marcus
  • 3,478
  • 1
  • 7
  • 16
  • Ok, thanks! This works for the data set input. However, I have made the select input conditional on the checkbox input, and that one does not work now. – Tabea Mar 13 '21 at 16:13
  • 1
    If I understand what you did, you would need an observer that updates the select input whenever the checkbox group changes. Something along the lines of (need to add `session` to your server function arguments) `observeEvent(Data, updateSelectInput(session, "tea", choices = <>))` – Marcus Mar 18 '21 at 05:05