Shiny: Is it possible to make a vertical slider?

2020-07-09 03:38发布

问题:

Is there any possibility to make a vertical slider in Shiny? I would basically want a plot, a vertical slider at its left, and a normal horizontal slider below it.

回答1:

There are ways to do it manually. You would need to create a custom js.

Here is a crazy shinny app where everything rotates

    #Libs
    require(c('shiny'))
    js<-"$(function() {
        var $elie = $('div');
        rotate(25);
        function rotate(degree) {        
            $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});  
            $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});
            timer = setTimeout(function() {
                rotate(++degree);
            },100);
        }
    });"


    renderInputs <- function(prefix) {
      wellPanel(
        fluidRow(
          column(3,
                 sliderInput(paste0(prefix, "_", "n_obs"), "View a specific date", min = as.Date('1980-05-15'), max = Sys.Date(), value = as.Date('2000-01-01'),
                 #sliderInput("date_range", "Choose Date Range:", min = as.Date("2016-02-01"), max = Sys.Date(), value = c(as.Date("2016-02-25"), Sys.Date())
                 ),
                 verbatimTextOutput("info")
          ),
          column(9,
                 plotOutput("plot1",
                            click = "plot_click",
                            dblclick = "plot_dblclick",
                            hover = "plot_hover",
                            brush = "plot_brush")
          )
        )
      )
    }

    ui <- shinyUI(fluidPage(theme="simplex.min.css",
                            tags$style(type="text/css",
                                       "label {font-size: 12px;}",
                                       ".recalculating {opacity: 1.0;}"
                            ),
                            tags$head(
                              tags$style(HTML("
                                              @import url('//fonts.googleapis.com/css?family=Lobster|Cabin:400,700');

                                              h1 {
                                              font-family: 'Lobster', cursive;
                                              font-weight: 500;
                                              line-height: 1.1;
                                              color: #48ca3b;
                                              }

                                              ")),
                              tags$script(HTML(js))
                              ),

                            # Application title
                            tags$h2("Google!!!"),
                            p("An adaptation of the",
                              tags$a(href="https://google.com", "Google"),
                              "from",
                              tags$a(href="https://duckduckgo.com/", "DuckDuckGo"),
                              "to get the best possible results without selling yourself"),
                            hr(),

                            fluidRow(
                              column(6, tags$h3("Scenario A")),
                              column(6, tags$h3("Scenario B"))
                            ),
                            fluidRow(
                              column(12, renderInputs("a"))
                            ),
                            fluidRow(
                              column(6,
                                     plotOutput("a_distPlot", height = "600px")
                              ),
                              column(6,
                                     plotOutput("b_distPlot", height = "600px")
                              )
                            )
                              )
                  )


    server <- function(input, output) {
      output$plot1 <- renderPlot({
        plot(mtcars$wt, mtcars$mpg)
      })

      output$info <- renderText({
        xy_str <- function(e) {
          if(is.null(e)) return("NULL\n")
          paste0("x=", round(e$x, 1), " y=", round(e$y, 1), "\n")
        }
        xy_range_str <- function(e) {
          if(is.null(e)) return("NULL\n")
          paste0("xmin=", round(e$xmin, 1), " xmax=", round(e$xmax, 1), 
                 " ymin=", round(e$ymin, 1), " ymax=", round(e$ymax, 1))
        }

        paste0(
          "click: ", xy_str(input$plot_click),
          "dblclick: ", xy_str(input$plot_dblclick),
          "hover: ", xy_str(input$plot_hover),
          "brush: ", xy_range_str(input$plot_brush)
        )
      })
    }

    shinyApp(ui, server)


    ################################

If you want it to work rotate only one element you need to modify the js like this:

    js<-"$(function() {
      var $elie = $(document.getElementsByClassName('form-group shiny-input-container'));
    rotate(270);
    function rotate(degree) {        
        $elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});  
        $elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});
    }
});"

It still needs a bit of work to fix the functionality like the sliding and the padding of the div and adding a custom id on all the elements you wish to rotate:thus making sure you wont apply the js on element you do not want to and get the mess from the first example, but it should be a good starting point.



回答2:

I was looking for it too, and I think it is not implemented. In the shiny discussion group there is a similar question from november 2014, and it`s still open.

https://groups.google.com/forum/#!searchin/shiny-discuss/vertical$20slider

Searching the official gallery and the web, I couldn`t find any example of it. All sliders are horizontals. Always.



标签: r slider shiny