shiny:在空/未选中 radioButtions()的情况下,actionButton()是否可以返回错误?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shiny:在空/未选中 radioButtions()的情况下,actionButton()是否可以返回错误?相关的知识,希望对你有一定的参考价值。

我还没有找到解决问题的方法,这个SO thread接近了,但还不是全部。

我产生了一个简单的app,其中包含多个radioButtons()。符合app的基本概念,其中有些像radioButtons( ... , selected=character(0))中一样为空,而另一些具有预选值。

重要的是,在radioButtons()启动进一步分析之前,所有actionButton()必须具有选定的值。

问题:如何设计一种actionButton(),使其(1)在未选择radioButtons()的情况下返回错误,而(2)返回哪些包含未选择的值的radioButtons()

预期输出

enter image description here

撰写者

library(shiny)
library(shinyjs)
library(shinycustomloader)
library(shinyWidgets)



ui <- fluidPage(

  useShinyjs(),


  tabsetPanel(

    # GTR
    tabPanel(title = html(paste(h4("Gross Total Resection"),
                                h6("Simpson Grade I-III", align = "left"))),
             br(), br(),

             fluidRow(

               column(
                 4,
                 wellPanel(
                   style = "height:275px",
                   h4("Patient-related factors", align="center"), br(),
                   sliderInput("GTR_age", "Age", 
                               min = 18, max = 100, value = 60), br(), 

                   radioButtons("GTR_sex", "Sex", choiceValues=list("Male","Female"),
                                choiceNames=list("Male","Female"), selected=character(0), inline = T)
                 ), br(), br(),

                 fluidRow(align="center", br(), actionBttn("GTRdo", "Submit", style = "material-flat"))
               ),


               column(
                 4,
                 wellPanel(
                   style = "height:375px", 
                   h4("Tumor-related factors", align="center"), br(),

                   radioButtons("GTR_WHO", "WHO Grade", choiceValues=list("WHO-I","WHO-II", "WHO-III"),
                                choiceNames=list("WHO-I","WHO-II","WHO-III"), selected=character(0), inline=T), br(),

                   sliderInput("GTR_Ki67", "Ki-67 proliferative index", 
                               min = 0, max = 60, value = 5), br(),

                   selectInput("GTR_location", "Location",
                               c("Convexity" = "0",
                                 "Parasagittal" = "1",
                                 "Anterior skull-base" = "2",
                                 "Mid skull-base" = "3",
                                 "Posterior skull-base" = "4"))
                 )),


                 column(
                   4,
                   wellPanel(
                     style = "height:525px", 
                     h4("Treatment-related factors", align="center"), br(),

                     radioButtons("GTR_Simpson", "Simpson Grade", choiceValues=list("Grade I","Grade II", "Grade III"),
                                  choiceNames=list("Grade I","Grade II","Grade III"), selected=character(0), inline=T), br(),

                     radioButtons("GTR_EXBR", "External Beam Radiation", choiceValues=list("No","Yes"),
                                  choiceNames=list("No","Yes"), selected ="No", inline=T),
                     sliderInput("GTR_EXBRGy", "Cumulative Gy",
                                  min = 40, max = 60, value = 54.2, step = 0.2), br(),

                     radioButtons("GTR_SRS", "Stereotactic radiosurgery", choiceValues=list("No","Yes"),
                                  choiceNames=list("No","Yes"), selected ="No", inline=T),
                     sliderInput("GTR_SRSGy", "Cumulative Gy",
                                  min = 12, max = 22, value = 15, step = 1), br(),

                   )
                 )

               )
             )
    )
  )

server <- function(input, output, session) 


  GTR_rvs <- reactiveValues(prev_value = 54.2)

  observeEvent(input$GTR_EXBR, 
    if(input$GTR_EXBR == "No")
      updateSliderInput(session, "GTR_EXBRGy",min = 0, max = 0, value=0)
      GTR_rvs$prev_value <- input$GTR_EXBRGy
      disable("GTR_EXBRGy")
    else
      updateSliderInput(session, "GTR_EXBRGy",  min = 40, max = 60, value = GTR_rvs$prev_value)
      enable("GTR_EXBRGy")
    
  )

  observeEvent(input$GTR_EXBRGy, 
    print(input$GTR_EXBRGy)
  )



  GTR_rvs_srs <- reactiveValues(prev_value = 15)

  observeEvent(input$GTR_SRS, 
    if(input$GTR_SRS == "No")
      updateSliderInput(session, "GTR_SRSGy",min = 0, max = 0, value=0)
      GTR_rvs_srs$prev_value <- input$GTR_SRSGy
      disable("GTR_SRSGy")
    else
      updateSliderInput(session, "GTR_SRSGy",  min = 12, max = 22, value = GTR_rvs_srs$prev_value)
      enable("GTR_SRSGy")
    
  )

  observeEvent(input$GTR_SRSGy, 
    print(input$GTR_SRSGy)
  )








shinyApp(ui, server)
答案

