How to return JSON using plumber api in R

I am new to R. I have to expose the recreation service using R, so I found a plumber to expose the recreation service using R, I successfully executed the plumber in R, but in return I get a json array as shown below

[{"Name": "Rajesh", "age": "10"}]

how to remove an array from the answer above

my expected result looks lower

{"name": "Rajesh", "age": "10"}

The code

 library(plumber)
r <- plumb("MyFile.R")
r$run(port=8000)

My .R file is listed below

    #* @post /sum
addTwo <- function(){
  name<-c("Rajesh")
  age<-c("10")
  df<-data.frame(name,age)

  return(df)

}
+4
source share
1 answer
library(jsonlite)

df <- jsonlite::toJSON(data.frame(name, age, auto_unbox=TRUE))
0
source

Source: https://habr.com/ru/post/1670805/


All Articles