简体   繁体   English

如何使用 Flexdashboard 和 Shiny 将 web 服务中的数据“正确”加载到 Rmarkdown 中的仪表板中?

[英]How to "correctly" load data from a web service into a dashboard in Rmarkdown with Flexdashboard and Shiny?

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.我有一个关于如何将数据从 web 服务正确加载到我正在构建仪表板的 rmarkdown 文件中的问题。

Basically, I have an rmd file in which I am building a dashboard with Flexdashboard and Shiny.基本上,我有一个 rmd 文件,我在其中使用 Flexdashboard 和 Shiny 构建仪表板。 I have several "r chunk" where I put maps (leaflet), tables (DT) and various plots (ggplot2 and plotly).我有几个“r 块”,我在其中放置了地图(传单)、表格(DT)和各种图(ggplot2 和 plotly)。

At the moment, I am reading the data through a web service like目前,我正在通过 web 服务读取数据,例如

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.我使用 Shiny 更改参数,它总是返回具有不同数据的 JSON。 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.到目前为止,我在每个“r 块”中处理 web 服务,在显示地图、表格或图表之前将其转换为数据框。

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?我的问题是,是否可以只处理一次参数更改并仅生成一个数据帧,Rmd 文件中的每个“r 块”都可以读取?

==== ====

Example in my Rmd file:我的 Rmd 文件中的示例:

{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".
})

And so on for each element that is using the same web service.对于使用相同 web 服务的每个元素,依此类推。

Is it necessary to process the web service in each "r chunk"?是否需要在每个“r chunk”中处理 web 服务?

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.只需在一个{r chunk}中读取 web 服务,然后生成的数据帧我将其设为响应式,如下所示。

Although for some reason the user inputs have to be in the same {r chunk} (For the moment) .尽管由于某种原因,用户输入必须在同一个{r chunk}中(目前)

{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&parameter2=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() .在此之后,可以使用myData()访问所有{r chunks}中的反应数据帧。

For example:例如:

{r data-table, echo=FALSE}
renderDT({
  A <-  DT::datatable(data = myData())
  A
})

The output is shown in the following image. output 如下图所示。

数据表

The leaflet map shown at the top of the image is also made with the same reactive data frame.图像顶部显示的 leaflet map 也是使用相同的反应数据框制成的。

Greetings.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM