Varun Anant
Varun Anant

Reputation: 57

R API to be accessible over the web

Hi I have R code which I have converted into an API using the plumber package.

library(plumber)
r <- plumb("code.R")
r$run(port=8000)

The code sits in the file code.R and using the above lines I am able to access the API from my local machine from the URL http://localhost:8000/functionname

However when I replace the local host with my IP address and access the same from other machines in the network, I am unable to access the API. Why is that?

Upvotes: 3

Views: 2064

Answers (2)

qoheleth
qoheleth

Reputation: 2309

making the host explicit works on my machine.

r$run(host = "0.0.0.0",port=8000)

and then to access it it is just

your_ip:8000/functionname

Upvotes: 6

Jeff Allen
Jeff Allen

Reputation: 17517

By default plumber listens on the host 0.0.0.0 which means it should be listening on all devices, whether your IP or localhost. It sounds like either your machine has a firewall or your organization might have a firewall in front of the machine you're using. You'd also need to confirm that the IP address is routable (e.g. you're not trying to access a 192.168.. address from another LAN).

I'd discourage you from actually trying to host an API on your personal machine or a server where you're doing iterative development, as it requires opening up your firewall and accepting traffic on a more sensitive server. The best practice here would be to deploy your API to a server that's intended to receive public traffic. Here's one easy way to get that setup that's now built in to the development version of plumber: https://plumber.trestletech.com/docs/digitalocean/

Upvotes: 2

Related Questions