ユーザーが選択したかを検証する
動画で見たとおり、セレクター入力にはデフォルト値を設定するのが一般的に良い実践ですが、あえて外す場合は、アプリを正しく動かすために必要な操作をユーザーに伝えるカスタムエラーメッセージを出すとよいです。
前の演習では、pickerInput() にデフォルト値がないとプロットが空になることを確認しました。空のプロットの代わりに、この演習ではアプリを動かすために必要な正しい選択を促すカスタムエラーメッセージを表示します。
この演習はコースの一部です
Rで作るShiny Webアプリケーション
演習の手順
- デフォルトで表示されるカスタムエラーメッセージを追加し、メンタルヘルスとフィジカルヘルスのどちらを入力として選択すべきかをユーザーに知らせてください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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)