Reaktif bir ifade ekle
Reaktif ifade, widget girdilerini kullanan ve bir değer döndüren bir R ifadesidir. Orijinal widget her değiştiğinde bu ifade değerini günceller. Reaktif ifadeler tembeldir (lazy) ve önbelleğe alınır.
Bu egzersizde, tekrarlanan bir hesaplamayı bir reaktif ifade olarak kapsülleyeceksin.
Bu egzersiz
R ile Shiny Kullanarak Web Uygulamaları Geliştirme
kursunun bir parçasıdırEgzersiz talimatları
- BMI hesaplamak için
rval_bmiadlı bir reaktif ifade ekle. - Bu reaktif ifadeyi kullanarak her iki çıktıdaki BMI hesaplamalarını değiştir.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
ui <- fluidPage(
titlePanel('BMI Calculator'),
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) {
# CODE BELOW: Add a reactive expression rval_bmi to calculate BMI
output$bmi <- renderText({
# MODIFY CODE BELOW: Replace right-hand-side with reactive expression
bmi <- input$weight/(input$height^2)
paste("Your BMI is", round(bmi, 1))
})
output$bmi_range <- renderText({
# MODIFY CODE BELOW: Replace right-hand-side with reactive expression
bmi <- input$weight/(input$height^2)
bmi_status <- cut(bmi,
breaks = c(0, 18.5, 24.9, 29.9, 40),
labels = c('underweight', 'healthy', 'overweight', 'obese')
)
paste("You are", bmi_status)
})
}
shinyApp(ui = ui, server = server)