R闪亮的主面板显示在错误的地方

时间:2017-04-09 08:16:44

标签: r shiny

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)

我是R shiny的新用户。我上面写了一个简单的代码,发现我的主面板不在sidebarLayout的右侧。我不知道是什么导致了这个,如果我想将它移到sidebarLayout的右侧,我该怎么办。

1 个答案:

答案 0 :(得分:1)

您只是忘记包含必要的sidebarPanel,因此您只有一个面板,因此fluidPage中的所有内容都在一列中。

您需要添加sidebarPanel,以便ui代码如下所示:

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") 
  )
)