Hi! I've made lots of custom shiny inputs but they've always returned a single value or a vector. I was wondering what the structure of my JS would need to be in order to return a list?
..... //shiny input binding
getValue: function getValue(el) {
var value = [
{id :1, name:"a" },
{ id:2, name:"b"},
{ id:3, name"c"}
]
return value
},
Bonus question! Can I give this list attributes so I can return something else from JS back to r but my user doesn't have to see it? What would that look like?
..... //shiny input binding
getValue: function getValue(el) {
var attr = "test"
var value = [
{id :1, name:"a" },
{ id:2, name:"b"},
{ id:3, name"c"}
]
return value
},
So input$mything would return the same thing or something similar to this?
dummy_input <- list(list(id = 1, name = "a"),
list(id = 2, name = "b"),
list(id = 3, name = "c")
)
attr(dummy_input, 'custom_attr') <- "test"
Hey Maya! Not sure of the specific context, but if this is in a package, I would recommend defining the .onLoad function as follows:
shiny::registerInputHandler("maya.listOutput", function(value, ...) {
if (is.null(value)) {
return(value)
} else {
df <- shiny:::safeFromJSON(value) #convert to df -- this is essentially jsonlite::fromJSON
output <- split(df, seq(nrow(df))) #make a list of the form I think you want
attr(output, 'custom_attr') <- "test" #add custom attribute
return(output)
}
}, force = TRUE) #force = TRUE so error message doesn't appear when the input handler is already loaded
Then in your custom input binding, you would want to change your getValue method to this (just change the output to a JSON string):
getValue: function getValue(el) {
var value = [
{ id :1, name:"a" },
{ id:2, name:"b"},
{ id:3, name: "c"}
]
return(JSON.stringify(value));
},
The last step is to add a getType method in your custom binding. This will tell Shiny that it should return to the user what data as you defined it.