plumber API giving extra brackets


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:


  #' 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:
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.

Thank you.

In R, when you a have data.frame only one row, you can asked to unbox this and do not have that {

df <- fromJSON('[{"df.time":"2019-05-14 05:44:28","df.temperature":33.49}]')
#> {"df.time":"2019-05-14 05:44:28","df.temperature":33.49}

Created on 2019-07-04 by the reprex package (v0.3.0.9000)

With plumber, the only option you have I think is the auto_unbox variant but it does not do that for one row data.frame

You may try to bypass serialization and creating yourself the json string

That is said, [{"df.time":"2019-05-14 05:44:28","df.temperature":33.49}] is still correct json that you should be able to read.

Your example is correctly picked up by firefox


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.