简体   繁体   中英

Shiny R Moving Sidebar

I have a side bar as follows

  sidebarPanel(
    wellPanel(

      list(tags$head(tags$style("body {background-color: #E0F2F7; }"))),


      helpText("Choose a stock ticker to examine, For example
               ^HSI - Hang Seng, 
               ^N225 - Nikkei 225 and 
               ^FTSE - FTSE 100. 
               Information will be collected from",tags$a(href="https://uk.finance.yahoo.com/lookup", "yahoo finance"),"."),

      textInput("symb", "Symbol", "^FTSE"),
      bsAlert(inputId = "alert_anchor"),

      dateRangeInput("dates", 
                     "Date range",
                     start = "2015-01-01", 
                     end = as.character(Sys.Date())),
      textOutput("DateRange"),
      div(style="display:inline-block",submitButton("Analysis")),
      div(style="display:inline-block",downloadButton('downloadData', 'Download Data')),width=6
      ))

Which gives the following (side bar is on the left)在此处输入图像描述

However I wanted it so that the sidebar follows the page as the user scrolls up and down instead of there being a blank blue space on the left as shown.

在此处输入图像描述

Is this possible to do on R Shiny? If so, how can it be done?

You can add a style argument to sidebarPanel , which sets the element position to fixed . Below is the Shiny default minimal example with minor modifications to illustrate the behavior.

library(shiny)

ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(
      style = "position:fixed; width:33%;", # Add this line to your code!
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
      
    ),
    mainPanel(
      lapply(1:10, function(i) {
        plotOutput(outputId = paste("distPlot", i, sep = "_"))
      })
      
    )
  )
)

server <- function(input, output) {
  
  lapply(1:10, function(i) {
    
    output[[paste("distPlot", i, sep = "_")]] <- renderPlot({
      set.seed(i)
      x    <- rnorm(n = 1000)
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      
      hist(x, breaks = bins, col = "#75AADB", border = "white",
           xlab = "Waiting time to next eruption (in mins)",
           main = "Histogram of waiting times")
      
    })
  })
}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM