如何从Shiny中的自定义离子范围滑块获取当前值?

时间:2018-03-08 14:41:44

标签: javascript shiny slider labels ion-range-slider

目的:我需要一种方法将自定义标签(0,0.33,0.66,1)添加到Shiny滑块。我修改了以下帖子中的代码:R Shiny date slider animation by month (currently by day)

问题:如何将当前滑块值传递给Shiny对象?我尝试使用此帖子修改我的代码,但未成功:http://ionden.com/a/plugins/ion.rangeslider/demo_interactions.html

请注意,我是Javascript的新手,我的代码不能包含任何其他软件包。

UI

# ui
library(shiny)
shinyUI(fluidPage(
  br(),
  uiOutput("mySlider"),
  verbatimTextOutput("myValue"))
)

服务器

# server    
x <- c(0,0.33,0.66,1) # slider values

# function to create slider
sliderValues <- function (inputId, values){

  sliderTag <- tags$div(
    do.call(tags$input, list(type = "single",
                             id = "inputId",
                             value = x)),
  tags$script(
    HTML('$("#mySlider").ionRangeSlider({
         grid: true,
         values: [0,0.33,0.66,1]});'))
    )

  dep <- htmltools::htmlDependency(
    "ionrangeslider",
    "2.1.12",
    c(href = "shared/ionrangeslider"),
    script = "js/ion.rangeSlider.min.js",
    stylesheet = c(
      "css/ion.rangeSlider.css",
      "css/ion.rangeSlider.skinShiny.css"
    ))
  htmltools::attachDependencies(sliderTag, dep)
}

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

  output$mySlider <- renderUI({
    sliderValues(inputId = "mySlider",
                 values = x)
  })

  output$myValue <- renderPrint({
    print(as.numeric(unlist(input$mySlider)))
  })
}

0 个答案:

没有答案