How to load data from a web service into an Rmd file with Flexdashboard and Shiny so that the data is reactive and global?

Hello everyone!

I have a question regarding how to correctly load data from a web service into an rmarkdown file in which I am building a dashboard.

Basically, I have an rmd file in which I am building a dashboard with Flexdashboard and Shiny. I have several "r chunk" where I put maps (leaflet), tables (DT) and various plots (ggplot2 and plotly).

At the moment, I am reading the data through a web service like

www.somewebpage.com/project1/service.py?parameter1=2020&parameter2=ABC

I change the parameters using Shiny and it always returns a JSON with different data. So far I process the web service in each "r chunk", where I convert it to a data frame before displaying the maps, tables or charts.

My question is, is it possible to process only once the change of the parameters and generate only one data frame that can be read by each "r chunk" in the Rmd file?

Basically I am reading the web service in each "r chunk", but I understand that I have to make a reactive data frame maybe in the "r global" chunk?

Here is some example code, where each one is an "r chunk".

{r Chunk1-map, echo = FALSE}

renderLeaflet({
WebService <-  "www.somewebpage.com/project1/service.py?parameter1=2020&parameter2=ABC"
dataURL <- urltools::param_set(urls = WebService, key = "parameter1", value = input$parameter1)   
dataURL <- urltools::param_set(urls = dataURL, key = "parameter2", value = input$parameter2)
resp <- httr::GET(dataURL)
jsonRespText <- content(resp, as = "text")
        jsonRespParsed <- content(resp,as="parsed")
        df_json <- jsonlite::fromJSON(jsonRespText)
        modJson <- jsonRespParsed$Data

df <- modJson %>% bind_rows %>% dplyr::select(var1, var2, var3, var4, var5)

# Below is the code to make a leaflet map with the Data Frame "df".
})

====

{r Chunk2-data-table, echo = FALSE}

renderDT({
WebService <-  "www.somewebpage.com/project1/service.py?parameter1=2020&parameter2=ABC"
dataURL <- urltools::param_set(urls = WebService, key = "parameter1", value = input$parameter1)   
dataURL <- urltools::param_set(urls = dataURL, key = "parameter2", value = input$parameter2)
resp <- httr::GET(dataURL)
jsonRespText <- content(resp, as = "text")
        jsonRespParsed <- content(resp,as="parsed")
        df_json <- jsonlite::fromJSON(jsonRespText)
        modJson <- jsonRespParsed$Data

df <- modJson %>% bind_rows %>% dplyr::select(var1, var2, var3, var4, var5)

# Below is the code to make a data table with the Data Frame "df".
})

====

{r Chunk3-scatterplot, echo = FALSE}

renderD3scatter({
WebService <-  "www.somewebpage.com/project1/service.py?parameter1=2020&parameter2=ABC"
dataURL <- urltools::param_set(urls = WebService, key = "parameter1", value = input$parameter1)   
dataURL <- urltools::param_set(urls = dataURL, key = "parameter2", value = input$parameter2)
resp <- httr::GET(dataURL)
jsonRespText <- content(resp, as = "text")
        jsonRespParsed <- content(resp,as="parsed")
        df_json <- jsonlite::fromJSON(jsonRespText)
        modJson <- jsonRespParsed$Data

df <- modJson %>% bind_rows %>% dplyr::select(var1, var2, var3, var4, var5)

# Below is the code to make a scatter plot with the Data Frame "df".
})

Thank you very much for the help.

EDITED 2

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.