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



我试图创建一个复选框列表,其中用户可以在单个csv文件中选择他们想要下载的内容,并且csv文件应该显示'choiceNames'和' choicevalvalues ',这在代码中定义。我写了下面的代码,但是下载按钮不工作,我不知道如何解决这个问题

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)

这是你要找的吗?

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)

最新更新