【问题标题】:Javascript in R dashboard : plot$series(event = ...)R仪表板中的Javascript:plot$series(event = ...)
【发布时间】:2019-05-13 07:37:57
【问题描述】:

我找到了一个示例here,它与我想要做的完全匹配。

唯一的问题是我不知道将代码放在 R 仪表板脚本中的什么位置。作者说他犯了一个错误:代码需要在 plot$series(event = ...) 但是我可以把 plot$series(event = ...) 放在哪里?

我试图把它放在 jsCode(包 shinyjs)的开头,但什么也没发生。我也在ui里试过...

library(highcharter)
library('shinyjs')

data_plot <- data.frame(categories = c("A", "B", "C", "D"),
                    serie1 = c(1563, 1458, 205, 695),
                    serie2 = c(562, 258, 17, 115),
                    serie3 = c(324, 654, 23, 987),
                    serie4 = c(123, 567, 234, 12),
                    serie5 = c(376, 88, 98, 123)
                    )
jsCode <- "
plot$series(event = legendItemClick = #! function(event) {
if (!this.visible )
return false;

var seriesIndex = this.index;
var series = this.chart.series;

for (var i = 0; i < series.length; i++)
{
if (series[i].index != seriesIndex)
{
series[i].visible ?
  series[i].hide() :
  series[i].show();
} 
}
return false;
} !#)"

ui <- dashboardPage(
dashboardHeader(title = "My Dashboard"),

dashboardSidebar(),

dashboardBody(highchartOutput ("hc1")))

server <- function(input, output,session) {

#Normal Chart
output$hc1 <- renderHighchart ({
Hch <- highchart(hcaes(x = Spring ,y = Ponctuation)) %>% 
  hc_title(text = "Graph",
           margin = 20, align = "left",
           style = list(color = "#FE8000", useHTML = TRUE)) %>% 
  hc_xAxis(categories = data_plot$categories, title = list(text = "Number 
 of spring",color = "#FE8000")) %>%
  hc_yAxis(title = list(text = "Result", color = "#FE8000"))%>% 
  hc_add_series(name = 'serie1', data = data_plot$serie1) %>% 
  hc_add_series(name = 'serie2', data = data_plot$serie2)%>% 
  hc_add_series(name = 'serie3', data = data_plot$serie3)%>% 
  hc_add_series(name = 'serie4', data = data_plot$serie4)%>% 
  hc_add_series(name = 'serie5', data = data_plot$serie5)

  Hch})


 }


 shinyApp(ui, server)

编辑

我更改了上面的 jsCode 但这不起作用:

hc1$series(legendItemClick = function(event) {
if (!this.visible )
return false;

var seriesIndex = this.index;
var series = this.chart.series;

for (var i = 0; i < series.length; i++)
{
if (series[i].index != seriesIndex)
{
series[i].visible ?
series[i].hide() :
series[i].show();
} 
}
return false;
})

【问题讨论】:

  • 您能否分享一个可重现的示例 - 包含一些示例数据或 R 的内置数据集?
  • 对不起,忘记放数据了,现在已经编辑好了

标签: r r-highcharter


【解决方案1】:

更新:

在这种情况下,我们真的不需要shinyjs,因为js 代码必须作为参数值传递给函数hc_plotOptions(),如上一个问题中提到的,它必须是@ 的值987654326@。

library(highcharter)
library(shiny)
library(shinydashboard)

data_plot <- data.frame(categories = c("A", "B", "C", "D"),
                        serie1 = c(1563, 1458, 205, 695),
                        serie2 = c(562, 258, 17, 115),
                        serie3 = c(324, 654, 23, 987),
                        serie4 = c(123, 567, 234, 12),
                        serie5 = c(376, 88, 98, 123)
)

jsCode <- JS("function(event) {
if (!this.visible )
return false;

var seriesIndex = this.index;
var series = this.chart.series;

for (var i = 0; i < series.length; i++)
{
if (series[i].index != seriesIndex)
{
series[i].visible ?
  series[i].hide() :
  series[i].show();
} 
}
return false;
}")

  ui <- dashboardPage(


    dashboardHeader(title = "My Dashboard"),

    dashboardSidebar(),

    dashboardBody(  

      highchartOutput ("hc1")))

  server <- function(input, output,session) {

    #Normal Chart
    output$hc1 <- renderHighchart ({
      Hch <- highchart(hcaes(x = Spring ,y = Ponctuation)) %>% 
        hc_title(text = "Graph",
                 margin = 20, align = "left",
                 style = list(color = "#FE8000", useHTML = TRUE)) %>% 
        hc_xAxis(categories = data_plot$categories, title = list(text = "Number 
 of spring",color = "#FE8000")) %>%
        hc_yAxis(title = list(text = "Result", color = "#FE8000"))%>% 
        hc_add_series(name = 'serie1', data = data_plot$serie1) %>% 
        hc_add_series(name = 'serie2', data = data_plot$serie2)%>% 
        hc_add_series(name = 'serie3', data = data_plot$serie3)%>% 
        hc_add_series(name = 'serie4', data = data_plot$serie4)%>% 
        hc_add_series(name = 'serie5', data = data_plot$serie5) %>% 
        hc_plotOptions(series = list(events = list(legendItemClick = jsCode)))

      Hch})


  }


  shinyApp(ui, server)

【讨论】:

  • 谢谢!是的,它不起作用,但我不知道为什么
  • 如果它不起作用,您可以删除标记为已回答
  • 在某种程度上你回答了我的问题,但还可以
  • 你能查到最新的答案吗?它似乎正在工作
  • 是的!这就是我想要的!非常感谢!
猜你喜欢
  • 2012-09-14
  • 2011-01-12
  • 2012-12-07
  • 2015-04-22
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 2018-07-07
  • 2018-01-19
相关资源
最近更新 更多