Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 212017

R Shiny - Coding for regular API JSON update

$
0
0

I am attempting to code a Shiny application that hits a JSON API as its main data source. The app would hit the API once per minute to retrieve updated data in JSON format, then use jsonlite package to parse it into a data frame for use in the Shiny app. My goal is to create valueBox and chart outputs and have those update each minute as the data count goes up and changes, to create a "stream-like" dashboard.

I've attempted to use these two examples to code this out, but my app does not seem to be able to access my data source when it's loaded.

Twitter Sentiment Analysis

Shiny CRANDash

Here's some sample code. Right now, I am just using a dummy test JSON source and appending the current time to see that the 60 second update is working.

helper_functions.R:

library(jsonlite)
getDataSet <- function() {
        URL = "http://jsonplaceholder.typicode.com/posts"
        results <- fromJSON(URL)
        results_df <- data.frame(results)
        results_df$RowCreated <- Sys.time()

        #I have also added this call to return (results_df) to no avail, so it is commented out
        #return(results_df)
      }

server.R

 source('helper_functions.R')

 shinyServer(function(input, output, session) {

 autoInvalidate = reactiveTimer(6000,session)

 get_input = reactive({
     autoInvalidate()
     #show progress bar
     withProgress(session, {
       setProgress(message = 'Collecting API data...')
       getDataSet()
     })
  })

  output$currentTime <- renderText({
     invalidateLater(1000,session)
     format(Sys.time())
  })

  #attempting to just write the data frame to a table to see it
  output$SampleDataFrame <- renderDataTable(
     getDataSet
  )

  #simply output the first row's RowCreated as a text field
  output$RowCreated <- renderText(
    getDataSet[1,]$RowCreated
   )

  })

ui.R

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Tab 1", tabName = "Tab 1", icon = icon("check-circle"), badgeLabel = "testing", badgeColor = "red"),
    menuItem("Tab 2", tabName = "Tab 2", icon = icon("database"), badgeLabel = "testing", badgeColor = "blue"),
    menuItem("Tab 3", tabName = "Tab 3", icon = icon("database"), badgeLabel = "testing", badgeColor = "blue")
  )
)
body <- dashboardBody(
          fluidRow(
              tags$code(
                "Data Last Updated from API: ", textOutput("currentTime", container = span)
              ),
            h2("Example Header"),
            fluidRow(
              textOutput("RowCreated")
            )
    )
)

dashboardPage(
  skin="black",
  dashboardHeader(title = "Sample"),
  sidebar,
  body
)

The dashboard renders, but in the section that outputs "RowCreated" this is the message:

error: object of type 'closure' is not subsettable

I'd expect this to update every 6 seconds, per the invalidate call.

Any help is appreciated. Thanks!


Viewing all articles
Browse latest Browse all 212017

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>