ComenzarEmpieza gratis

Hacer tu gráfico más grande

Al igual que las funciones de entrada pueden tener distintos argumentos según el tipo de entrada, las funciones contenedoras de salida también pueden tener diferentes argumentos para modificar su apariencia o su comportamiento.

Por ejemplo, cuando muestras un gráfico en una app Shiny usando plotOutput(), la altura del gráfico por defecto será de 400 píxeles. La función plotOutput() incluye algunos parámetros que puedes utilizar para modificar la altura o la anchura del gráfico.

Este ejercicio forma parte del curso

Casos prácticos: crea aplicaciones web con Shiny en R

Ver curso

Instrucciones del ejercicio

Tienes el código de la app Shiny del ejercicio anterior. Tu tarea es hacer el gráfico más grande. En concreto:

  • 600 píxeles de alto y 600 píxeles de ancho. Puedes consultar la documentación de plotOutput() para averiguar qué parámetros usar (línea 18).

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

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)
Editar y ejecutar código