【发布时间】:2019-02-22 03:42:22
【问题描述】:
我正在构建一个闪亮的应用程序,我想要完成的一件事是创建一个下拉菜单。我想将劳动变量绘制为不同级别的年份变量的函数。请参阅下面的示例数据框:
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
我已经知道如何获得第一个下拉菜单(带有 level_3 的图表 2 是通过单击图表 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)
【问题讨论】:
-
您似乎绑定了两次,即第二张图表中的
canvasClickFunction,我认为它根本不应该存在。创建另一个canvasClickFunction2并将其绑定到图表 2hc_plotOptions,然后将其用于图表 3 -
@猪排。谢谢。这是工作!我还需要更改
canvasClicked,然后创建第二个canvasClickFunction。我会更新上面的应用程序。 -
好的很好:)),一旦你更新它,请自己发布答案并接受它以供参考
标签: r highcharts shiny shinyjs