Anass El Ghellab Anass El Ghellab - 5 months ago 37
JSON Question

Display HTTP GET Data in SHINY

I have a PostgreSQL database for which I created RESTful Api's using Talend DI that allow me to query tables

Now my question is how to call my Api's in Shiny and display the data received ?

example of http get response :

[{"medical_consultations":{"medical_consultation":[{"id":"1640087","id_consultation":"GFAPAAPA P 834406012009","consultation_date":"07-01-2009","id_center":"APA"},{"id":"1640088","id_consultation":"GFAPAAPA P1079007012010","consultation_date":"08-01-2010","id_center":"APA"},{"id":"1640089","id_consultation":"GFAPAAPA P1098811052007","consultation_date":"12-05-2007","id_center":"APA"}]}}]


Ps: I have no problem doing it using 'RPostgreSQL' package in Shiny to connect directly to my database but I prefer to separate it from shiny and that would be by using my web services

Answer

If you have the API all setup then you could do something like this:

require(shiny)
require(httr)

url <- "http://httpbin.org/get?"

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput('GETargs',"GET string"),
      actionButton('sendGET','Send')
    ),
    mainPanel(
      verbatimTextOutput('GETresponse'),
      dataTableOutput('table1')
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$sendGET, {
    getargs <- input$GETargs

    if( is.null(input$GETargs) ) getargs <- ""

    res                <- GET(sprintf("%s%s",url, getargs))
    output$GETresponse <- renderPrint(content(res))

    output$table1 <- renderDataTable( as.data.frame(content(res)$args) )
  })
}

runApp(shinyApp(ui,server))

For example setting the text in the textbox to "param1=value1&param2=value2" sends a GET request to the selected URL. This might not be the way you want to implement this but maybe this gives you some idea on how this can be done.

If i use the example query sting I could for instance access "param1"'s value by typing:

content(res)$args$param1
Comments