เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Update output (server)

You are almost there! The final step is to update the plot output to display a line plot of prop vs. year, colored by sex, for the name that was input by the user. You can use this plot template to create your plot:

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

Recall that a user input named foo can be accessed as input$foo in the server. We have already pre-loaded the shiny and ggplot2 packages, as well as the babynames dataset.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Building Web Applications with Shiny in R

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Add the plotting code inside renderPlot(). Make sure to replace the hard-coded name (name == "David") with the name that was input by the user.

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

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)
แก้ไขและรันโค้ด