# find a free port
port <- httpuv::randomPort()
port
# Deploy a clustering process listening for data on the port
rp1 <- publish_DSC_via_WebService("DSC_DBSTREAM(r = .05)", port = port)
rp1
# look at ? DSC_WebService for a convenient interface.
# Here we we show how to connect to the port and send data manually.
library(httr)
# the info verb returns some basic information about the clusterer.
resp <- RETRY("GET", paste0("http://localhost:", port, "/info"))
d <- content(resp, show_col_types = FALSE)
d
# create a local data stream and send it to the clusterer using the update verb.
dsd <- DSD_Gaussians(k = 3, d = 2, noise = 0.05)
tmp <- tempfile()
stream::write_stream(dsd, tmp, n = 500, header = TRUE)
resp <- POST(paste0("http://localhost:", port, "/update"),
body = list(upload = upload_file(tmp)))
unlink(tmp)
resp
# retrieve the cluster centers using the get_centers verb
resp <- GET(paste0("http://localhost:", port, "/get_centers"))
d <- content(resp, show_col_types = FALSE)
head(d)
plot(dsd, n = 100)
points(d, col = "red", pch = 3, lwd = 3)
# kill the process.
rp1$kill()
rp1
# Debug the interface (run the service and start a web interface)
if (interactive())
publish_DSC_via_WebService("DSC_DBSTREAM(r = .05)",
port = port, debug = TRUE)
Run the code above in your browser using DataLab