我正在构建一个闪亮的应用程序,我想要完成的一件事就是创建一个下拉菜单。我想将劳动变量绘制为不同级别的年变量的函数。请参阅下面的示例数据框:
year level_2 level_3 level_4 labour
1 2013 10 101 1011 1
2 2014 10 101 1011 5
3 2015 10 101 1011 10
4 2016 10 101 1011 20
5 2017 10 101 1011 25
6 2013 11 111 1111 5
7 2014 11 111 1111 10
8 2015 11 111 1111 20
9 2016 11 111 1111 25
10 2017 11 111 1111 30
11 2013 10 102 1021 2
12 2014 10 102 1021 6
13 2015 10 102 1021 11
14 2016 10 102 1021 21
15 2017 10 102 1021 26
16 2013 11 112 1122 6
17 2014 11 112 1122 11
18 2015 11 112 1122 21
19 2016 11 112 1122 26
20 2017 11 112 1122 31
我已经知道如何获得第一个下拉(图表2中的level_3是通过点击图表1 level_2中的特定ime系列生成的)。但是,我想通过点击第二个高级图中的特定时间系列来执行类似的操作(将输入第三个图表/ level_4)。
但是,我有点卡住了。我正在构建一个webapp通过单击第二个图表的时间序列,应该会出现第三个图表。我设法做到了这一点,但第二个图表在第三个图表生成时消失了。我认为它与clickevent函数覆盖第一个input$canvasClicked[[1]]
的事实有关,但我没有设法得到正确的结果a)用input$canvasClicked[[2]]
索引或b)创建第二个JS("function(event) { ... }")
。
任何帮助,将不胜感激!简化应用程序的示例代码可以在下面找到。
library(shiny)
library(dplyr)
library(highcharter)
ui <- shinyUI(
fluidPage(
column(width = 4, highchartOutput("hcontainer", height = "500px")),
column(width = 4, highchartOutput("hcontainer2", height = "500px")),
column(width = 4, highchartOutput("hcontainer3", height = "500px")) #added add highcharter output
)
)
server <- function(input, output, session) {
df <- data.frame(year = c(rep(c(2013, 2014, 2015, 2016, 2017), 4)),
level_2 = c(rep(c(10, 10, 10, 10, 10, 11, 11, 11, 11, 11),2)),
level_3 = c(101, 101, 101, 101, 101, 111, 111, 111, 111, 111,
102, 102, 102, 102, 102, 112, 112, 112, 112, 112),
level_4 = c(c(1011, 1011, 1011, 1011, 1011, 1111, 1111, 1111, 1111, 1111,
1021, 1021, 1021, 1021, 1021, 1122, 1122, 1122, 1122, 1122)), # additional level added
labour = c(1, 5, 10, 20, 25, 5, 10, 20, 25, 30,
2, 6, 11, 21, 26, 6, 11, 21, 26, 31))
output$hcontainer <- renderHighchart({
temp <- df %>%
group_by(year, level_2) %>%
summarize(Sum = sum(labour)) %>%
arrange(level_2)
hchart(temp, "line", hcaes(x = year, y = Sum, group = level_2)) %>%
hc_plotOptions(series = list(events = list(click = canvasClickFunction)))
})
canvasClickFunction <- JS("function(event) {Shiny.onInputChange('canvasClicked', [this.name, event.point.category]);}")
#second highcharter which should appear when user clicked on the serie named 10
output$hcontainer2 <- renderHighchart({
req(input$canvasClicked[[1]])
temp2 <- df %>%
filter(level_2 == input$canvasClicked[[1]]) %>% # filter selected by click
group_by(year, level_3) %>%
summarize(Sum = sum(labour)) %>%
arrange(level_3)
hchart(temp2, "line", hcaes(x = year, y = Sum, group = level_3)) %>%
hc_title(text = paste0("I clicked ",input$canvasClicked[[1]])) %>%
hc_plotOptions(series = list(events = list(click = canvasClickFunction2)))
})
canvasClickFunction2 <- JS("function(event) {Shiny.onInputChange('canvasClicked2', [this.name, event.point.category]);}")
output$hcontainer3 <- renderHighchart({
req(input$canvasClicked2[[1]])
temp3 <- df %>%
filter(level_3 == input$canvasClicked2[[1]]) %>% # filter selected by click
group_by(year, level_4) %>%
summarize(Sum = sum(labour)) %>%
arrange(level_4)
hchart(temp3, "line", hcaes(x = year, y = Sum, group = level_4)) %>%
hc_title(text = paste0("I clicked ",input$canvasClicked2[[1]])) #%>%
})
}
shinyApp(ui = ui, server = server)
感谢Pork Chop,该应用程序正在运行。我需要更改新的canvasClicked
中的JS("function(event) { ... }")
部分以使应用程序正常工作。示例代码现在看起来像:
library(shiny)
library(dplyr)
library(highcharter)
ui <- shinyUI(
fluidPage(
column(width = 4, highchartOutput("hcontainer", height = "500px")),
column(width = 4, highchartOutput("hcontainer2", height = "500px")),
column(width = 4, highchartOutput("hcontainer3", height = "500px")) #added add highcharter output
)
)
server <- function(input, output, session) {
df <- data.frame(year = c(rep(c(2013, 2014, 2015, 2016, 2017), 4)),
level_2 = c(rep(c(10, 10, 10, 10, 10, 11, 11, 11, 11, 11),2)),
level_3 = c(101, 101, 101, 101, 101, 111, 111, 111, 111, 111,
102, 102, 102, 102, 102, 112, 112, 112, 112, 112),
level_4 = c(c(1011, 1011, 1011, 1011, 1011, 1111, 1111, 1111, 1111, 1111,
1021, 1021, 1021, 1021, 1021, 1122, 1122, 1122, 1122, 1122)), # additional level added
labour = c(1, 5, 10, 20, 25, 5, 10, 20, 25, 30,
2, 6, 11, 21, 26, 6, 11, 21, 26, 31))
output$hcontainer <- renderHighchart({
temp <- df %>%
group_by(year, level_2) %>%
summarize(Sum = sum(labour)) %>%
arrange(level_2)
hchart(temp, "line", hcaes(x = year, y = Sum, group = level_2)) %>%
hc_plotOptions(series = list(events = list(click = canvasClickFunction)))
})
canvasClickFunction <- JS("function(event) {Shiny.onInputChange('canvasClicked', [this.name, event.point.category]);}")
#second highcharter which should appear when user clicked on the serie named 10
output$hcontainer2 <- renderHighchart({
req(input$canvasClicked[[1]])
temp2 <- df %>%
filter(level_2 == input$canvasClicked[[1]]) %>% # filter selected by click
group_by(year, level_3) %>%
summarize(Sum = sum(labour)) %>%
arrange(level_3)
hchart(temp2, "line", hcaes(x = year, y = Sum, group = level_3)) %>%
hc_title(text = paste0("I clicked ",input$canvasClicked[[1]])) %>%
hc_plotOptions(series = list(events = list(click = canvasClickFunction2)))
})
canvasClickFunction2 <- JS("function(event) {Shiny.onInputChange('canvasClicked2', [this.name, event.point.category]);}")
output$hcontainer3 <- renderHighchart({
req(input$canvasClicked2[[1]])
temp3 <- df %>%
filter(level_3 == input$canvasClicked2[[1]]) %>% # filter selected by click
group_by(year, level_4) %>%
summarize(Sum = sum(labour)) %>%
arrange(level_4)
hchart(temp3, "line", hcaes(x = year, y = Sum, group = level_4)) %>%
hc_title(text = paste0("I clicked ",input$canvasClicked2[[1]])) #%>%
})
}
shinyApp(ui = ui, server = server)
一旦我能够(两天后),我会接受答案作为参考。