shiny:可以将updateSliderInput()与基于radioButton()的限制滑动范围结合吗?

问题描述 投票:1回答:1

我正在生成包含appradioButton()updateSliderInput()

radioButton()对应于患者是否接受了放射治疗"Yes"/"No"updateSliderInput对应于在radioButton=="Yes"的情况下接收到的辐射剂量,其范围在4060之间,并带有step=0.2。逻辑上,如果为radioButton=="no",则为updateSliderInput==0。因此,用户绝对不能访问>0<40之间的范围。

问题:如何将(1)sliderInput==0组合为radioButton=="No",将(2)sliderInput==40 to 60组合为radioButton=="Yes"

我已经寻求一种将sliderInput-functionobserveEvent()updateSliderInput组合在一起的解决方案。但是,如果是全新的,则非常欢迎常规反馈和其他解决方案。

重要的是,不能选择值>0<40。因此,ticksslider-axis-values范围内的>0<40不应显示在sliderInput

预期输出:enter image description here

enter image description here

library(shiny)
library(shinyjs)



sliderInput2 <- function(inputId, label, min, max, value, step=NULL, from_min, from_max){
  x <- sliderInput(inputId, label, min, max, value, step)
  x$children[[2]]$attribs <- c(x$children[[2]]$attribs, 
                               "data-from-min" = from_min, 
                               "data-from-max" = from_max, 
                               "data-from-shadow" = TRUE)
  x
}


ui <- fluidPage(

  useShinyjs(),

  radioButtons("EXBR", "External Beam Radiation", choiceValues=list("No","Yes"),
               choiceNames=list("No","Yes"), selected ="No", inline=T),
  sliderInput2("EXBRGy", "Cumulative Gy",
               min = 0, max = 60, value = 54.2, step = 0.2, from_min = 40, from_max = 60
  )
)

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

  observeEvent(input$EXBR, {
    if((input$EXBR == "No")){
      disable("EXBRGy")
    }else{
      enable("EXBRGy")
    }
  })


  rvs <- reactiveValues(EXBR = "No")


  observeEvent(input$EXBR, {
    if ((input$EXBR == "No")) {
      updateSliderInput(session, "EXBRGy", value=0)
    }
    rvs$EXBR <- input$EXBR
  })




}

shinyApp(ui, server)
r shiny uislider shinyapps
1个回答
0
投票

您可以再次更新滑块,如下所示:

library(shiny)
library(shinyjs)

sliderInput2 <- function(inputId, label, min, max, value, step=NULL, from_min, from_max){
    x <- sliderInput(inputId, label, min, max, value, step)
    x$children[[2]]$attribs <- c(x$children[[2]]$attribs,
                                 "data-from-min" = from_min,
                                 "data-from-max" = from_max,
                                 "data-from-shadow" = TRUE)
    x
}

ui <- fluidPage(
    useShinyjs(),
    radioButtons("EXBR", "External Beam Radiation", choiceValues=list("No","Yes"),
                 choiceNames=list("No","Yes"), selected ="No", inline=T),
    sliderInput2("EXBRGy", "Cumulative Gy",
                 min = 0, max = 60, value = 54.2, step = 0.2, from_min = 40, from_max = 60
    )
)

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

    rvs <- reactiveValues(prev_value = 54.2)

    observeEvent(input$EXBR, {
        if(input$EXBR == "No"){
            updateSliderInput(session, "EXBRGy",min = 0, max = 0, value=0)
            rvs$prev_value <- input$EXBRGy
            disable("EXBRGy")
        }else{
            updateSliderInput(session, "EXBRGy",  min = 0, max = 60, value = rvs$prev_value)
            enable("EXBRGy")
        }
    })

    observeEvent(input$EXBRGy, {
        print(input$EXBRGy)
    })
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.