rankPlayer | R Documentation |
Get current player's rank among group of past players
rankPlayer(
score,
playerId = NULL,
rankFile,
score_col = "score",
id_col = "pin",
rank_col = "rank"
)
score |
The player's current score. |
playerId |
The player's ID number. MUST be unique for each player! Use |
rankFile |
A file path to a new or existing .rds or .csv file of previous players' scores. Must contain 3 columns described by the remaining arguments. |
score_col |
The name of the column in |
id_col |
The name of the column in |
rank_col |
The name of the column in |
A list with 2 values: (1) rank = the current players rank (integer) and (2) out_of = the total number of players so far.
# Erase the out file, "sample_rank.rds" after you run this example,
# ...so you won't get the error of multiple "players" with the same ID
if (interactive()) {
library(shiny)
library(shinyalert)
library(shinyjs)
ui <- fluidPage(
useShinyalert(),
actionButton("ok", "Click to add to your score!"),
textOutput("score_txt"),
actionButton("rank", "Show your rank when you're ready.")
)
server <- function(input, output, session) {
rv <- reactiveValues(score = 0, pin = 1)
observeEvent(input$ok, {
rv$score <- rv$score + sample(c(1:9), 1)
output$score_txt <- renderText({
paste0("Your current score: ",rv$score)
})
enable("rank")
})
observeEvent(input$rank, {
disable("rank")
rv$pin <- rv$pin+1
rank <- rankPlayer(score = rv$score,
playerId = rv$pin,
rankFile = "sample_rank.rds")
text <- paste0("You rank #", rank$rank,
" out of ", rank$out_of, " player(s).")
shinyalert(title = "Congratulations!",
type = "success",
text = text)
})
}
shinyApp(ui = ui, server = server)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.