Trying to pass R Shiny input as variable name

Below is an example of two Shiny input variables that I have:

fluidRow(column(6, selectInput(inputId = "prop_change",
         label = "Adjust prop type",
         choices = c("Prop to change" = "", "Points" = "points",
                        "Rebounds" = "rebounds", "Assists" = "assists", "PRA" = "pra"))),
         column(6, selectInput(inputId = "over_under",
                        label = "Adjust over or under",
                        choices = c("Side to change" = "", "Over" = "over", "Under" = "under"))))

I then have a dataframe with which I want to use the inputs as variable names to mutate data for. Below is the current code I have:

value <- reactive({
        
        prop_type <- as.name(input$prop_change)
        ou_type <- as.name(input$over_under)
        
        total_type <- as.name(paste0(prop_type,"_total"))
        total_odds_type <- as.name(paste0(prop_type, "_",ou_type,"_odds"))
        
        projections_final %>% 
            left_join(prop_bets2(),
                      by = c("Player" = "participant")) %>% 
            left_join(prop_bets3(),
                      by = c("Player" = "participant")) %>%
            mutate(total_type = if_else(Player == "Luke Mueller",
                                        6.5,
                                        total_type),
                   total_odds_type = if_else(Player == "Luke Mueller",
                                             -100,
                                             total_odds_type))
    })

So, as you can see, I'm trying to use the prop_change and over_under inputs as variable names that'll be mutated. I seem to be getting an error that says "Attempt to use zero-length variable name" - any suggestions on how to fix this? I thought the as.name would work but maybe not?

Here's an attempt at my minimal reprex of the full shiny app:

library(shiny)
library(utils)
library(httr)
library(stringi)
library(jsonlite)
library(tidyverse)
library(dplyr)
library(DT)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            fileInput(inputId = "projection_file",
                      label = "Upload Projections",
                      multiple = FALSE,
                      accept = ".csv"),
            tags$div(p(strong("Adjust individual prop lines"))),
            textInput(inputId = "player_change",
                        label = "Adjust player"),
            fluidRow(column(6, selectInput(inputId = "prop_change",
                        label = "Adjust prop type",
                        choices = c("Prop to change" = "", "Points" = "points",
                                    "Rebounds" = "rebounds", "Assists" = "assists", "PRA" = "pra"))),
            column(6, selectInput(inputId = "over_under",
                        label = "Adjust over or under",
                        choices = c("Side to change" = "", "Over" = "over", "Under" = "under")))),
            fluidRow(column(6, numericInput(inputId = "total_change",
                         label = "Adjust total",
                         value = 5,
                         step = 0.5)),
            column(6, numericInput(inputId = "odds_change",
                         label = "Adjust odds",
                         value = 100,
                         step = 5)))),
        
        mainPanel(
            tabsetPanel(type = "tabs",
                        tabPanel("Points Value",
                                 DT::dataTableOutput("points_value")))
            
        )
    )
)

