library(nessy)
library(shinyjs)
<- "shinyjs.closeWindow = function() { window.close(); }"
jscode <- cartridge(
ui title = "{Memorize the Names!}",
subtitle = "Do you have some names to memorize in few minutes? Try this game!",
container_with_title(
title = "Names you want to memorize (i.e. Jonathan, Lesa)"
),container_with_title(
title = "Add a Name",
text_input(id = "name1", label = "Name", placeholder = "Jonathan Templin"),
text_input(id = "key", label = "Keys", placeholder = "Iowa/DCM"),
htmlOutput("namelist"),
button_primary(id = "add", "Add")
),button_success(id = "play", "Play the Game"),
useShinyjs(),
extendShinyjs(text = jscode, functions = c("closeWindow")),
button_error(id = "close", "Close Window"),
# Game pages
uiOutput("gamepage")
)
<- function(input, output, session) {
server <- reactiveValues(
names oldnames = "",
allnames = NULL,
allkeys = NULL
)
observeEvent(input$add, {
$oldnames = paste(names$oldnames, "<br>", input$name1, " <==> ", input$key)
names$allnames = c(names$allnames, input$name1)
names$allkeys = c(names$allkeys, input$key)
names$namelist <- renderText(names$oldnames)
output
})
observeEvent(input$play, {
<- sample(names$allkeys, 1)
selectedkey <- names$allnames[names$allkeys == selectedkey]
selectedname $gamepage <- renderUI({
outputcontainer_with_title(
paste("Key:", selectedkey),
text_input(id = "guessname", label = "Guess a Name", placeholder = "Jonathan")
)
})
})
observeEvent(input$close, {
$closeWindow()
jsstopApp()
})
}
::shinyApp(ui, server) shiny
Recently I found a interesting R package call nessy which allows you to create a simple game driven by shiny. Thus. I tried a little bit about this package. Making a interactive app in R is promising in the files like teaching, presentation and visualization.
Finally, I created the following shiny app:
The game is like this:
Back to top