通过下载Handler-r Shiny在单个CSV文件中下载多个复选框

发布于 2025-02-08 22:43:38 字数 4811 浏览 2 评论 0原文

我正在尝试创建一个复选框列表,用户可以在其中选择要在单个CSV文件中下载的内容,并且CSV文件应同时显示“ choiceNames”和“ chosecevalues”,该文件在代码中定义了。我在下面写了代码,但是下载按钮不起作用,我不知道如何解决此问题

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)

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, 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两仪 2025-02-15 22:43:40

这是您想要的吗?

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文