skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER

    Corrected code which works.

    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()
            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())
      })
    
      output$SampleDataFrame <- renderDataTable(
         getDataSet()
      )
    
      #simply output the first row's RowCreated as a text field
      #minor tweak to format date correctly
      output$RowCreated <- renderText(
        format(getDataSet()[1,]$RowCreated[1])
       )
    
      })
    

    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
    )
    

  2. I know I am way late to this party, but I don’t think your solution should work…
    you have:

    output$currentTime <- renderText({
    

    then in your second renderText you have:

    output$RowCreated <- renderText(
    

    notice the none existent { in the second one?

    Just trying to replicate the code, but found that in my attempt

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search