Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny - Display static text outside sidebar panel

Tags:

r

shiny

I want to display static text outside of the sidebar panel in Shiny. I am able to display the text inside the sidebar panel. However, if I try to display text outside of the sidebar panel then I get this error: "Error in match.arg: 'arg' must be NULL or a character vector".

Below is an example code which displays the sentence "This is a static text" inside the sidebar panel. I want to display the text "just below" the sidebar panel but not inside the panel window.

The code below gives me this output:

this output but I want it to look like this:

like this How do I achieve this?

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         h5("This is a static text")
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })

}

# Run the application 
shinyApp(ui = ui, server = server)
like image 207
Siddharth Gosalia Avatar asked Sep 27 '18 19:09

Siddharth Gosalia


Video Answer


2 Answers

The sidebarPanel function will put everything inside a form with class well. One hacky solution (maybe there is a better one) is to create a custom function siderbarPanelfunction to put elements outside the form. Below is your code with the function sidebarPanel2 that is just a customization of the original function to put element "just below". You can put anything, not just text.

library(shiny)

sidebarPanel2 <- function (..., out = NULL, width = 4) 
{
  div(class = paste0("col-sm-", width), 
    tags$form(class = "well", ...),
    out
  )
}

# Define UI for application that draws a histogram
ui <- fluidPage(
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel2(fluid = FALSE,
      sliderInput("bins",
                 "Number of bins:",
                 min = 1,
                 max = 50,
                 value = 30),
      out = h5("This is a static text")
    ),
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)
like image 119
Geovany Avatar answered Oct 24 '22 01:10

Geovany


One alternative: you could not use sidebarLayout() at all, and just define your own shiny-input-container styles (see this answer for more examples adjusting slider component styles). Then you can make more complex arrangements of input widgets, text, and plots with fluidRow() and column() nesting (example).

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  #CSS Styles
  tags$style(HTML(
    paste0(".shiny-input-container {background-color: #f5f5f5; border: 1px solid #e3e3e3;",
           "padding-left: 10px; padding-right: 10px; border-radius: 3px;}")
           )),

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  #use column to mimic sidebarPanel
  column(
    width = 4,
    sliderInput("bins",
                "Number of bins:",
                min = 1,
                max = 50,
                value = 30),    
    hr(),
    h5("This is a static text")
  ),

  #use column to mimic main panel
  column(
    width = 8,
    plotOutput("distPlot")
  )

)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })

}

# Run the application 
shinyApp(ui = ui, server = server)

Created on 2018-09-28 by the reprex package (v0.2.1)

like image 26
DanTan Avatar answered Oct 24 '22 02:10

DanTan