Hello,
I thought that when you use session$sendCustomMessage to pass info from the server to the UI that it converts the object to JSON using jsonlite::toJSON, but that doesn't appear to be the case. Here is an example:
df <- list(mydf=data.frame(v1="Hello",v2="World",
stringsAsFactors=FALSE))
library(shiny)
ui <- shinyUI(fluidPage(
singleton(tags$script(HTML(
"Shiny.addCustomMessageHandler('test-json',function(object) {
console.log(JSON.stringify(object));
})"
))),
actionButton("test","Press me")
))
server <- shinyServer(function(input, output, session) {
observeEvent(input$test,{
print(jsonlite::toJSON(df))
session$sendCustomMessage('test-json',df)
})
})
# Run the application
shinyApp(ui = ui, server = server)
When I print jsonlite::toJSON(df) I get this:
{ mydf: [ {v1: "Hello", v2: "World"} ] }
But when I pass the object to the UI and print the JSON to the console I get:
{ mydf: { v1: ["Hello"], v2: ["World"] } }
The placement of the brackets is different, and it is causing me some problems.
Is there a way to ensure that when a data.frame in a list is passed to the UI that the JSON wraps the data.frame in brackets?
Thanks