【问题标题】:Shiny Slider Customized Values闪亮的滑块自定义值
【发布时间】:2018-01-28 07:43:35
【问题描述】:

我正在尝试为闪亮的滑块(1、5、10、15、20、25 和 30)设置自定义值。我试过step,但结果是(0,5,10,15,20,25,30)或(1,6,11,16,21,26,31)。有什么方法可以为滑块定义自定义值吗? 谢谢!

plotpath <- "/Volumes/share-ites-1-$/Projects/Scientifica/Simulations_Scientifica"

ui <- fluidPage(
     titlePanel("LandClim Simulations"),
     sidebarLayout(
          sidebarPanel(
               sliderInput(inputId = "temp", 
                           label = "Temperature increase:", 
                           value = 1, min = 1, max = 2,
                           step = 1, animate = TRUE ),
               sliderInput(inputId = "prec", 
                           label = "Precipitation change:", 
                           value = 0, min = -2, max = 2,
                           step = 1, animate = TRUE ),
               sliderInput(inputId = "decade", 
                           label = "Time (decade):", 
                           value = 1, min = 0, max = 30,
                           step = 5, animate = TRUE )
          ),
          mainPanel(imageOutput("image")) 
     )
)

server <- function(input, output) {
     output$image <- renderImage( deleteFile = FALSE, {
               return(list(
                    src = paste(plotpath,"/Temp",input$temp,"Prec",input$prec,"Dec",input$decade,".png",sep = ""),
                    contentType = "image/png"))
     } ) }

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    shinyWidgets 包现在通过一个允许自定义值的滑块为您解决了这个问题。第三个滑块的更新代码如下所示:

    shinyWidgets::sliderTextInput(inputId = "decade", 
      label = "Time (decade):", 
      choices = c(1,5,10,15,20,25,30))
    

    【讨论】:

      【解决方案2】:

      这可以使用shiny 中的sliderTextInput 函数轻松完成。无需添加所有这些复杂的 js 函数。只需几行代码就可以解决问题。安装包含sliderTextInput 函数的shinywidgets 包。执行以下操作:

        sliderTextInput("decade","Time (decade):, 
                        choices = c(1,5,10,15,20,25,30), 
                        selected = c(1,5,10,15,20,25,30), 
                        animate = FALSE, grid = FALSE, 
                        hide_min_max = FALSE, from_fixed = FALSE,
                        to_fixed = FALSE, from_min = NULL, from_max = NULL, to_min = NULL,
                        to_max = NULL, force_edges = FALSE, width = NULL, pre = NULL,
                        post = NULL, dragRange = TRUE)
      

      【讨论】:

        【解决方案3】:

        虽然 Shiny 提供了自定义滑块输入的选项,但我认为没有办法以 (1,5,10...) 的形式获得输出。这是因为您的第 1 点和第 2 点之间的差异为 4,之后为 5,这将与 step 参数的工作方式不一致。Step 只能根据滑块输入之间的恒定差异生成数字。

        【讨论】:

        • 我知道 step 无法解决我的问题,但我认为没有其他方法可以解决。谢谢。
        【解决方案4】:

        根据需要有一个滑块,输入带有可变中断,您可以使用其他选项之一,例如

        选择输入

        https://shiny.rstudio.com/reference/shiny/latest/selectInput.html

        甚至是复选框输入

        https://shiny.rstudio.com/reference/shiny/latest/checkboxInput.html

        【讨论】:

        • 我需要使用滑块。
        • 很公平。如果将“sliderInput”放入终端,则可以获取函数详细信息,找到以“step
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-11
        • 2020-09-13
        • 2023-02-15
        • 2023-04-01
        • 2017-04-01
        • 1970-01-01
        • 2022-01-15
        相关资源
        最近更新 更多