r shiny slider input round

风流意气都作罢 提交于 2019-12-10 18:49:47

问题


I have a problem with the R shiny slider input. The "Round feature" does not work as you can see in in this picture. Did I do something wrong ?

  sliderInput("Er", "Choose expected return (in percent)",
              min = min, max = max, value = min , round = -1,
              sep = "" , post = "%", ticks = FALSE
          )

回答1:


You have to specify a step for rounding to work:

library(shiny)
min_Er <- 20.31
max_Er <- 23.59
shinyApp( ui = fluidPage(sliderInput("Er1", "Rounding doesn't work", 
                                     round = -2, step = NULL,
                                     min = min_Er, 
                                     max = max_Er,
                                     value = min_Er,
                                     sep = "" , post = "%", ticks = FALSE),

                         sliderInput("Er2", "Rounding works",  
                                     round = -2, step = 0.01,
                                     min = min_Er, 
                                     max = max_Er,
                                     value = min_Er,
                                     sep = "" , post = "%", ticks = FALSE)
), server=function(input, output, session){
  observe(print(input$Er1))
  observe(print(input$Er2))
})

Otherwise, as commented by @Ryan Morton, if you use integers for min and max, rounding will work even if step = NULL:

library(shiny)
min_Er <- 20.31
max_Er <- 23.59
shinyApp(ui = fluidPage(sliderInput("Er1", "Rounding doesn't work", 
                                    round = TRUE,
                                    min = min_Er, 
                                    max = max_Er,
                                    value = min_Er, 
                                    sep = "" , post = "%", ticks = FALSE),

                        sliderInput("Er2", "Rounding works",  
                                    round = TRUE,
                                    min = floor(min_Er), 
                                    max = ceiling(max_Er),
                                    value = min_Er,
                                    sep = "" , post = "%", ticks = FALSE)
), server=function(input, output, session){
  observe(print(input$Er1))
  observe(print(input$Er2))
})



来源:https://stackoverflow.com/questions/42675102/r-shiny-slider-input-round

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!