Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to send data to a shiny app?

I am building a web application using java (backend) and javascript. At some point my application retrieves some specific data from a remote database. I want to embed a shiny app in my web-application that reads, analyses and visualizes this data. The data that is retrieved is dependent on user interaction with my application.

So basically I am trying to send data to a shiny app (possibly using REST). Is this possible?

like image 802
Elias Avatar asked Feb 06 '19 14:02

Elias


2 Answers

Just to add an example: if you want to:

  1. Calculate in R -> Use in any web app: plumber
  2. GET from REST API -> Shiny: httr

A minimal example of using httr to load some JSON data (from OpenCPU) in Shiny:

library(shiny)
library(httr)
library(dplyr)

ui <- fluidPage(
  tableOutput("tbl")
)

server <- function(input, output, session) {
  output$tbl <- renderTable( {
    # GET request from an API
    req <- httr::GET(url = "cran.ocpu.io/ggplot2/data/msleep/json")
    req_parsed <- httr::content(req, type = "application/json")

    # Convert to data.frame
    dplyr::bind_rows(req_parsed)
  })
}

shinyApp(ui, server)
like image 174
GyD Avatar answered Oct 24 '22 05:10

GyD


Of course you can! An R Shiny app can receive data in the same ways any web app can. E.g. it could run an internal timer to go and fetch data from an API, scrape data from the web, or access a database.

Some suggestions

  • You could simply connect to a remote database (e.g. here's how to connect to a sql server database (it's easier than it looks)
  • You could build an API in whatever language suited you. If you wanted to use R, the plumber package would be a good place to start
  • One unusual way (just to show what's possible) is if you already know how to build a web app, then you could make one that displays the data you want your shiny app to have access to, then have the Shiny app scrape the data at whatever interval you choose (5 seconds, 5 hours, 5 days, anything). Note that this would not be a good solution for any sensitive data

Two of the most used scraping packages are rvest and (Hadley Wickham's) httr. These are a great start for accessing APIs or scraping raw data from anywhere on the web

If you wanted to connect to a database, I recommend deciding which one you'll use then googling how to connect to it using R. Once you've succeeded, you can move that code inside the Shiny app!

like image 29
stevec Avatar answered Oct 24 '22 05:10

stevec