Using global data in High Charts point click custom function in R Shiny

半世苍凉 提交于 2019-12-24 02:57:13

问题


I am trying to show a different table or plot in a different div when a bar on a a bar plot is clicked. I have come this far.

server.R

    library(shiny)
    shinyServer(function(input,output,session) {
    custom_data = # a data frame in R
    barclick_func <- "#! function() {
                            var cats = ['100','200','3000','4000'];
                            var datum = [20,40,30,10];
                    }       
                    $('#container_subcat').highcharts({
                            chart: {
                                    type: 'column',
                            },
                            xAxis: { categories:cats },
                            series: [{data :datum}]
                    }); 
    } !#"

    output$my_plot <- renderChart2({
                    a <- hPlot(x="id",y="variable",data=custom_data,type="column")
                    a$tooltip( animation = 'true', formatter = "#! function() {return '<b>' + 'Frequency of tag_id ' + this.x + '</b>' + ' is ' + this.y;} !#")
                    a$plotOptions(series = list(color = '#388E8E'),column = list(dataLabels = list(enabled = T, rotation =-90, align = 'right', color = '#FFFFFF', x = 4, y = 10),
cursor = 'pointer', point = list(events = list(click = barclick_func))))
                    return(a)
            })
    })

ui.R

library(shiny)
require(rCharts)
shinyUI(fluidPage(
  tags$head(
    tags$link(rel = "stylesheet", type = "text/css", href = "custom.css"),
    tags$script(src="http://code.jquery.com/jquery-git2.min.js"),
  ),
  titlePanel("Test"),
  fluidRow(  
      showOutput("my_plot","highcharts")
  ),
  div(id="container_subcat",style="min-width: 310-px; height: 400px; margin: 0 auto; margin-top:100px;")
 )
))

In the above server.R script, in barclick_func() function, the data(variables cats and datum) is hardcoded. The above app works as expected when a bar is clicked, another plot pops up properly with the data.

But, if I want to use another data, that is if I have another data frame in R and want to use that data frame in barclick_func(), the console is throwing an error that the variable is not recognized and if I look at the type of that variable, it shows as 'undefined'. Can anyone suggest how to send data to the javascript function in this particular case. Ideally, my desired code is this.

server.R

    library(shiny)
    shinyServer(function(input,output,session) {
    custom_data = # a data frame in R

    custom_data2 = # another data frame in R for which I wanna shoe a plot when the bar is clicked.

    barclick_func <- "#! function() { 

                            var cats = #subsetting custom_data2 ;
                            var datum = #subsetting custom_data2 ;

                      }       
                    $('#container_subcat').highcharts({
                            chart: {
                                    type: 'column',
                            },
                            xAxis: { categories:cats },
                            series: [{data :datum}]
                    }); 
    } !#"

    output$my_plot <- renderChart2({
                    a <- hPlot(x="id",y="variable",data=custom_data,type="column")
                    a$tooltip( animation = 'true', formatter = "#! function() {return '<b>' + 'Frequency of tag_id ' + this.x + '</b>' + ' is ' + this.y;} !#")
                    a$plotOptions(series = list(color = '#388E8E'),column = list(dataLabels = list(enabled = T, rotation =-90, align = 'right', color = '#FFFFFF', x = 4, y = 10), 
cursor = 'pointer', point = list(events = list(click = barclick_func))))
                    return(a)
            })
    })

回答1:


You could try using paste to add the data to your barclick_func.

For example:

         custom_data2 = data.frame(cats=c(100,200,3000,400),datum=c(20,40,30,10))


        barclick_func <- paste("#! function() { 

        var cats = ",paste('[',paste(custom_data2$cats,collapse=','),']',sep=''),"
        var datum = ",paste('[',paste(custom_data2$datum,collapse=','),']',sep=''),"; 

        }       
        $('#container_subcat').highcharts({
        chart: {
        type: 'column',
        },
        xAxis: { categories:cats },
        series: [{data :datum}]
        }); 
        } !#")

Should give the same barclick_func as in your hardcoded version.



来源:https://stackoverflow.com/questions/31119432/using-global-data-in-high-charts-point-click-custom-function-in-r-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!