server <- function(input, output) {
    #browser()
    output$download_template <- downloadHandler(
        filename = paste0("NBA_player_props_template.csv"),
        content = function(file){
            write.csv(props_template, file, row.names = FALSE)
        }
    )
    
    projections <- reactive({
        
        in_file <- input$projection_file
        #validate(need(in_file != "", "Please upload projections to find value!"))
        
        if(is.null(in_file))
            return(NULL)
        
        read_csv(in_file$datapath)
    })
    
    sb_data <- reactive({
        prop_bets <- read_csv(url("https://raw.githubusercontent.com/samhoppen/Fantasy-Evaluator/main/prop_bet_test.csv"))
        prop_bets$line <- sapply(strsplit(prop_bets$side, "\\s+"), `[`, 2)
        prop_bets$side <- sapply(strsplit(prop_bets$side, "\\s+"), `[`, 1)
        
        prop_bets %>% 
            mutate(type = case_when(type == "Points scored by the player" ~ "points_total",
                                    type == "Rebounds by the player" ~ "rebounds_total",
                                    type == "Assists by the player" ~ "assists_total",
                                    type == "Points, rebounds & assists by the player" ~ "pra_total",
                                    type == "3-point field goals made by the player" ~ "fg3m_total",
                                    TRUE ~ "NA"),
                   side = case_when(side == "Over" & type == "points_total" ~ "points_over_odds",
                                    side == "Under" & type == "points_total" ~ "points_under_odds",
                                    side == "Over" & type == "rebounds_total" ~ "rebounds_over_odds",
                                    side == "Under" & type == "rebounds_total" ~ "rebounds_under_odds",
                                    side == "Over" & type == "assists_total" ~ "assists_over_odds",
                                    side == "Under" & type == "assists_total" ~ "assists_under_odds",
                                    side == "Over" & type == "pra_total" ~ "pra_over_odds",
                                    side == "Under" & type == "pra_total" ~ "pra_under_odds",
                                    side == "Over" & type == "fg3m_total" ~ "fg3m_over_odds",
                                    side == "Under" & type == "fg3m_total" ~ "fg3m_under_odds",
                                    TRUE ~ "NA"),
                   line = as.numeric(line))
        
        
       })
    
    prop_bets2 <- reactive({
        sb_data() %>% 
            select(participant, side, odds) %>% 
            distinct() %>% 
            pivot_wider(id_cols = participant, names_from = side, values_from = odds)
    })
    
    prop_bets3 <- reactive({
        sb_data() %>% 
            select(participant, type, line) %>% 
            distinct() %>% 
            pivot_wider(id_cols = participant, names_from = type, values_from = line)
    })
    
    value <- reactive({
        req(input$player_change, input$prop_change, input$over_under,
            input$total_change, input$odds_change)
        
        player_name <- input$player_change
        prop_type <- as.name(input$prop_change)
        ou_type <- as.name(input$over_under)
        new_total <- input$total_change
        new_odds <- input$odds_change
        
        total_type <- as.name(paste0(prop_type,"_total"))
        total_odds_type <- as.name(paste0(prop_type, "_",ou_type,"_odds"))
        
        projections_final <- projections() %>% 
            mutate(PRA = Points + Rebounds + Assists)
        
        projections_final %>% 
            left_join(prop_bets2(),
                      by = c("Player" = "participant")) %>% 
            left_join(prop_bets3(),
                      by = c("Player" = "participant")) %>%
            mutate(total_type = if_else(Player == player_name,
                                        new_total,
                                        total_type),
                   total_odds_type = if_else(Player == player_name,
                                               new_odds,
                                             total_odds_type)) %>% 
            dplyr::mutate(points_diff = Points - points_total,
                   rebounds_diff = Rebounds - rebounds_total,
                   assists_diff = Assists - assists_total,
                   pra_diff = PRA - pra_total,
                   #fg3m_diff = `Three Pointers` - fg3m_total,
                   points_imp_over = if_else(points_over_odds <= 0,
                                             (abs(points_over_odds)/(abs(points_over_odds)+100)*100),
                                             (100/(points_over_odds + 100))*100),
                   points_imp_under = if_else(points_under_odds <= 0,
                                              (abs(points_under_odds)/(abs(points_under_odds)+100)*100),
                                              (100/(points_under_odds + 100))*100),
                   rebounds_imp_over = if_else(rebounds_over_odds <= 0,
                                               (abs(rebounds_over_odds)/(abs(rebounds_over_odds)+100)*100),
                                               (100/(rebounds_over_odds + 100))*100),
                   rebounds_imp_under = if_else(rebounds_under_odds <= 0,
                                                (abs(rebounds_under_odds)/(abs(rebounds_under_odds)+100)*100),
                                                (100/(rebounds_under_odds + 100))*100),
                   assists_imp_over = if_else(assists_over_odds <= 0,
                                              (abs(assists_over_odds)/(abs(assists_over_odds)+100)*100),
                                              (100/(assists_over_odds + 100))*100),
                   assists_imp_under = if_else(assists_under_odds <= 0,
                                               (abs(assists_under_odds)/(abs(assists_under_odds)+100)*100),
                                               (100/(assists_under_odds + 100))*100),
                   pra_imp_over = if_else(pra_over_odds <= 0,
                                          (abs(pra_over_odds)/(abs(pra_over_odds)+100)*100),
                                          (100/(pra_over_odds + 100))*100),
                   pra_imp_under = if_else(pra_under_odds <= 0,
                                           (abs(pra_under_odds)/(abs(pra_under_odds)+100)*100),
                                           (100/(pra_under_odds + 100))*100),
                   points_over_prob = (((points_diff)/points_total)+0.5)*100,
                   points_under_prob = (0.5-((points_diff)/points_total))*100,
                   points_over_value = points_over_prob - points_imp_over,
                   points_under_value = points_under_prob - points_imp_under,
                   rebounds_over_prob = (((rebounds_diff)/rebounds_total)+0.5)*100,
                   rebounds_under_prob = (0.5-((rebounds_diff)/rebounds_total))*100,
                   rebounds_over_value = rebounds_over_prob - rebounds_imp_over,
                   rebounds_under_value = rebounds_under_prob - rebounds_imp_under,
                   assists_over_prob = (((assists_diff)/assists_total)+0.5)*100,
                   assists_under_prob = (0.5-((assists_diff)/assists_total))*100,
                   assists_over_value = assists_over_prob - assists_imp_over,
                   assists_under_value = assists_under_prob - assists_imp_under,
                   pra_over_prob = (((pra_diff)/pra_total)+0.5)*100,
                   pra_under_prob = (0.5-((pra_diff)/pra_total))*100,
                   pra_over_value = pra_over_prob - pra_imp_over,
                   pra_under_value = pra_under_prob- pra_imp_under)
    })
    
    points_value <- reactive({
        
        value() %>%
            dplyr::filter(!is.na(points_over_odds)) %>% 
            dplyr::select(Player, Team, Opp, Points, points_total, points_diff, points_over_odds, points_over_value,
                   points_under_odds, points_under_value) %>% 
            dplyr::mutate(points_over_value = points_over_value/100,
                   points_under_value = points_under_value/100,
                   points_diff = round(points_diff, digits = 1)) %>% 
            dplyr::rename(Projection = Points,
                   Line = points_total,
                   Difference = points_diff,
                   `Over Odds` = points_over_odds,
                   `Over Value` = points_over_value,
                   `Under Odds` = points_under_odds,
                   `Under Value` = points_under_value) %>% 
            dplyr::arrange(desc(`Over Value`))
    })

    output$points_value <- DT::renderDataTable({
        datatable(points_value(),
                  options = list(lengthMenu = c(25, 50, 100), pageLength = 25)) %>% 
            formatPercentage("Over Value", 1) %>% 
            formatPercentage("Under Value", 1) 
        
    })
    
}

