开始使用免费开始使用

更新输出(server)

就差最后一步了!现在请更新图形输出,显示用户输入的 name 对应的 propyear 的折线图,并按 sex 着色。您可以用下面的模板来创建绘图:

ggplot(subset(babynames, name == "David")) +
  geom_line(aes(x = year, y = prop, color = sex))

请回忆,名为 foo 的用户输入可在 server 中通过 input$foo 访问。我们已预先加载了 shinyggplot2 包,以及 babynames 数据集。

本练习是课程的一部分

使用 R 构建 Shiny Web 应用

查看课程

练习说明

  • 将绘图代码添加到 renderPlot() 中。务必将硬编码的姓名(name == "David")替换为用户输入的姓名。

交互式实操练习

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

ui <- fluidPage(
  titlePanel("Baby Name Explorer"),
  sidebarLayout(
    sidebarPanel(textInput('name', 'Enter Name', 'David')),
    mainPanel(plotOutput('trend'))
  )
)

server <- function(input, output, session) {
  output$trend <- renderPlot({
    # CODE BELOW: Update to display a line plot of the input name
    ggplot()
    
  })
}

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