Fonte vs. Condutor vs. Ponto final
A mágica por trás do Shiny é impulsionada pela reatividade. Como você viu nesta lição, existem três tipos de componentes reativos em um app Shiny.
- Fonte reativa: entrada do usuário que vem, em geral, por uma interface de navegador.
- Condutor reativo: componente reativo entre uma fonte e um ponto final, normalmente usado para encapsular cálculos lentos.
- Ponto final reativo: algo que aparece na janela do navegador do usuário, como um gráfico ou uma tabela de valores.
ui <- fluidPage(
titlePanel('BMI Calculator'),
theme = shinythemes::shinytheme('cosmo'),
sidebarLayout(
sidebarPanel(
numericInput('height', 'Enter your height in meters', 1.5, 1, 2),
numericInput('weight', 'Enter your weight in Kilograms', 60, 45, 120)
),
mainPanel(
textOutput("bmi"),
textOutput("bmi_range")
)
)
)
server <- function(input, output, session) {
rval_bmi <- reactive({
input$weight/(input$height^2)
})
output$bmi <- renderText({
bmi <- rval_bmi()
paste("Your BMI is", round(bmi, 1))
})
output$bmi_range <- renderText({
bmi <- rval_bmi()
health_status <- cut(bmi,
breaks = c(0, 18.5, 24.9, 29.9, 40),
labels = c('underweight', 'healthy', 'overweight', 'obese')
)
paste("You are", health_status)
})
}
shinyApp(ui, server)
Neste exercício, você verá um conjunto de componentes reativos. Classifique cada um como fonte, condutor ou ponto final reativo.
Este exercício faz parte do curso
Construindo Aplicações Web com Shiny em R
Exercício interativo prático
Transforme a teoria em ação com um de nossos exercícios interativos
Começar o exercício