簡體   English   中英

使用帶有范圍的 updateSliderInput 更新 Shiny 滑塊輸入

[英]Updating Shiny sliderInput using updateSliderInput with range

我有一個sliderInput (接收器),我想從另一個sliderInput (控制器)更新它。 當我只為updateSliderInput中的 value 參數傳遞一個輸入時,我的代碼可以工作,但是如果我為接收器的范圍傳遞一個向量,它就不起作用。

在下面的代碼中,我提供了工作和不工作的兩個滑塊。 我怎樣才能讓updateSliderInput接受一個范圍的值向量?

library(shiny)

shinyApp(

  ui <- fluidPage(
    fluidRow(
        column(4, sliderInput("controller", "Controller:", min = 0, max = 50, value = c(10,20)))
        ,column(4, sliderInput("receiver1", "Receiver 1 - Works with 1 value input", min=0, max=20, value=10, step=1))
        ,column(4, sliderInput("receiver2", "Receiver 2 - Doesn't work with 2 values in a vector", min=0, max=20, value=10, step=1))
    )

    ,fluidRow(column(4, verbatimTextOutput("range1")))
    ,fluidRow(column(4, verbatimTextOutput("range2")))

  )

  ,server <- function(input, output, session) {
    observe({
      updateSliderInput(session = session, "receiver1", value = c(input$controller[1]), min = 0, max = 100)
      updateSliderInput(session = session, "receiver2", value = c(input$controller[1],input$controller[2]), min = 0, max = 100)
    })

    output$range1 <- renderPrint({input$controller[1]})
    output$range2 <- renderPrint({input$controller[2]})
  }

)

您在ui中為receiver1receiver2提供了一個值,因此它們只接受updateSliderInput中的一個值。 如果您在receiver2中放置一個包含兩個值的向量,它將在controller中接受updateSliderInput的兩個值。

library(shiny)

shinyApp(

  ui <- fluidPage(
    fluidRow(
      column(4, sliderInput("controller", "Controller:", min = 0, max = 50, value = c(10,20)))
      ,column(4, sliderInput("receiver1", "Receiver 1 - Works with 1 value input", min=0, max=20, value=10, step=1))
      ,column(4, sliderInput("receiver2", "Receiver 2 - Doesn't work with 2 values in a vector", min=0, max=20, value=c(0, 10), step=1))
    )

    ,fluidRow(column(4, verbatimTextOutput("range1")))
    ,fluidRow(column(4, verbatimTextOutput("range2")))

  )

  ,server <- function(input, output, session) {
    observe({
      updateSliderInput(session = session, "receiver1", value = c(input$controller[1]), min = 0, max = 100)
      updateSliderInput(session = session, "receiver2", value = c(input$controller[1],input$controller[2]), min = 0, max = 100)
    })

    output$range1 <- renderPrint({input$controller[1]})
    output$range2 <- renderPrint({input$controller[2]})
  }

)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM