Highcharter 和 Shiny,服务器功能中的反应性 dataset/mutated 数据集无法正常工作
Highcharter and Shiny with reactive dataset/mutated dataset within server function not working
当我尝试使用基于 selectInput 值分组和汇总的数据集在 Shiny 中生成一个 highcharter barplot 并且这些相同的值在 hcaes() 中被引用时,我得到错误“object 'input' not找到
我也试过 hcaes_string() 然后我得到“找不到对象 'My.Variable'” 但是当我输入 My.Variable 时,它会生成 hchart 所以它可以与在服务器函数中创建的数据集进行交互。显然,我想通过下拉菜单在 My.Variable 和 My.Variable2 之间切换。我已尝试将汇总数据集分配给反应对象,但随后出现错误“hchart 不支持 class/type reactiveExpr/reactive/function 的对象”。
我已经研究了好几个小时了,这是我在 Whosebug 上的第一个问题。我装配了一个示例数据集,以便代码可重现,并且我已将 R 和 RStudio 更新到最新版本。
library('highcharter')
library('plyr')
library('dplyr')
library('tidyr')
library('lubridate')
library('stringr')
library('tools')
library('shiny')
#demo <- read.csv("data/name-change-analysis.csv",stringsAsFactors = FALSE)
indiv <- rep(c('p1','p2','p3','p4','p5'),4)
Name.Change <- rep(c('yes','yes','no','yes','no'),4)
Overall.Category <- rep(c('against','support','support','neutral','against'),4)
Race <- rep(c('Black','White','White','Asian','White'),4)
Gender <- rep(c('Male','Male','Male','Female','Male'),4)
demo <- as.data.frame(cbind(indiv,Name.Change,Overall.Category,Race,Gender))
ui <-
navbarPage(
"Responses by demographics",
tabPanel(
"Manual labels",
fluidPage(
fluidRow(
column(
selectInput(
"category",
label = "Select a demographic category:",
choices = c("Race",
"Gender" = "gender")
),
width = 6
),
column(
selectInput(
"name_or_overall",
label = "Response Category",
choices = c(
"Name Change" = "Name.Change",
"Overall Category" = "Overall.Category"
),
width = "100%"
),
width = 6
)
),
highchartOutput("hcontainer")
)
),
collapsible = TRUE
)
server <- function(input, output, session) {
output$hcontainer <- renderHighchart({
demo %>%
group_by(input$category,input$name_or_overall) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = "count",
x = as.name(input$category),
group = as.name(input$name_or_overall))) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
})
}
shinyApp(ui,server)
试试这个
output$hcontainer <- renderHighchart({
df1 <- demo %>% mutate(var1=demo[[as.name(input$category)]], var2=demo[[as.name(input$name_or_overall)]])
df <- df1 %>% group_by(var1,var2) %>% summarise(count = n())
highchart() %>%
hc_add_series(df, type = "bar",
hcaes(y = "count",
x = "var1",
group = "var2")) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = FALSE)
})
您将得到以下输出:
当我尝试使用基于 selectInput 值分组和汇总的数据集在 Shiny 中生成一个 highcharter barplot 并且这些相同的值在 hcaes() 中被引用时,我得到错误“object 'input' not找到
我也试过 hcaes_string() 然后我得到“找不到对象 'My.Variable'” 但是当我输入 My.Variable 时,它会生成 hchart 所以它可以与在服务器函数中创建的数据集进行交互。显然,我想通过下拉菜单在 My.Variable 和 My.Variable2 之间切换。我已尝试将汇总数据集分配给反应对象,但随后出现错误“hchart 不支持 class/type reactiveExpr/reactive/function 的对象”。
我已经研究了好几个小时了,这是我在 Whosebug 上的第一个问题。我装配了一个示例数据集,以便代码可重现,并且我已将 R 和 RStudio 更新到最新版本。
library('highcharter')
library('plyr')
library('dplyr')
library('tidyr')
library('lubridate')
library('stringr')
library('tools')
library('shiny')
#demo <- read.csv("data/name-change-analysis.csv",stringsAsFactors = FALSE)
indiv <- rep(c('p1','p2','p3','p4','p5'),4)
Name.Change <- rep(c('yes','yes','no','yes','no'),4)
Overall.Category <- rep(c('against','support','support','neutral','against'),4)
Race <- rep(c('Black','White','White','Asian','White'),4)
Gender <- rep(c('Male','Male','Male','Female','Male'),4)
demo <- as.data.frame(cbind(indiv,Name.Change,Overall.Category,Race,Gender))
ui <-
navbarPage(
"Responses by demographics",
tabPanel(
"Manual labels",
fluidPage(
fluidRow(
column(
selectInput(
"category",
label = "Select a demographic category:",
choices = c("Race",
"Gender" = "gender")
),
width = 6
),
column(
selectInput(
"name_or_overall",
label = "Response Category",
choices = c(
"Name Change" = "Name.Change",
"Overall Category" = "Overall.Category"
),
width = "100%"
),
width = 6
)
),
highchartOutput("hcontainer")
)
),
collapsible = TRUE
)
server <- function(input, output, session) {
output$hcontainer <- renderHighchart({
demo %>%
group_by(input$category,input$name_or_overall) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = "count",
x = as.name(input$category),
group = as.name(input$name_or_overall))) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
})
}
shinyApp(ui,server)
试试这个
output$hcontainer <- renderHighchart({
df1 <- demo %>% mutate(var1=demo[[as.name(input$category)]], var2=demo[[as.name(input$name_or_overall)]])
df <- df1 %>% group_by(var1,var2) %>% summarise(count = n())
highchart() %>%
hc_add_series(df, type = "bar",
hcaes(y = "count",
x = "var1",
group = "var2")) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = FALSE)
})
您将得到以下输出: