I want to make a R shiny application that prints a riddle (raadsel) and where somebody has to enter an answer (antwoord). If someone puts in the right answer, a pop up appears that says "Goed geraden...) otherwise the popup says "try again". I wrote the code underneath, but unfortunately when I put in an answer, a popup does not appear. Also, it does not print the riddle. Where does it go wrong and does somebody have a solution?
library(shiny)
# Definieer raadsels en antwoorden
raadsels <- c("Ik ben groot als ik jong ben, maar klein als ik oud ben. Wat ben ik?",
"Ik ben altijd om je heen, maar nooit gezien. Wat ben ik?")
antwoorden <- c("een boom", "lucht")
# UI voor de spiegel
ui <- fluidPage(
titlePanel("Interactieve Spiegel"),
sidebarLayout(
sidebarPanel(
textInput("antwoord", "Wat is je antwoord?"),
actionButton("controleer", "Controleer Antwoord")
),
mainPanel(
verbatimTextOutput("raadsel_output"),
verbatimTextOutput("feedback")
)
)
)
# Server logica
server <- function(input, output, session) {
output$raadsel_output <- renderText({
raadsels[1] # Weergeef het eerste raadsel
})
observeEvent(input$controleer, {
antwoord <- tolower(input$antwoord) # Antwoorden zijn niet hoofdlettergevoelig
if (antwoord %in% tolower(antwoorden)) {
showModal(
modalDialog(
title = "Goed Geraden!",
"Jullie mogen de envelop openen."
)
)
} else {
showModal(
modalDialog(
title = "Fout Antwoord",
"Het antwoord is niet correct. Probeer het opnieuw."
)
)
}
})
}
# Run de app
shinyApp(ui = ui, server = server)