Passing json / data to javascript object with brilliant

I'm trying to figure out how to get R to interact through brilliant with other javascript elements, which I assume means that server.R serves a custom brilliant object (perfectly json formatted?) To ui.R , which then converts to a javascript array. Code of my work in progress:

server.R

 library(shiny) shinyServer( function(input, output) { output$species_table <- renderTable({ iris[iris$Species == input$species,] }) output$json <- RJSONIO::toJSON(iris[iris$Species == input$species,], byrow=T, colNames=T) # error line } ) 

ui.R

 require(shiny) specs = as.character(unique(iris$Species)) names(specs) = specs pageWithSidebar( headerPanel("minimal json handling example"), sidebarPanel(selectInput("species", "Species", specs)), mainPanel( tableOutput("species_table") ) ) 

What server error returns:

 Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) 

.. because this is obviously the wrong approach. Without server.R line output$json <- ... result works, and looks like this , so the rest of the code is fine. But I also want to get json (or any alternative format) after some time and call the subsequent javascript action to read it as an array object. Grateful for any pointers and apologized in advance if my description is unclear.

+2
r shiny
source share
2 answers

So, this error usually means that you need to wrap reactive({}) around something, in this case your toJSON . This works and displays JSON data.

ui.r

 require(shiny) specs = as.character(unique(iris$Species)) names(specs) = specs pageWithSidebar( headerPanel("minimal json handling example"), sidebarPanel(selectInput("species", "Species", specs)), mainPanel( #tableOutput("species_table") textOutput("json") ) ) 

server.r

 library(shiny) shinyServer( function(input, output) { output$species_table <- renderTable({ iris[iris$Species == input$species,] }) output$json <-reactive({ RJSONIO::toJSON(iris[iris$Species == input$species,], byrow=T, colNames=T) })# error line } ) 
+2
source share

In the interest of others, this is a working formula. If someone can offer a more elegant solution, I would be grateful. Open the browser javascript console log to update updated object information.

server.R

 library(shiny) iris <- datasets::iris names(iris) <- gsub('[/.]','_',tolower(names(iris))) shinyServer( function(input, output) { output$json <- reactive({ paste('<script>data=', RJSONIO::toJSON(iris[iris$species == input$species,], byrow=T, colNames=T), ';console.log(data[0]);', # print 1 data line to console '</script>') }) } ) 

ui.R

 require(shiny) iris <- datasets::iris names(iris) <- gsub('[/.]','_',tolower(names(iris))) specs <- as.character(unique(iris$species)) names(specs) <- specs pageWithSidebar( headerPanel("minimal json handling example"), sidebarPanel(selectInput("species", "Species", specs)), mainPanel( htmlOutput("json") ) ) 
+3
source share

All Articles