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¶meter2=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?
====
Example in my Rmd file:
{r Chunk1-map, echo = FALSE}
renderLeaflet({
WebService <- "www.somewebpage.com/project1/service.py?parameter1=2020¶meter2=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¶meter2=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¶meter2=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".
})
And so on for each element that is using the same web service.
Is it necessary to process the web service in each "r chunk"?
I answer my question.
Just read the web service in one {r chunk}
and the resulting data frame I make it reactive as I show below.
Although for some reason the user inputs have to be in the same {r chunk}
(For the moment).
{r inputs-data, echo=FALSE}
inputPanel(
selectInput(inputId = "parameter1", label = "Parameter 1",
choices = choices1, selected = "2020"),
selectInput(inputId = "parameter2", label = "Parameter 2",
choices = choices2, selected = "ABC"),
actionButton(inputId = "params", label = "Go",
class = "btn btn-primary btn-block")
)
myData <- reactive({
input$params # ACTION BUTTON
WebService <- "www.somewebpage.com/project1/service.py?parameter1=2020¶meter2=ABC"
dataURL <- isolate(urltools::param_set(urls = WebService,
key = "parameter1",
value = input$parameter1))
dataURL <- isolate(urltools::param_set(urls = dataURL,
key = "parameter2",
value = input$parameter2))
resp <- httr::GET(dataURL)
jsonRespText <- httr::content(resp, as = "text")
jsonRespParsed <- httr::content(resp, as="parsed")
df_json <- jsonlite::fromJSON(jsonRespText)
modJson <- jsonRespParsed$Data
df <- modJson %>% bind_rows %>% dplyr::select(var1, var2, var3, var4, var5)
After this, it is possible to access the reactive data frame in all {r chunks}
using myData()
.
For example:
{r data-table, echo=FALSE}
renderDT({
A <- DT::datatable(data = myData())
A
})
The output is shown in the following image.
The leaflet map shown at the top of the image is also made with the same reactive data frame.
Greetings.