Reputation: 103
I am trying to fix the following error: Warning: Error in : Please supply variables to complete
Stack trace (innermost first)
While building a ShinyApp to graph my data through bar charts, first I wanted to check that the Shiny interactives would work in my code. However, the function complete_()
does not seem to work with shiny interactives. I created the following ui and server to test this:
My data:
gender <- c("Male", "Female")
residency <-c("InsideUS", "OutsideUS")
category <- c("A", "B")
SES <- c("Lower", "Middle", "Upper")
choices <- c("gender", "residency", "SES")
variables <- c("SES")
cat<- c("category")
df <- data.frame( gender=as.factor(sample(gender, size=100, replace=TRUE)),
residency=as.factor(sample(residency, size=100, replace=TRUE)),
category=as.factor(sample(category, size=100, replace=TRUE)),
SES=as.factor(sample(SES, size=100, replace=TRUE)))
My ui:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "choiceDisplay1",
label= "Please select an option",
choices= c("Gender"="gender",
"Residency"="residency",
"Social Economic Status"="SES"),
selected="gender")
),
mainPanel(
verbatimTextOutput("summary")
)
)
)
My server:
server <- function(input, output) {
#Target Market
output$summary <- renderPrint({
df %>% group_by_(.choices=input$choiceDisplay1, .cat=c("category"),
.variables=c("SES")) %>%
summarize(n=n()) %>%
ungroup() %>%
complete_(.choices=input$choiceDisplay1, .cat=c("category"),
.variables=c("SES"),
fill=list(n=0)) %>%
group_by_(.choices=input$choiceDisplay1, .cat=c("category")) %>%
mutate(perc= n/sum(n))
})
}
I know the problem first arises with complete_()
. Any insights on to what is causing this?
Thank you!
Upvotes: 3
Views: 1160
Reputation: 25385
The function works fine with Shiny reactive variables, but there are some small issues with your code. First, when we type help(complete_)
, we read that "the underscored versions are now superfluous"
, so we can use the regular ones. Furthermore, in the complete
and the subsequent group_by
statements, we only have to refer to the columns, So instead of
.choices=input$choiceDisplay1, .cat=c("category"), .variables=c("SES")
we can just do
.choices, .cat, .variables,
since those are the columns we want to complete. A working example is given below. Note that I have limited your dataset to 20 records so we can actually see the missing records being added by complete
.
Hope this helps!
gender <- c("Male", "Female")
residency <-c("InsideUS", "OutsideUS")
category <- c("A", "B")
SES <- c("Lower", "Middle", "Upper")
choices <- c("gender", "residency", "SES")
variables <- c("SES")
cat<- c("category")
set.seed(1)
df <- data.frame( gender=as.factor(sample(gender, size=20, replace=TRUE)),
residency=as.factor(sample(residency, size=20, replace=TRUE)),
category=as.factor(sample(category, size=20, replace=TRUE)),
SES=as.factor(sample(SES, size=20, replace=TRUE)))
library(dplyr)
library(tidyr)
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "choiceDisplay1",
label= "Please select an option",
choices= c("Gender"="gender",
"Residency"="residency",
"Social Economic Status"="SES"),
selected="gender")
),
mainPanel(
verbatimTextOutput("summary")
)
)
)
server <- function(input, output) {
#Target Market
output$summary <- renderPrint({
df %>% group_by_(.choices=input$choiceDisplay1, .cat=c("category"),
.variables=c("SES")) %>%
summarize(n=n()) %>%
ungroup() %>% complete(.choices, .cat, .variables,
fill=list(n=0)) %>%
group_by(.choices, .cat) %>%
mutate(perc= n/sum(n))
})
}
shinyApp(ui,server)
Upvotes: 1