开始使用免费开始使用

更新布局(UI)

您可以使用 Shiny 提供的 layout 函数来排列 UI 元素。本例中,我们将使用 sidebarLayout():把输入放在 sidebarPanel() 中,把输出放在 mainPanel() 中。您可以用下面的模板来更新应用的布局。

sidebarLayout(
  sidebarPanel(p("This goes into the sidebar on the left")),
  mainPanel(p("This goes into the panel on the right"))
)

我们已为您预先加载了 shinyggplot2 包。请注意,p('hello') 会返回一段包含文本 "hello" 的 HTML 段落。

本练习是课程的一部分

使用 R 构建 Shiny Web 应用

查看课程

练习说明

  • textInput() 放入 sidebarPanel() 中。
  • plotOutput() 放入 mainPanel() 中。
  • 将这两个面板一起放入侧边栏布局:sidebarLayout(___ , ___)

交互式实操练习

通过完成这段示例代码来试试这个练习。

ui <- fluidPage(
  titlePanel("Baby Name Explorer"),
  # CODE BELOW: Add a sidebarLayout, sidebarPanel, and mainPanel
  
  textInput('name', 'Enter Name', 'David'),
  plotOutput('trend')
  
)

server <- function(input, output, session) {
  output$trend <- renderPlot({
    ggplot()
  })
}

shinyApp(ui = ui, server = server)
编辑并运行代码