사용자가 선택했는지 검증하기
영상에서 보셨듯이 선택 입력에 기본값을 지정하는 것이 일반적으로는 좋은 방법이지만, 기본값을 두지 않아야 하는 경우에는 사용자에게 앱을 정상적으로 실행하려면 무엇을 해야 하는지 안내하는 맞춤형 오류 메시지를 띄울 수 있어요.
이전 연습 문제에서 pickerInput()에 기본값이 없으면 그래프가 그냥 비어 있다는 것을 확인했죠. 이 연습 문제에서는 빈 그래프 대신, 앱이 동작하도록 올바른 선택을 하라고 알려주는 맞춤형 오류 메시지를 사용자에게 보여 주세요.
이 연습은 강의의 일부입니다
R로 Shiny 웹 애플리케이션 만들기
연습 안내
- 기본적으로 표시될 맞춤형 오류 메시지를 추가해, 사용자가 정신 건강과 신체 건강 중 하나의 입력을 선택해야 한다는 것을 알려 주세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
ui <- fluidPage(
titlePanel("2014 Mental Health in Tech Survey"),
sidebarPanel(
sliderTextInput(
inputId = "work_interfere",
label = "If you have a mental health condition, do you feel that it interferes with your work?",
grid = TRUE,
force_edges = TRUE,
choices = c("Never", "Rarely", "Sometimes", "Often")
),
checkboxGroupInput(
inputId = "mental_health_consequence",
label = "Do you think that discussing a mental health issue with your employer would have negative consequences?",
choices = c("Maybe", "Yes", "No"),
selected = "Maybe"
),
pickerInput(
inputId = "mental_vs_physical",
label = "Do you feel that your employer takes mental health as seriously as physical health?",
choices = c("Don't Know", "No", "Yes"),
multiple = TRUE
)
),
mainPanel(
plotOutput("age")
)
)
server <- function(input, output, session) {
output$age <- renderPlot({
# MODIFY CODE BELOW: Add validation that user selected a 3rd input
mental_health_survey %>%
filter(
work_interfere == input$work_interfere,
mental_health_consequence %in% input$mental_health_consequence,
mental_vs_physical %in% input$mental_vs_physical
) %>%
ggplot(aes(Age)) +
geom_histogram()
})
}
shinyApp(ui, server)