使用闪亮的滑块存储和显示精度

时间:2019-07-09 21:36:12

标签: r shiny

我知道sliderInput()的值将基于滑块的步长为round。  但是,是否有一种优雅的方法将值存储到sliderInput()而不是显示的值上?到目前为止,我提出的解决方案是不完善的,并且在我看时没有看到任何其他主题的SO线程。

MWE:

library(shiny)

ui <- fluidPage(
    sidebarPanel(
        sliderInput("alpha", label = "Var 1", min = 0, max = 12, step = 1, value = 0),
        actionButton("go", "Set to Mean")
    ),
    mainPanel(
        textOutput("alphaTrueVal"),
        conditionalPanel("input.go !=0",
            textOutput("alphaSliderVal")
        )
    )
)

server <- function(input, output, session){   
    # Gen some fake integer data with a non-integer mean
    data <- eventReactive(input$go, {
        x <- sample(0:12, 75, replace=T)
    })

    output$alphaTrueVal <- renderText({
        paste0("Var's true mean: ", mean(data()))
    })

    output$alphaSliderVal <- renderText({
        paste0("Var's slider val (when mean set): ", input$alpha)
    })  

    # To update to mean
    observeEvent(input$go, {
        updateSliderInput(session, "alpha",
            value=mean(data())        
        )
    })      
}

shinyApp(ui, server)

理想情况下,滑块可以可视地显示舍入的整数,但是当服务器查询滑块的值时,它将与均值匹配。情况不是这样。

到目前为止,两种不完善的解决方案:

  1. 使用updateSliderInput()调整滑块的步长:
library(shiny)

ui <- fluidPage(
    sidebarPanel(
        sliderInput("alpha", label = "Var 1", min = 0, max = 12, step = 1, value = 0),
        actionButton("go", "Set to Mean")
    ),
    mainPanel(
        textOutput("alphaTrueVal"),
        conditionalPanel("input.go !=0",
            textOutput("alphaSliderVal")
        )
    )
)

server <- function(input, output, session){   
    # Gen some fake data with a non-integer mean
    data <- eventReactive(input$go, {
        x <- sample(0:12, 75, replace=T)
    })

    output$alphaTrueVal <- renderText({
        paste0("Var's true mean: ", mean(data()))
    })

    output$alphaSliderVal <- renderText({
        paste0("Var's slider val (when mean set): ", input$alpha)
    })

    # "Solution" 1
    observeEvent(input$go, {
        updateSliderInput(session, "alpha",
            step=0.00001, value=mean(data())    
        )
    })  
}

shinyApp(ui, server)

但是,当用户随后与之交互时,该滑块的渐变非常精细。

  1. shinyWidgets'sliderTextInput()

还没有写出有效的代码,但似乎您可以通过通过sliderTextInput()插入均值来更改updateSliderTextInput()的开始选择。如果初始值为:

choices = c=("0", "1", "2", "3", "4") 

平均值为2.3,将其插入初始选择集,将所得矢量按数字排序,然后将其重新转换为字符,然后将其传递回updateSliderTextInput(),如下所示:

choices = c=("0", "1", "2", "2.3", "3", "4")

问题我看到,当2.3与2和3不等距时,所有点的间距都将相等。(从某种意义上说,鉴于事物的存储方式,而不是我的目标。)< / p>

0 个答案:

没有答案