So I have a Shiny app that has a selectInput with the options of "RB", "WR", and "TE". As part of the server, I then use the fct_reorder() function from the forcats package. I want what's in the fct_reorder() to change based on what's selected in the selectInput so I used a case_when() function to help with that, but it doesn't seem to be working. Below are each aspects of the code I'm dealing with.
UI:
selectInput(inputId = "position_sort",
label = "Sort by position",
choices = c("RB", "WR", "TE"), selected = "RB")
Server:
team_targets <- reactive({
pos_sort <- input$position_sort
fp_team_targets(season = 2019, start_week = 1, end_week = 17) %>%
select(
team,
ends_with("percent")
) %>%
mutate(
team = case_when(pos_sort == "RB" ~ fct_reorder(team, rb_percent),
pos_sort == "WR" ~ fct_reorder(team, wr_percent),
pos_sort == "TE" ~ fct_reorder(team, te_percent))
) %>%
gather("position", "percent", -team) %>%
mutate(
pos = factor(
position,
levels = c("te_percent", "wr_percent", "rb_percent"),
labels = c("TE", "WR", "RB")
)
)})
For further clarification, if "RB" is selected, then I want the portion inside the fct_reorder to read (team, rb_percent). Any thoughts on why this isn't working and what an alternative might be?