简体   繁体   English

通过 downloadhandler-R Shiny 在单个 csv 文件中下载多个复选框

[英]Download multiple checkboxes in a single csv file via downloadhandler- R Shiny

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.我正在尝试创建一个复选框列表,用户可以在其中选择他们想要在单个 csv 文件中下载的内容,并且 csv 文件应显示代码中定义的“choiceNames”和“choiceValues”。 I wrote the code below, but the download button does not work, and I don't know how to solve this issue我写了下面的代码,但是下载按钮不起作用,不知道如何解决这个问题

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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM