我正在尝试修复以下错误:Warning: Error in : Please supply variables to complete
Stack trace (innermost first)
在构建ShinyApp以通过条形图绘制我的数据时,首先我想检查Shiny交互式内容是否适用于我的代码。但是,函数complete_()
似乎不适用于闪亮的交互式。我创建了以下ui和服务器来测试这个:
我的数据:
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)))
我的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")
)
)
)
我的服务器:
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))
})
}
我知道问题首先出现在complete_()
。有关导致此问题的任何见解?
谢谢!
答案 0 :(得分:1)
该函数适用于Shiny反应变量,但您的代码存在一些小问题。首先,当我们输入help(complete_)
时,我们会读到"the underscored versions are now superfluous"
,因此我们可以使用常规的complete
。此外,在group_by
和后续.choices=input$choiceDisplay1, .cat=c("category"), .variables=c("SES")
语句中,我们只需要引用列,而不是
.choices, .cat, .variables,
我们可以做到
complete
因为那些是我们想要完成的列。下面给出一个工作实例。请注意,我已将您的数据集限制为20条记录,因此我们实际上可以看到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)
添加的丢失记录。
希望这有帮助!
{{1}}