I'm using an R script to query an API, process data, and then run it on a shiny server. Basically there's a input field, and then it queries that username. However, I'd like to do this from a discord bot. So I'm wondering if there's a way to listen to www.example.com/endpoint/ and then use the input from this endpoint into the query?
Asked
Active
Viewed 196 times
0
-
Are you saying that `/endpoint/` at some point puts out something new, and then when it changes you want to do something with that new "thing"? – r2evans Jun 05 '20 at 19:03
-
No, a discordbot with post something there. I figured it out though, using https://shiny.rstudio.com/articles/client-data.html – RegressionSquirrel Jun 05 '20 at 19:08
1 Answers
1
The solution was to use the plumber package. This essentially creates an endpoint for your script. An example:
# plumber.R
#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg=""){
list(msg = paste0("The message is: '", msg, "'"))
}
Then you save that as a R-script, and use the following:
library(plumber)
r <- plumb("plumber.R") # Where 'plumber.R' is the location of the file shown above
r$run(port=8000)
I'm running this on an Ubuntu server, where following code starts the service. In that case you don't need the lines above, only the plumber.R file since the code below in essence does the same:
sudo nano /etc/systemd/system/plumber-api.service
Content of the file should be:
[Unit]
Description=Plumber API
# After=postgresql
# (or mariadb, mysql, etc if you use a DB with Plumber, otherwise leave this commented)
[Service]
ExecStart=/usr/bin/Rscript -e "api <- plumber::plumb('/your-dir/your-api-script.R'); api$run(port=8080, host='0.0.0.0')"
Restart=on-abnormal
WorkingDirectory=/your-dir/
[Install]
WantedBy=multi-user.target
https://www.rplumber.io/docs/hosting.html
sudo systemctl start plumber-api # starts the service
Details on the plumber package here

RegressionSquirrel
- 422
- 3
- 8
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/26334165) – Itchydon Jun 06 '20 at 12:53
-
1Yeah; good point. I've edited the answer to reflect the steps I took and have included example code that will get people going. – RegressionSquirrel Jun 06 '20 at 13:14