Assign a column from dynamical API calls to a new list within a shiny app

Within a shiny app i have a reactive data frame which calls an API every 10 seconds. Every time the API is called and the new data frame is initialized with new data i want the abv column to be stored into a new list

beer = reactive({
        invalidateLater(10000)
        beer = jsonlite::fromJSON("https://api.punkapi.com/v2/beers/random", flatten = T) %>%
            tibble() %>% 
            select(name, abv, description)
    })

This list must somehow be also reactive and be updated every time an API call is received. I basically want end up with a list from which i can plot the different values for abv i get from the consecutive API calls. How would i do this?

library(shiny)
library(tidyverse)
ui <- fluidPage(
  tableOutput("beert")
  ,plotOutput("myplot")
)

server <- function(input, output, session) {
  
  abvlist <- reactiveVal(NULL)
  
  beer = reactive({
    invalidateLater(1000)
    new <- jsonlite::fromJSON("https://api.punkapi.com/v2/beers/random", flatten = T) %>%
      tibble() %>% 
      select(name, abv, description)
 
    new
  })
  
  observeEvent(beer(),
               {  
                 abvlist(c(abvlist(),beer()$abv))
                 })
  
  output$beert <- renderTable({
    beer()
  })
  output$myplot <- renderPlot({
     abv <- req(abvlist())
    hist(abv)
  })
}
shinyApp(ui, server)

This is doing the job, thank you!
But its not really clear to me why u sign the fromJSON call to a new variable. The code would also run without initializing a variabel called new

I agree,
it would allow further manipulations on new, if needed but in the current form is redundant.
You had similar set up in your own example ...

I see, for further manipulations it definitely would make sense.
I had that setup since i was experimenting a little, but it turns out, it isnt necessary for this urpose : )

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.