shinyApp(ui = ui, server = server)

Could you include your chunks in a simple shiny app and post the full code?

putting aside shiny for a moment. Look at

 as.name("something")

in your console, and then

 as.name("")

shiny provides req() function, to skip out from running code if requirements arent met, like having an empty input.

Here's as good of a minimal reprex as I could create (along with the "prop_bet_test" file and an example of a file to upload for projections attached):

library(shiny)
library(utils)
library(httr)
library(stringi)
library(jsonlite)
library(tidyverse)
library(dplyr)
library(DT)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            fileInput(inputId = "projection_file",
                      label = "Upload Projections",
                      multiple = FALSE,
                      accept = ".csv"),
            tags$div(p(strong("Adjust individual prop lines"))),
            textInput(inputId = "player_change",
                        label = "Adjust player"),
            fluidRow(column(6, selectInput(inputId = "prop_change",
                        label = "Adjust prop type",
                        choices = c("Prop to change" = "", "Points" = "points",
                                    "Rebounds" = "rebounds", "Assists" = "assists", "PRA" = "pra"))),
            column(6, selectInput(inputId = "over_under",
                        label = "Adjust over or under",
                        choices = c("Side to change" = "", "Over" = "over", "Under" = "under")))),
            fluidRow(column(6, numericInput(inputId = "total_change",
                         label = "Adjust total",
                         value = 5,
                         step = 0.5)),
            column(6, numericInput(inputId = "odds_change",
                         label = "Adjust odds",
                         value = 100,
                         step = 5)))),
        
        mainPanel(
            tabsetPanel(type = "tabs",
                        tabPanel("Points Value",
                                 DT::dataTableOutput("points_value")))
            
        )
    )
)

