I am making a simple Shiny app that inputs a player's name and statistic and then returns the percentile that the player is in for that statistic. I am currently running into an issue where the 'statistic' widget is causing an error (see title).
Here are the packages I am using and a sample of the data:
library(shiny)
library(dplyr)
library(mosaic)
player <- c("John", "Mike", "Devon", "Greg", "Bruce", "Zachary", "Jack", "Graham", "Jordan", "Sandy")
team <- c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B")
wins <- c(1:10)
losses <- c(10:1)
sampledata <- data.frame(player, team, wins, losses)
On the app, there are three widgets: (1) input a player's name, (2) select a stat, and (3) execute the selections.
The output is a single line of text.
Here is the ui.r:
ui <- fluidPage(
titlePanel("Percentile Generator"),
sidebarLayout(
sidebarPanel(
textInput("playerfind",
"Player:",
value = "Devon"),
selectInput("stat1", "Select Statistic:",
choices = list("wins", "losses", "ties"),
selected = "wins"),
actionButton("action", label = "Generate Percentile!")
),
mainPanel(
textOutput("percentmachine")
))
)
The server is a little bit more complicated. Step 1 filters the sample data and produces a 1x3 dataframe based on the inputs. Step 2 pulls the necessary value out of the matric and stores it. Lastly, step 3 takes the input from step 2 and produces a percentile.
server <- function(input, output) {
step1 <- reactive({sampledata %>%
transmute(player, stat = zscore(input$stat1)) %>%
filter(player == input$playerfind)})
step2 <- reactive({step1()[1,2]})
step3 <- reactive({round(pnorm(step2())*100, digits = 1)})
output$percentmachine <- renderText ({
input$action
isolate(paste(input$playerfind,
"had more",
input$stat1,
"than",
step3(),
"percent of players."))})
}
I believe that the error comes from input$stat1 in step 1. If I replace this input with a specific stat like 'wins', then the shiny app runs fine although the statistic cannot be changed. I have been struggling with this for quite some time so I figured I would ask on here.
Thanks in advance! xD
charactervalue (input$stat1) intozscore, a function that requiresnumericinputs. If you tryzscore("A"), you'll get the same error. - r2evanszscoreis looking for a distribution of data in order to determine the z-scores for each of them, perhaps you should first calculate your z-scores with something likesampledata$z <- zscore(sampledata$wins). *shrug* - r2evanszscore(some_strings)ever working, looking at its source. Perhaps your R script is feeding a different variable? Are you intending to get theinput$stat1column from the data and put it in there? Perhaps you meanzscore(sampledata[,input$stat1])? (Except that the frame doesn't have a"ties"column ...) - r2evanssample datawas piped to transmute, although I wasn't! Thanks for the clarification and if you write that comment as an answer, I will mark it as solved :) - wickmanb22