highcharts - Using global data in High Charts point click custom function in R Shiny -
i trying show different table or plot in different div when bar on a bar plot clicked. have come far.
server.r
library(shiny) shinyserver(function(input,output,session) { custom_data = # 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({ <- 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>' + ' ' + 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 above server.r script, in barclick_func()
function, data(variables cats
, datum
) hardcoded. above app works expected when bar clicked, plot pops data.
but, if want use data, if have data frame in r , want use data frame in barclick_func()
, console throwing error variable not recognized , if @ type of variable, shows 'undefined'. can suggest how send data javascript function in particular case. ideally, desired code this.
server.r
library(shiny) shinyserver(function(input,output,session) { custom_data = # data frame in r custom_data2 = # data frame in r wanna shoe plot when bar 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({ <- 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>' + ' ' + 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) }) })
you try using paste
add data 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 same barclick_func
in hardcoded version.
Comments
Post a Comment