Hello,
I have been using the package plumber to create an API that access an influxdb database. This has gone well except I have square brackets surrounding the returned values:
[{"df.time":"2019-05-14 05:44:28","df.temperature":33.49}]
I originally had two of the brackets around the values because I used list(dataframe) at the end because I thought I had to, I have tried unlist(dataframe) but that doesn't give the correct format anymore.
My code:
library(influxdbr)
library(plumber)
#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @get /echo
function(username="", type = "", device = "", tail = ""){
con <- influxdbr::influx_connection("###")
query_data <- paste0("SELECT temperature FROM Example")
db_data <- paste0(username)
docking <- 10
result <- influx_select(con = con,
db = db_data,
field_keys = type,
measurement = device,
group_by = "*",
limit = tail,
order_desc = TRUE,
return_xts = FALSE)
df <- data.frame(result)
dat <- data.frame(df$time, df$temperature)
}
The url is here if you would like to see the result:
http://api.gannantech.com/echo?username=Example&type=temperature&device=Pool&tail=5
I also need to clean up the names such as "df.time" but that a different issue that I suspect won't be hard.
My question is how do I remove the outer square brackets and display json format?
I have spent a fair bit of time googling, but it's very possible I missed something obvious. I've been using the main documentation to try and help + numerous stackoverflow forums but nothing that I've been able to implement. https://www.rplumber.io/docs/
Thank you.