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

ทำให้กราฟมีขนาดใหญ่ขึ้น

ฟังก์ชัน input แต่ละประเภทมีอาร์กิวเมนต์ที่แตกต่างกัน ฟังก์ชัน output placeholder ก็เช่นกัน — สามารถรับอาร์กิวเมนต์เพิ่มเติมเพื่อปรับรูปลักษณ์หรือพฤติกรรมได้

ตัวอย่างเช่น เมื่อแสดงกราฟใน Shiny app ด้วย plotOutput() ความสูงของกราฟจะถูกกำหนดไว้ที่ 400 พิกเซลโดยค่าเริ่มต้น ฟังก์ชัน plotOutput() มีพารามิเตอร์ที่ใช้ปรับความสูงและความกว้างของกราฟได้

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

กรณีศึกษา: การสร้างเว็บแอปพลิเคชันด้วย Shiny ใน R

ดูคอร์ส

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

โค้ดของ Shiny app จากแบบฝึกหัดที่แล้วได้เตรียมไว้ให้แล้ว ให้ปรับขนาดกราฟให้ใหญ่ขึ้น โดยกำหนดดังนี้

  • ความสูง 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)
แก้ไขและรันโค้ด