EmpezarEmpieza gratis

Convierte la estatura de pulgadas a centímetros

Antes en el capítulo, practicamos cómo detener, retrasar y activar apps. Este es un patrón muy común de programación en Shiny que permite optimizar tus apps para que sean rápidas (y solo se vuelvan a ejecutar cuando algo se actualiza y tu usuario quiere volver a ejecutar la app).

En este ejercicio, volverás a practicar esos conceptos para afianzarlos. En lugar de calcular el IMC, esta app convierte la estatura en pulgadas a centímetros.

Este ejercicio forma parte del curso

Creación de aplicaciones web con Shiny en R

Ver curso

Instrucciones del ejercicio

  • Server: Retrasa el cálculo de la estatura en cm hasta que la persona usuaria haga clic en el botón 'Show height in cm'.

ejercicio interactivo práctico

Prueba este ejercicio completando este código de ejemplo.

ui <- fluidPage(
  titlePanel("Inches to Centimeters Conversion"),
  sidebarLayout(
    sidebarPanel(
      numericInput("height", "Height (in)", 60),
      actionButton("show_height_cm", "Show height in cm")
    ),
    mainPanel(
      textOutput("height_cm")
    )
  )
)

server <- function(input, output, session) {
  # MODIFY CODE BELOW: Delay the height calculation until
  # the show button is pressed
  rval_height_cm <- reactive({
    input$height * 2.54
  })
  
  output$height_cm <- renderText({
    height_cm <- rval_height_cm()
    	paste("Your height in centimeters is", height_cm, "cm")
    })
}

shinyApp(ui = ui, server = server)
Editar y ejecutar código