Bin
Bin

Reputation: 547

R shiny main panel display in wrong place

library(shiny)

ui <- fluidPage(
  titlePanel("Slider App"),
  sidebarLayout(
    h1("Move the slider!"),
    sliderInput(inputId = "n", label = "Sample size", 
                min = 10, max = 1000, value = 30) 
    ),
  mainPanel(
    h3('Illustrating outputs'),
    h4('mean of random normal sample'),
    textOutput(outputId = "output_mean" ),
    h4('variance of random normal sample'),
    textOutput(outputId = "output_var"),
    h4('histogram of random normal sample'),
    plotOutput(outputId = "output_hist") 
    )
) 


server <- function(input, output) {
  output$output_hist <- renderPlot({
    set.seed(1221)
    sample <- rnorm(input$n)
    hist(sample) 
    })
  output$output_mean <- renderText({
    set.seed(1221)
    sample <- rnorm(input$n)
    mean(sample) 
  })
  output$output_var <- renderText({
    set.seed(1221)
    sample <- rnorm(input$n)
    var(sample) 
  })
} 

shinyApp(ui = ui, server = server)

I am a new user of R shiny. I wrote a simple code as above, and found out that my main panel are not on the right side of sidebarLayout. I do not know what leads to this and what should I do if I want to move it to the right side of sidebarLayout.

Upvotes: 1

Views: 1452

Answers (1)

Mike Wise
Mike Wise

Reputation: 22807

You just forgot to include the necessary sidebarPanel, so you only have one panel, thus everything in your fluidPage is in one column.

You need to add that sidebarPanel so that your ui code looks like this:

ui <- fluidPage(
  titlePanel("Slider App"),
  sidebarPanel(
    sidebarLayout(
      h1("Move the slider!"),
      sliderInput(inputId = "n", label = "Sample size", 
                  min = 10, max = 1000, value = 30) 
    )
  ),
  mainPanel(
    h3('Illustrating outputs'),
    h4('mean of random normal sample'),
    textOutput(outputId = "output_mean" ),
    h4('variance of random normal sample'),
    textOutput(outputId = "output_var"),
    h4('histogram of random normal sample'),
    plotOutput(outputId = "output_hist") 
  )
) 

Upvotes: 1

Related Questions