Reputation: 21212
My input radio buttons allow for several features that the user can filter on, including "Voice", "Format", "DeviceType".
Here is a data frame with hard coded values using the "Voice" option that allows my Shiny app to run when used:
combinations <- reactive({
expand.grid(unique(combined_data$date),
unique(combined_data$Voice)) %>%
rename_("date" = "Var1",
"Voice" = "Var2") %>%
mutate_("Voice" = as.character("Voice"))
})
I'm trying to transform combinations to use the input variable:
combinations <- reactive({
the_breakdown <- input$breakdown
expand.grid(unique(combined_data$date),
unique(combined_data[[input$breakdown]])) %>%
rename_("date" = "Var1",
the_breakdown = "Var2") %>%
mutate_(the_breakdown = as.character(the_breakdown))
})
This gives me an error later in my code, and right now I'm having a hard time debugging it. However, it's clear that my attempt to make reactive data frame combinations()
use the variable input$breakdown
, somehting is not working.
I tried to incrementally go through the initial df by switching hard coded example "Voice" to input$breakdown but failed to isolate the problem.
How should I make df combinations use the input variable input$breakdown
, given I know that the hard coded version with "Voice" does work?
Upvotes: 2
Views: 679
Reputation: 887118
We can use rename
and mutate
with sym
from rlang
combinations <- reactive({
the_breakdown <- input$breakdown
expand.grid(unique(combined_data$date),
unique(combined_data[[input$breakdown]])) %>%
rename(date = Var1,
!! the_breakdown := Var2) %>%
mutate(!! the_breakdown := as.character(!! rlang::sym(the_breakdown)))
})
Using a small reproducible example
the_breakdown <- 'digitnew'
df1 %>%
rename(!!the_breakdown := digit, s9 = s3) %>% #rename
mutate(!! the_breakdown := as.character(!! rlang::sym(the_breakdown))) %>% #transform
str # get the structure
# 'data.frame': 4 obs. of 8 variables:
# $ digitnew: chr "0" "1" "2" "3" #####
# $ s1 : int 1 0 1 1
# $ s2 : int 1 0 0 0
# $ s9 : int 1 1 1 1
# $ s4 : int 1 0 0 1
# $ s5 : int 1 0 1 0
# $ s6 : int 1 1 0 1
# $ s7 : int 0 0 0 1
df1 <- structure(list(digit = 0:3, s1 = c(1L, 0L, 1L, 1L), s2 = c(1L,
0L, 0L, 0L), s3 = c(1L, 1L, 1L, 1L), s4 = c(1L, 0L, 0L, 1L),
s5 = c(1L, 0L, 1L, 0L), s6 = c(1L, 1L, 0L, 1L), s7 = c(0L,
0L, 0L, 1L)), .Names = c("digit", "s1", "s2", "s3", "s4",
"s5", "s6", "s7"), row.names = c("1", "2", "3", "4"), class = "data.frame")
Upvotes: 2