I keep getting an 'argument is of length zero' error on startup and when selecting different positions (basically whenever selectInput choices need to update).
I've included only the relevant code here (playerdf and adf are pre-loaded data.frames). I'm not sure how else to check for the inputs being null other than
if(is.null(input$player1) | is.null(input$player2)){
return(invisible())
}
I still get the error though even when checking for the nulls. The error is occurring in output$text below:
ui.R:
htmlOutput("text")
server.R:
shinyServer(function(input, output){
input1Choices <- reactive({
if(!("All" %in% input$position)) {
a <- playerdf[playerdf$pos %in% input$position,]$name
return(a)
} else{
return(playerdf$name)
}
})
input2Choices <- reactive({
if(!("All" %in% input$position)) {
p <- playerdf[-grep(input$player1, playerdf$name),]
p <- p[p$pos %in% input$position,]
return(p$name)
} else {
return (playerdf[-grep(input$player1, playerdf$name),]$name)
}
})
output$player1 <- renderUI({
selectInput("player1", "", choices=input1Choices())
})
output$player2 <- renderUI({
if(is.null(input$player1))
return()
selectInput("player2", "OR", choices=input2Choices())
})
output$text <- renderPrint({
if(is.null(input$player1) | is.null(input$player2)){
return(invisible())
}
x1 <- adf[which(adf$pick1 == input$player1 & adf$pick2 == input$player2),]$betterpick
x2 <- adf[which(adf$pick1 == input$player2 & adf$pick2 == input$player1),]$betterpick
tp1 <- playerdf[which(playerdf$name == input$player1),]$timesPicked
tp2 <- playerdf[which(playerdf$name == input$player2),]$timesPicked
if(tp1 >= tp2){
tpmin <- tp2
tpmax <- tp1
} else if (tp2 >= tp1){
tpmin <- tp1
tpmax <- tp2
}
if(x1 >= x2){
y <- paste(input$player1, " was drafted <br>", signif((x1 / tpmin) * 100, 3), "% of the time before ", input$player2, sep="")
o <- HTML(paste("<p><strong>", y, "</p>", sep=""))
cat(as.character(o))
} else {
y <- paste(input$player1, " was drafted <br>", signif((x1 / tpmax) * 100, 3), "% of the time before ", input$player2, sep="")
o <- HTML(paste("<p><strong>", y, "</p>"))
cat(as.character(o))
}
})
})