server <- function(input, output) {
    #browser()
    output$download_template <- downloadHandler(
        filename = paste0("NBA_player_props_template.csv"),
        content = function(file){
            write.csv(props_template, file, row.names = FALSE)
        }
    )
    
    projections <- reactive({
        
        in_file <- input$projection_file
        #validate(need(in_file != "", "Please upload projections to find value!"))
        
        if(is.null(in_file))
            return(NULL)
        
        read_csv(in_file$datapath)
    })
    
    sb_data <- reactive({
        prop_bets <- read_csv(url("https://raw.githubusercontent.com/samhoppen/Fantasy-Evaluator/main/prop_bet_test.csv"))
        prop_bets$line <- sapply(strsplit(prop_bets$side, "\\s+"), `[`, 2)
        prop_bets$side <- sapply(strsplit(prop_bets$side, "\\s+"), `[`, 1)
        
        prop_bets %>% 
            mutate(type = case_when(type == "Points scored by the player" ~ "points_total",
                                    type == "Rebounds by the player" ~ "rebounds_total",
                                    type == "Assists by the player" ~ "assists_total",
                                    type == "Points, rebounds & assists by the player" ~ "pra_total",
                                    type == "3-point field goals made by the player" ~ "fg3m_total",
                                    TRUE ~ "NA"),
                   side = case_when(side == "Over" & type == "points_total" ~ "points_over_odds",
                                    side == "Under" & type == "points_total" ~ "points_under_odds",
                                    side == "Over" & type == "rebounds_total" ~ "rebounds_over_odds",
                                    side == "Under" & type == "rebounds_total" ~ "rebounds_under_odds",
                                    side == "Over" & type == "assists_total" ~ "assists_over_odds",
                                    side == "Under" & type == "assists_total" ~ "assists_under_odds",
                                    side == "Over" & type == "pra_total" ~ "pra_over_odds",
                                    side == "Under" & type == "pra_total" ~ "pra_under_odds",
                                    side == "Over" & type == "fg3m_total" ~ "fg3m_over_odds",
                                    side == "Under" & type == "fg3m_total" ~ "fg3m_under_odds",
                                    TRUE ~ "NA"),
                   line = as.numeric(line))
        
        
       })
    
    prop_bets2 <- reactive({
        sb_data() %>% 
            select(participant, side, odds) %>% 
            distinct() %>% 
            pivot_wider(id_cols = participant, names_from = side, values_from = odds)
    })
    
    prop_bets3 <- reactive({
        sb_data() %>% 
            select(participant, type, line) %>% 
            distinct() %>% 
            pivot_wider(id_cols = participant, names_from = type, values_from = line)
    })
    
    value <- reactive({
        req(input$player_change, input$prop_change, input$over_under,
            input$total_change, input$odds_change)
        
        player_name <- input$player_change
        prop_type <- as.name(input$prop_change)
        ou_type <- as.name(input$over_under)
        new_total <- input$total_change
        new_odds <- input$odds_change
        
        total_type <- as.name(paste0(prop_type,"_total"))
        total_odds_type <- as.name(paste0(prop_type, "_",ou_type,"_odds"))
        
        projections_final <- projections() %>% 
            mutate(PRA = Points + Rebounds + Assists)
        
        projections_final %>% 
            left_join(prop_bets2(),
                      by = c("Player" = "participant")) %>% 
            left_join(prop_bets3(),
                      by = c("Player" = "participant")) %>%
            mutate(total_type = if_else(Player == player_name,
                                        new_total,
                                        total_type),
                   total_odds_type = if_else(Player == player_name,
                                               new_odds,
                                             total_odds_type)) %>% 
            dplyr::mutate(points_diff = Points - points_total,
                   rebounds_diff = Rebounds - rebounds_total,
                   assists_diff = Assists - assists_total,
                   pra_diff = PRA - pra_total,
                   #fg3m_diff = `Three Pointers` - fg3m_total,
                   points_imp_over = if_else(points_over_odds <= 0,
                                             (abs(points_over_odds)/(abs(points_over_odds)+100)*100),
                                             (100/(points_over_odds + 100))*100),
                   points_imp_under = if_else(points_under_odds <= 0,
                                              (abs(points_under_odds)/(abs(points_under_odds)+100)*100),
                                              (100/(points_under_odds + 100))*100),
                   rebounds_imp_over = if_else(rebounds_over_odds <= 0,
                                               (abs(rebounds_over_odds)/(abs(rebounds_over_odds)+100)*100),
                                               (100/(rebounds_over_odds + 100))*100),
                   rebounds_imp_under = if_else(rebounds_under_odds <= 0,
                                                (abs(rebounds_under_odds)/(abs(rebounds_under_odds)+100)*100),
                                                (100/(rebounds_under_odds + 100))*100),
                   assists_imp_over = if_else(assists_over_odds <= 0,
                                              (abs(assists_over_odds)/(abs(assists_over_odds)+100)*100),
                                              (100/(assists_over_odds + 100))*100),
                   assists_imp_under = if_else(assists_under_odds <= 0,
                                               (abs(assists_under_odds)/(abs(assists_under_odds)+100)*100),
                                               (100/(assists_under_odds + 100))*100),
                   pra_imp_over = if_else(pra_over_odds <= 0,
                                          (abs(pra_over_odds)/(abs(pra_over_odds)+100)*100),
                                          (100/(pra_over_odds + 100))*100),
                   pra_imp_under = if_else(pra_under_odds <= 0,
                                           (abs(pra_under_odds)/(abs(pra_under_odds)+100)*100),
                                           (100/(pra_under_odds + 100))*100),
                   points_over_prob = (((points_diff)/points_total)+0.5)*100,
                   points_under_prob = (0.5-((points_diff)/points_total))*100,
                   points_over_value = points_over_prob - points_imp_over,
                   points_under_value = points_under_prob - points_imp_under,
                   rebounds_over_prob = (((rebounds_diff)/rebounds_total)+0.5)*100,
                   rebounds_under_prob = (0.5-((rebounds_diff)/rebounds_total))*100,
                   rebounds_over_value = rebounds_over_prob - rebounds_imp_over,
                   rebounds_under_value = rebounds_under_prob - rebounds_imp_under,
                   assists_over_prob = (((assists_diff)/assists_total)+0.5)*100,
                   assists_under_prob = (0.5-((assists_diff)/assists_total))*100,
                   assists_over_value = assists_over_prob - assists_imp_over,
                   assists_under_value = assists_under_prob - assists_imp_under,
                   pra_over_prob = (((pra_diff)/pra_total)+0.5)*100,
                   pra_under_prob = (0.5-((pra_diff)/pra_total))*100,
                   pra_over_value = pra_over_prob - pra_imp_over,
                   pra_under_value = pra_under_prob- pra_imp_under)
    })
    
    points_value <- reactive({
        
        value() %>%
            dplyr::filter(!is.na(points_over_odds)) %>% 
            dplyr::select(Player, Team, Opp, Points, points_total, points_diff, points_over_odds, points_over_value,
                   points_under_odds, points_under_value) %>% 
            dplyr::mutate(points_over_value = points_over_value/100,
                   points_under_value = points_under_value/100,
                   points_diff = round(points_diff, digits = 1)) %>% 
            dplyr::rename(Projection = Points,
                   Line = points_total,
                   Difference = points_diff,
                   `Over Odds` = points_over_odds,
                   `Over Value` = points_over_value,
                   `Under Odds` = points_under_odds,
                   `Under Value` = points_under_value) %>% 
            dplyr::arrange(desc(`Over Value`))
    })

    output$points_value <- DT::renderDataTable({
        datatable(points_value(),
                  options = list(lengthMenu = c(25, 50, 100), pageLength = 25)) %>% 
            formatPercentage("Over Value", 1) %>% 
            formatPercentage("Under Value", 1) 
        
    })
    
}

shinyApp(ui = ui, server = server)

Hope this helps!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.