開始使用免費開始

把你的圖變大

就像輸入函式會依輸入型別而有不同參數一樣,輸出佔位函式也有各種參數可用來調整外觀或行為。

例如,在 Shiny 應用程式中使用 plotOutput() 顯示圖形時,預設高度為 400 像素。plotOutput() 提供一些參數,可用來修改圖形的高度或寬度。

本練習屬於課程

案例研究:使用 R 的 Shiny 建置網頁應用程式

檢視課程

練習說明

已提供上一個練習中的 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)
編輯並執行程式碼