rplumber

Making plumber API available over internet


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.


Solution

  • 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.sstatic.net/mEcg4.png

    Step 2:

    https://i.sstatic.net/EapIw.png

    Step 3:

    https://i.sstatic.net/h74C6.png

    Step 4:

    [https://i.sstatic.net/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