2

I am fairly new to the plumber package in R. I got a working API to run locally on my machine and I can access it from a live JS application on the web with the code:

r <- plumb("my_api_code.r")
r$run(host = "0.0.0.0", port = 8000)

I have read that it is not a good idea to host the r code on say my personal laptop, but I just wanted to be able to run the above code, open an R server session, and then access this over the internet from a different machine for demonstration purposes.

Is there a way to access this api over the internet based on the IP address of the machine running the R session? I wasn't able to find documentation for the r$run r$host r$serve functions that are available.

Mxblsdl
  • 484
  • 6
  • 16
  • 1
    Create a VM or container on a hosting service (Azure, AWS, Google, etc) and run your app there. – Hong Ooi Mar 17 '19 at 20:39
  • Could you expand on your answer? Do you mean host the R code on one of these services? I know the ultimate answer is to host the R code on R Studio Connect, Digital Ocean, Docker.... but I'm just trying to show a demonstration of what plumber can do without setting up/paying for a hosting service. Also, we have a hosting service for our website but I'm not sure how to install and run an instance of R on our server. – Mxblsdl Mar 17 '19 at 21:26

2 Answers2

2

You can try this this plot/API available over the internet

plumbr.R

#' @get /plotly
#' @serializer htmlwidget
plotlygraph <- function(){
 library(plumber)
 library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
 p <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo') %>%
   add_trace(y = ~LA_Zoo, name = 'LA Zoo') %>%
   layout(yaxis = list(title = 'Count'), barmode = 'group')
 return(p)
}

Run this above code using the following command on R Console

r <- plumber::plumb("plumbr.R")
r$run(host="0.0.0.0", port=8000)

After running this The Swagger window will open There is button "Get/Plotly" click on this then click on "try out" click on "Execute" wait for few seconds

Step 1:

https://i.stack.imgur.com/mEcg4.png

Step 2:

https://i.stack.imgur.com/EapIw.png

Step 3:

https://i.stack.imgur.com/h74C6.png

Step 4:

[https://i.stack.imgur.com/9CkGc.png][4]

then it shows HTML code above this, it gives a Request URL like this

http://127.0.0.1:8000/plotly

copy that link paste in the browser you will see the plot

Then using terminal/command prompt get your IP...

Assume your IP is "192.168.10.04"

copy it and replace it with this like:-

http://192.168.10.04:8000/plotly

Then copy that link and paste it to your IFRAME of another web app

You will see your R Plot on a new framework (web app)...

I think this should help you

user229044
  • 232,980
  • 40
  • 330
  • 338
Mahesh Kulkarni
  • 110
  • 2
  • 15
1

It should work beautifully given you

  • know the ip of your laptop
  • the ip is accessible to the others (in your team), i.e. you are on the same network, or your computer has a globally valid ip
  • no firewall blocks port 8000
  • CORS is enabled by something like
#' @filter cors
cors <- function(req, res) {
   res$setHeader("Access-Control-Allow-Origin", "*")
   plumber::forward()
}

Obviously, this is not the answer for a production environment but works well for just showcasing/testing.

Ott Toomet
  • 1,894
  • 15
  • 25
  • I think this is the correct answer, although I have not gotten it to work yet. The operation times out. I think it might be an issue with the firewall. – Mxblsdl Mar 27 '19 at 17:40