ShinyApp操作按钮:单击和双击时的值不同

时间:2019-03-18 18:03:39

标签: r shiny

我在ShinyApp中进行了特殊的观察,但不确定为什么会发生或如何解决。 首次启动应用程序时,选择输入并单击操作按钮(转到),会在表中向我显示输出值,这些输出值是错误的。当我再次单击操作按钮而不更改输入时,输出值将更改为正确的值。 该问题似乎仅在我首次启动该应用程序时发生。然后,该应用程序运行良好,并且当我单击时会显示正确的值。我正在复制下面的server.R代码。 谢谢!

server <- function(input, output,session) {


  one_or_duration <- eventReactive(input$go,ignoreNULL = FALSE,{
              if(input$one_or_duration == "A single measurement"){
              "single"}
              else if(input$one_or_duration == "Multiple measurements"){
              "multiple"}})

  nnrti <- eventReactive(input$go,ignoreNULL = FALSE,{
    if(input$nnrti == "Yes, include participants on NNRTIs"){
      "yes"}
    else if(input$nnrti == "No, exclude participants on NNRTIs"){
      "no"}})

  arm <- eventReactive(input$go,ignoreNULL = FALSE,{
    if(input$arm == "Yes, include participants in intervention arms"){
      "yes"}
    else if(input$arm == "No, exclude participants in intervention arms"){
      "no"}})

  freq <- eventReactive(input$go,ignoreNULL = FALSE,{
    if(input$freq == "Same frequency as observed by authors"){
      "same"}
    else if(input$freq == "Input expected frequency"){
      "diff"}})

  pt_class <- eventReactive(input$go,ignoreNULL = FALSE,{
    if(input$pt_class == "All study participants"){
      "all"}
    else if(input$pt_class == "Early treated participants"){
      "early"}
    else if(input$pt_class == "Chronic treated participants"){
      "chronic"}
  })


  ptcs_plus_ncs <- eventReactive(input$go,ignoreNULL = FALSE,{
    if (freq() == "same"){
    validate(need(input$vl >= 50, "Please select viral load threshold equal to or greater than 50 copies/mL"))
      system(sprintf("python3 new_duration_ctrl.py %s %s %s %s %s %s", input$vl , input$duration, pt_class(), one_or_duration(), nnrti(),arm()))
      data <- read.csv("output_all.csv")
      data}

    else if (freq() == "diff"){
      validate(need(input$vl >= 50, "Please select viral load threshold equal to or greater than 50 copies/mL"))
      system(sprintf("python3 new_another_freq.py %s", input$expected_freq))
      data <- read.csv("output_all.csv")
      data}
    })



    ptc <- eventReactive(input$go,ignoreNULL = FALSE,{
      system(sprintf("python3 new_duration_ctrl_ptc.py %s %s %s %s %s %s", input$vl , input$duration, pt_class(), one_or_duration(), nnrti(), arm()))
      data <- read.csv("output_ptc.csv")
      data 
    })

    nc <- eventReactive(input$go,ignoreNULL = FALSE,{
      system(sprintf("python3 new_duration_ctrl_nc.py %s %s %s %s %s %s", input$vl , input$duration, pt_class(), one_or_duration(), nnrti(), arm()))
      data <- read.csv("output_nc.csv")
      data 
      })

    ptcs_plus_ncs_subset <- eventReactive(input$go,ignoreNULL = FALSE,
      {
      if(input$weekly == FALSE & freq() == "same")
      {
      data <- read.csv("output_all.csv")
      data_ptc <- read.csv("output_ptc.csv")
      data_nc <- read.csv("output_nc.csv")
      mytimes <- c("1", "5", "9", "13", "25", "49")
      subset1 <- data[mytimes,1:4]
      subset2 <- data_ptc[mytimes,2:4]
      subset3 <- data_nc[mytimes,2:4]
      subset <- cbind(subset1,subset2,subset3)
      colnames(subset) <- c("Weeks after treatment interruption", "Percentage with viral suppression (Total)", "Count with viral suppression (Total)", "Count under follow-up (Total)", "Percentage with viral suppression (PTCs)", "Count with viral suppression (PTCs)", "Count under follow-up (PTCs)","Percentage with viral suppression (NCs)", "Count with viral suppression (NCs)", "Count under follow-up (NCs)")
      subset
    }
        else if(input$weekly == TRUE & freq() == "same"){
          data <- read.csv("output_all.csv")
          data_ptc <- read.csv("output_ptc.csv")
          data_nc <- read.csv("output_nc.csv")
          mytimes <- c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49")
          subset1 <- data[mytimes,1:4]
          subset2 <- data_ptc[mytimes,2:4]
          subset3 <- data_nc[mytimes,2:4]
          subset <- cbind(subset1,subset2,subset3)
          colnames(subset) <- c("Weeks after treatment interruption", "Percentage with viral suppression (Total)", "Count with viral suppression (Total)", "Count under follow-up (Total)", "Percentage with viral suppression (PTCs)", "Count with viral suppression (PTCs)", "Count under follow-up (PTCs)","Percentage with viral suppression (NCs)", "Count with viral suppression (NCs)", "Count under follow-up (NCs)")
          subset          
        }
        else if(input$weekly == FALSE & freq() == "diff"){
          data <- read.csv("output_all.csv")
          data_ptc <- read.csv("output_ptc.csv")
          data_nc <- read.csv("output_nc.csv")
          mytimes <- c("1", "5", "9", "13", "25", "49")
          subset1 <- data[mytimes,1:2]
          subset2 <- data_ptc[mytimes,2:4]
          subset3 <- data_nc[mytimes,2:4]
          subset <- cbind(subset1,subset2,subset3)
          colnames(subset) <- c("Weeks after treatment interruption", "Percentage with viral suppression (Total)", "Percentage with viral suppression (PTCs)", "Count with viral suppression (PTCs)", "Count under follow-up (PTCs)","Percentage with viral suppression (NCs)", "Count with viral suppression (NCs)", "Count under follow-up (NCs)")
          subset         

        }

        else if(input$weekly == TRUE & freq() == "diff"){
          data <- read.csv("output_all.csv")
          data_ptc <- read.csv("output_ptc.csv")
          data_nc <- read.csv("output_nc.csv")
          mytimes <- c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49")
          subset1 <- data[mytimes,1:2]
          subset2 <- data_ptc[mytimes,2:4]
          subset3 <- data_nc[mytimes,2:4]
          subset <- cbind(subset1,subset2,subset3)
          colnames(subset) <- c("Weeks after treatment interruption", "Percentage with viral suppression (Total)", "Percentage with viral suppression (PTCs)", "Count with viral suppression (PTCs)", "Count under follow-up (PTCs)","Percentage with viral suppression (NCs)", "Count with viral suppression (NCs)", "Count under follow-up (NCs)")
          subset          
        }
        })

  output$table <- 
    DT::renderDataTable({

    if (freq()=="same"){
    datatable(ptcs_plus_ncs_subset(),
              container=
                withTags(table(
                class = 'display',
                thead(
                    tr(
                      th(rowspan=2 ,'Weeks after treatment interruption'),
                      th(colspan = 3, 'Total'),
                      th(colspan = 3, 'PTCs'),
                      th(colspan = 3, 'NCs')
                    ),
                    tr(
                      lapply(c('Percentage with viral suppression (Total)', 'Count with viral suppression (Total)', 'Count under follow-up (Total)','Percentage with viral suppression (PTCs)', 'Count with viral suppression (PTCs)', 'Count under follow-up (PTCs)','Percentage with viral suppression (NCs)', 'Count with viral suppression (NCs)', 'Count under follow-up (NCs)'), th)
                    )                  
                    )
                )),
      rownames=FALSE,options = list(paging=FALSE, bInfo=FALSE, lengthChange=FALSE, searching = FALSE,
      columnDefs = list(list(className = 'dt-center', targets = "_all")))) %>% formatStyle(colnames(ptcs_plus_ncs_subset())[2],backgroundColor = "gray", color = "#fff", fontWeight = 'bold') %>% formatStyle(colnames(ptcs_plus_ncs_subset())[5],backgroundColor = "red", color = "#fff", fontWeight = 'bold') %>% formatStyle(colnames(ptcs_plus_ncs_subset())[8],backgroundColor = "blue", color = "#fff", fontWeight = 'bold')
    }
    else if (freq()=="diff")
    {
      datatable(ptcs_plus_ncs_subset(),
                container=
                  withTags(table(
                    class = 'display',
                    thead(
                      tr(
                        th(rowspan=2 ,'Weeks after treatment interruption'),
                        th(colspan = 1, 'Total'),
                        th(colspan = 3, 'PTCs'),
                        th(colspan = 3, 'NCs')
                      ),
                      tr(
                        lapply(c('Percentage with viral suppression (Total)','Percentage with viral suppression (PTCs)', 'Count with viral suppression (PTCs)', 'Count under follow-up (PTCs)','Percentage with viral suppression (NCs)', 'Count with viral suppression (NCs)', 'Count under follow-up (NCs)'), th)
                      )
                    )
                  )),
                rownames=FALSE,options = list(paging=FALSE, bInfo=FALSE, lengthChange=FALSE, searching = FALSE,
                                              columnDefs = list(list(className = 'dt-center', targets = "_all")))) %>% formatStyle(colnames(ptcs_plus_ncs_subset())[2],backgroundColor = "gray", color = "#fff", fontWeight = 'bold') %>% formatStyle(colnames(ptcs_plus_ncs_subset())[3],backgroundColor = "red", color = "#fff", fontWeight = 'bold') %>% formatStyle(colnames(ptcs_plus_ncs_subset())[6],backgroundColor = "blue", color = "#fff", fontWeight = 'bold')

    }
    })
}

0 个答案:

没有答案