【问题标题】:Display HTTP GET Data in SHINY在 SHINY 中显示 HTTP GET 数据
【发布时间】:2016-06-16 09:36:48
【问题描述】:

我有一个 PostgreSQL 数据库,我使用 Talend DI 创建了 RESTful Api,它允许我查询表
现在我的问题是如何在 Shiny 中调用我的 Api 并显示收到的数据?

http 获取响应示例:

 [{"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:我在 Shiny 中使用“RPostgreSQL”包直接连接到我的数据库没有问题,但我更喜欢将它与闪亮分开,那就是使用我的网络服务

【问题讨论】:

    标签: json http-get shiny httr


    【解决方案1】:

    如果您已经设置了 API,那么您可以执行以下操作:

    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))
    

    例如,将文本框中的文本设置为“param1=value1&param2=value2”会向所选 URL 发送 GET 请求。这可能不是您想要实现的方式,但也许这会让您对如何做到这一点有所了解。

    如果我使用示例查询字符串,我可以通过键入来访问“param1”的值:

    content(res)$args$param1
    

    【讨论】:

    • 工作得很好,但它呈现的数据不是表格,我怎样才能把它放在一个表格中?
    • 您必须构建一个 data.frame 或一个矩阵来渲染表格,查看?renderDataTable?renderTable。我更新了我的答案,将返回的 GET 参数放在一个表中。
    猜你喜欢
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2017-11-15
    • 2020-04-28
    • 2021-09-02
    • 2021-09-21
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多