Search code examples
rshiny

Download multiple checkboxes in a single csv file via downloadhandler


I'm attempting to create a list of checkboxes where a user can select what they want to download in a single csv file and the csv file should display both 'choiceNames' and 'choiceValues', which defined in the code. I wrote the code below, but the download button does not work.

ui <- fluidPage(
  titlePanel(h1(" Worksheet",align="center")),
  hr(),
  
  sidebarPanel(  
    titlePanel(h4("Antenatal Risk Factors/Current Pregnancy",align="center")),
    hr(),
    checkboxGroupInput("Antel", "  ",
                       choiceNames =
                         list("Urinary tract infections this pregnancy",
                              "Urinary tract infections this pregnancy, treated",
                              "Anemia this pregnancy (HCT < 30/Hgb <10)",
                              "Hemoglobinopathy this pregnancy",
                              "Coagulation disorder",
                              "Rh sensitization",
                              "Other iso-immunization",
                              "Biliary/liver disorder(Yes at delivery)",
                              "Cardiac disease",
                              "Autoimmune disease",
                              "Antiphospholipid syndrome",
                              "Specify collagen vascular disease",
                              "Asthma",
                              "Acute or chronic lung disease",
                              "Renal disorder/disease",
                              "Renal dialysis or end stage renal disease",
                              "Thyroid disease",
                              "Cancer this pregnancy",
                              "Cancer treatment this pregnancy"
                         ),
                       choiceValues =list("RFC_INFUT","RFC_INFUTTX",
                                          "RFC_ANEMIA",
                                          "RFC_HEMO",
                                          "COAGULATION_DISORDER",
                                          "RFC_RHS",
                                          "RFC_ISO",
                                          "BILARY_LIVE_DISORD",
                                          "RFC_CDDZ",
                                          "RFC_CVDZ",
                                          "RFC_APSY",
                                          "RFC_CVSPEC",
                                          "RFC_ASTH",
                                          "RFC_LGDZ",
                                          "RENAL_DISORDER_DISEASE",
                                          "RFC_RNDY",
                                          "RFC_THYDZ",
                                          "RFC_CA",
                                          "CANCER_TREATMENT" )
    ),  

   
    
    checkboxGroupInput("Fetal", "Fetal Conditions",
                       choiceNames = list("Decreased fetal movement",
                                          "Abnormal fetal heart rate/rhythm",
                                          "Suspected IUGR this pregnancy",
                                          "Fetal compromise this pregnancy",
                                          "Suspected Fetal CNS Anomaly",
                                          "Diagnosed fetal anomaly:",
                                          "Fetal damage",
                                          "Postterm, > 41 6/7 weeks"),
                       choiceValues = list("RFC_FETMOV",
                                           "RFC_ABRHY","RFC_IUGR",
                                           "RFC_FCOMP",
                                           "RFC_FEANOM",
                                           "RFC_FAN",
                                           "RFC_FD",
                                           "RFC_POST")
                       
                       
    ), 
 checkboxGroupInput("Maternal", "Maternal Characteristics",
                     choiceNames = list("Maternal traumatic injury during this pregnancy",
                                  "Domestic violence during this pregnancy",
                                  "aternal surgical procedure during this pregnancy",
                              "Other antenatal risk factors during this pregnancy:_____"),
                     choiceValues = list("RFC_TINJ",
                                           "RFC_VIOL",
                                           "RFC_SURG",
                                           "RFC_OTHR")
    ), 
    
  )
 downloadButton("download") 
  #______________END_______________#   
)


server <- function(input, output) {
  
  output$download_checkboxes <- downloadHandler(
    filename = function() {
      "results.csv"
    },
    content = function(file) {
      Data <- data.frame(selected = input$Antel & input$Fetal )
      write.csv(Data, file, row.names = FALSE)
    }
  )
  
  
}
)
 
shinyApp(ui = ui, server = server)