这应该给您您想要的。它使用以下技术:

  • [renderUI()以格式显示错误消息
  • req()检查是否应该显示红色错误消息
  • [toggleState()仅在所有指定的单选按钮的长度都不为0时才使“提交”按钮可单击(由character(0)指定
library(shiny)
library(shinyjs)
library(shinycustomloader)
library(shinyWidgets)



ui <- fluidPage(

  useShinyjs(),


  tabsetPanel(

    # GTR
    tabPanel(title = HTML(paste(h4("Gross Total Resection"),
                                h6("Simpson Grade I-III", align = "left"))),
             br(), br(),

             fluidRow(

               column(
                 4,
                 wellPanel(
                   style = "height:275px",
                   h4("Patient-related factors", align="center"), br(),
                   sliderInput("GTR_age", "Age", 
                               min = 18, max = 100, value = 60), br(), 

                   radioButtons("GTR_sex", "Sex", choiceValues=list("Male","Female"),
                                choiceNames=list("Male","Female"), selected=character(0), inline = T)
                 ), br(), br(),

                 fluidRow(align="center", br(), actionBttn("GTRdo", "Submit", style = "material-flat"),br(),
                          uiOutput("req_text", style = "width: 200px; color: red"))
               ),


               column(
                 4,
                 wellPanel(
                   style = "height:375px", 
                   h4("Tumor-related factors", align="center"), br(),

                   radioButtons("GTR_WHO", "WHO Grade", choiceValues=list("WHO-I","WHO-II", "WHO-III"),
                                choiceNames=list("WHO-I","WHO-II","WHO-III"), selected=character(0), inline=T), br(),

                   sliderInput("GTR_Ki67", "Ki-67 proliferative index", 
                               min = 0, max = 60, value = 5), br(),

                   selectInput("GTR_location", "Location",
                               c("Convexity" = "0",
                                 "Parasagittal" = "1",
                                 "Anterior skull-base" = "2",
                                 "Mid skull-base" = "3",
                                 "Posterior skull-base" = "4"))
                 )),


               column(
                 4,
                 wellPanel(
                   style = "height:525px", 
                   h4("Treatment-related factors", align="center"), br(),

                   radioButtons("GTR_Simpson", "Simpson Grade", choiceValues=list("Grade I","Grade II", "Grade III"),
                                choiceNames=list("Grade I","Grade II","Grade III"), selected=character(0), inline=T), br(),

                   radioButtons("GTR_EXBR", "External Beam Radiation", choiceValues=list("No","Yes"),
                                choiceNames=list("No","Yes"), selected ="No", inline=T),
                   sliderInput("GTR_EXBRGy", "Cumulative Gy",
                               min = 40, max = 60, value = 54.2, step = 0.2), br(),

                   radioButtons("GTR_SRS", "Stereotactic radiosurgery", choiceValues=list("No","Yes"),
                                choiceNames=list("No","Yes"), selected ="No", inline=T),
                   sliderInput("GTR_SRSGy", "Cumulative Gy",
                               min = 12, max = 22, value = 15, step = 1), br(),

                 )
               )

             )
    )
  )
)

server <- function(input, output, session) 


  GTR_rvs <- reactiveValues(prev_value = 54.2)

  observeEvent(input$GTR_EXBR, 
    if(input$GTR_EXBR == "No")
      updateSliderInput(session, "GTR_EXBRGy",min = 0, max = 0, value=0)
      GTR_rvs$prev_value <- input$GTR_EXBRGy
      disable("GTR_EXBRGy")
    else
      updateSliderInput(session, "GTR_EXBRGy",  min = 40, max = 60, value = GTR_rvs$prev_value)
      enable("GTR_EXBRGy")
    
  )

  observeEvent(input$GTR_EXBRGy, 
    print(input$GTR_EXBRGy)
  )



  GTR_rvs_srs <- reactiveValues(prev_value = 15)

  observeEvent(input$GTR_SRS, 
    if(input$GTR_SRS == "No")
      updateSliderInput(session, "GTR_SRSGy",min = 0, max = 0, value=0)
      GTR_rvs_srs$prev_value <- input$GTR_SRSGy
      disable("GTR_SRSGy")
    else
      updateSliderInput(session, "GTR_SRSGy",  min = 12, max = 22, value = GTR_rvs_srs$prev_value)
      enable("GTR_SRSGy")
    
  )

  observeEvent(input$GTR_SRSGy, 
    print(input$GTR_SRSGy)
  )

  observe(
    toggleState(id = "GTRdo", condition = length(input$GTR_sex) > 0 &
                  length(input$GTR_WHO) > 0 &
                  length(input$GTR_Simpson) > 0)
  )

  output$req_text <- renderUI(
    req(length(input$GTR_sex) == 0 |
        length(input$GTR_WHO) == 0 |
        length(input$GTR_Simpson) == 0)

    out <- tagList(p("Please choose:"),
                   tags$ul(style = "text-align: left"))

    if(length(input$GTR_sex) == 0) 
      out[[2]] <- tagAppendChild(out[[2]],tags$li("Sex"))
    
    if(length(input$GTR_WHO) == 0) 
      out[[2]] <- tagAppendChild(out[[2]],tags$li("WHO Grade"))
    
    if(length(input$GTR_Simpson) == 0) 
      out[[2]] <- tagAppendChild(out[[2]],tags$li("Simpson Grade"))
    
    out

  )




shinyApp(ui, server)

以上是关于shiny:在空/未选中 radioButtions()的情况下,actionButton()是否可以返回错误?的主要内容,如果未能解决你的问题,请参考以下文章

闪亮:未选中复选框时,xts 图(xtsExtra)消失

何时在空实例上调用成员函数会导致 C++11 中的未定义行为? [复制]

是否在空的初始化程序列表(并明确指定类型)上调用 std::min 未定义的行为?

如果单选按钮不可见,请取消选中它

图像未在 Shiny 应用 R 中显示

R Shiny 服务器未呈现正确的 ggplot 字体系列