datastorm-open / rAmCharts

API for Amcharts
48 stars 16 forks source link

Shiny.onInputChange() in Shiny module not working #59

Closed JianWang2016 closed 7 years ago

JianWang2016 commented 7 years ago

Hi, I am trying to capture rAmCharts click event in shiny. The example works great until the codes are adapted for shiny modules. Any advice on how to change shiny.onInputChange() javascript is greatly appreciated.

Here is the code from rAmCharts Getting Started section:

` require(shiny)

shinyApp( ui = fluidPage( amChartsOutput(outputId = "amchart"), fluidRow( column(width = 1, strong("Values: ")), column(width = 3, verbatimTextOutput("click")) ) ),

server = function(input, output) { output$amchart <- renderAmCharts({

build the chart

  pipeR::pipeline(
    amBoxplot(iris[, -5]),
    addListener(name = 'clickGraphItem',
                expression = paste('function (event) {',
                                   'Shiny.onInputChange(\'myValues\', event.item.values);',
                                   '}'))
  )
})
output$click <- renderPrint(input$myValues)

} ) ` Here is the module UI and Server codes in a file named testClickModule.R. The graph still displays but the click event is not registered. I get a sense that the name space is the culprit but didn't manage to get it working after testing session$ns.

` clickMeUI <- function(id) { ns <- NS(id)

tagList( amChartsOutput(outputId = ns("amchart")), fluidRow( column(width = 1, strong("Values: ")), column(width = 3, verbatimTextOutput(ns("click"))) ) ) }

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

output$amchart <- renderAmCharts({
  # build the chart
  pipeR::pipeline(
    amBoxplot(iris[, -5]),
    addListener(name = 'clickGraphItem',
                expression = paste('function (event) {',
                                   'Shiny.onInputChange(\'myValues\', event.item.values);',
                                   '}'))
  )
})

output$click <- renderPrint(input$myValues)

}

`

Here is the app.R using the module defined above

` library(shiny) library(rAmCharts)

source("testClickModule.R", local=TRUE)

ui <- fixedPage( h2("Module example"), clickMeUI("testClick") )

server <- function(input, output, session) { callModule(clickMe, "testClick")

}

shinyApp(ui, server) `

JianWang2016 commented 7 years ago

Could someone please take a look at this? Thanks much.

debsush commented 7 years ago

Try changing the below Shiny.onInputChange(\'myValues\', event.item.values); to Shiny.onInputChange(myValues, event.item.values); or Shiny.onInputChange("myValues", event.item.values);

bthieurmel commented 7 years ago

Perhaps some module bad definition. This simple example working fine with rAmCharts 2.1.2, actually on github, but coming in next day on CRAN. 2.1.1 have some bug on listeners.

require(shiny)
require(rAmCharts)

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

  output$amb <- renderAmCharts({
    pipeR::pipeline(
      amBoxplot(iris[, -5]),
      addListener(name = 'clickGraphItem',
                  expression = paste('function (event) {',
                                     'Shiny.onInputChange("myValues", event.item.values);',
                                     '}'))
    )
  }) 

  observe({
    print(input$myValues)
  })
}

ui <- fluidPage(
  amChartsOutput("amb")
)

shinyApp(ui = ui, server = server)