Solution

  • Is this what you're looking for?

    library(shiny)
    
    c_antel <- data.frame(cn = c("Urinary tract infections this pregnancy",
                                 "Urinary tract infections this pregnancy, treated",
                                 "Anemia this pregnancy (HCT < 30/Hgb <10)",
                                 "Hemoglobinopathy this pregnancy",
                                 "Coagulation disorder",
                                 "Rh sensitization",
                                 "Other iso-immunization",
                                 "Biliary/liver disorder(Yes at delivery)",
                                 "Cardiac disease",
                                 "Autoimmune disease",
                                 "Antiphospholipid syndrome",
                                 "Specify collagen vascular disease",
                                 "Asthma",
                                 "Acute or chronic lung disease",
                                 "Renal disorder/disease",
                                 "Renal dialysis or end stage renal disease",
                                 "Thyroid disease",
                                 "Cancer this pregnancy",
                                 "Cancer treatment this pregnancy"),
                          cv = c("RFC_INFUT",
                                 "RFC_INFUTTX",
                                 "RFC_ANEMIA",
                                 "RFC_HEMO",
                                 "COAGULATION_DISORDER",
                                 "RFC_RHS",
                                 "RFC_ISO",
                                 "BILARY_LIVE_DISORD",
                                 "RFC_CDDZ",
                                 "RFC_CVDZ",
                                 "RFC_APSY",
                                 "RFC_CVSPEC",
                                 "RFC_ASTH",
                                 "RFC_LGDZ",
                                 "RENAL_DISORDER_DISEASE",
                                 "RFC_RNDY",
                                 "RFC_THYDZ",
                                 "RFC_CA",
                                 "CANCER_TREATMENT"))
    
    c_fetal <- data.frame(cn = c("Decreased fetal movement",
                                 "Abnormal fetal heart rate/rhythm",
                                 "Suspected IUGR this pregnancy",
                                 "Fetal compromise this pregnancy",
                                 "Suspected Fetal CNS Anomaly",
                                 "Diagnosed fetal anomaly:",
                                 "Fetal damage",
                                 "Postterm, > 41 6/7 weeks"),
                          cv = c("RFC_FETMOV",
                                 "RFC_ABRHY","RFC_IUGR",
                                 "RFC_FCOMP",
                                 "RFC_FEANOM",
                                 "RFC_FAN",
                                 "RFC_FD",
                                 "RFC_POST"))
    
    c_mater <- data.frame(cn = c("Maternal traumatic injury during this pregnancy",
                                  "Domestic violence during this pregnancy",
                                 "aternal surgical procedure during this pregnancy",
                                 "Other antenatal risk factors during this pregnancy:_____"),
                          cv = c("RFC_TINJ",
                                "RFC_VIOL",
                                "RFC_SURG",
                                "RFC_OTHR"))
    ui <- fluidPage(
      titlePanel(h1(" Worksheet",align="center")),
      hr(),
      
      sidebarPanel(  
        titlePanel(h4("Antenatal Risk Factors/Current Pregnancy",align="center")),
        hr(),
        checkboxGroupInput("Antel", "  ",
                           choiceNames = c_antel$cn,
                           choiceValues = c_antel$cv
        ),  
        checkboxGroupInput("Fetal", "Fetal Conditions",
                           choiceNames = c_fetal$cn,
                           choiceValues = c_fetal$cv
        ), 
        checkboxGroupInput("Maternal", "Maternal Characteristics",
                           choiceNames = c_mater$cn,
                           choiceValues = c_mater$cv
        )
      ),
      downloadButton("download_checkboxes", "download"),
      #______________END_______________#   
    )
    
    
    server <- function(input, output) {
      
      output$download_checkboxes <- downloadHandler(
        contentType = "text/csv",
        filename = "results.csv",
        content = function(file) {
          Data <- data.frame(
            key = c(input$Antel, 
                    input$Fetal,
                    input$Maternal),
            value = c(c_antel$cn[c_antel$cv %in% input$Antel],
                      c_fetal$cn[c_fetal$cv %in% input$Fetal],
                      c_mater$cn[c_mater$cv %in% input$Maternal])
            )
          write.csv(Data, file, row.names = F)
        }
      )
      
    }
    
    shinyApp(ui = ui, server = server)