开始使用免费开始使用

更改点大小:数值输入

数值输入相比文本输入还有一些额外参数,例如 minmax,用于限定可选择的最小值和最大值。

请注意,在服务器代码中访问输入值时,Shiny 会根据使用的输入类型自动返回相应类型的对象。也就是说,如果您有一个 ID 为 "foo" 的数值输入,那么 input$foo 将返回一个数值。

本练习是课程的一部分

案例研究:使用 R 的 Shiny 构建 Web 应用

查看课程

练习说明

上一个练习中的 Shiny 应用代码已提供。您的任务是添加一个数值输入,让用户可以更改图中点的大小。具体要求:

  • 在 UI 中添加一个数值输入,ID 为 "size",标签为 "Point size",默认值为 1,最小值为 1。
  • 在服务器端添加代码,使该数值输入决定绘图中的点大小(第 20 行)。

交互式实操练习

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

# Define UI for the application
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput("title", "Title", "GDP vs life exp"),
      # Add a size numeric input
      ___
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

# Define the server logic
server <- function(input, output) {
  output$plot <- renderPlot({
    ggplot(gapminder, aes(gdpPercap, lifeExp)) +
      # Use the size input as the plot point size
      geom_point(size = ___) +
      scale_x_log10() +
      ggtitle(input$title)
  })
}

# Run the application
shinyApp(ui = ui, server = server)
编辑并运行代码