开始使用免费开始使用

让您的图形更大

就像输入函数会根据输入类型拥有不同的参数一样,输出占位函数也可以通过不同的参数来调整其外观或行为。

例如,在 Shiny 应用中使用 plotOutput() 显示图形时,图形的默认高度为 400 像素。plotOutput() 函数提供了一些参数,可用于修改图形的高度或宽度。

本练习是课程的一部分

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

查看课程

练习说明

已提供上一个练习中的 Shiny 应用代码。您的任务是把图形放大。具体要求:

  • 高度 600 像素,宽度 600 像素。您可以查阅 plotOutput() 文档,了解应使用哪些参数(第 18 行)。

交互式实操练习

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

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput("title", "Title", "GDP vs life exp"),
      numericInput("size", "Point size", 1, 1),
      checkboxInput("fit", "Add line of best fit", FALSE),
      colourInput("color", "Point color", value = "blue"),
      selectInput("continents", "Continents",
                  choices = levels(gapminder$continent),
                  multiple = TRUE,
                  selected = "Europe"),
      sliderInput("years", "Years",
                  min(gapminder$year), max(gapminder$year),
                  value = c(1977, 2002))
    ),
    mainPanel(
      # Make the plot 600 pixels wide and 600 pixels tall
      plotOutput("plot", ___, ___)
    )
  )
)

# Define the server logic
server <- function(input, output) {
  output$plot <- renderPlot({
    data <- subset(gapminder,
                   continent %in% input$continents &
                     year >= input$years[1] & year <= input$years[2])
    
    p <- ggplot(data, aes(gdpPercap, lifeExp)) +
      geom_point(size = input$size, col = input$color) +
      scale_x_log10() +
      ggtitle(input$title)
    
    if (input$fit) {
      p <- p + geom_smooth(method = "lm")
    }
    p
  })
}

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