Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return JSON using plumber api in R

Tags:

json

r

plumber

I am new to R . I am supposed to expose rest service using R , So i found plumber to expose rest service using R , I Successfully implemented plumber in R , but in response i am receiving a json array like below

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

how to remove the array from the above response

my expected output is like below

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

Code

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

My File .R is mentioned below

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

  return(df)

}
like image 781
Rajesh Kumar Duraisamy Avatar asked Feb 25 '17 04:02

Rajesh Kumar Duraisamy


1 Answers

library(jsonlite)

df <- jsonlite::toJSON(data.frame(name, age, auto_unbox=TRUE))
like image 148
Eoghan Hynes Avatar answered Oct 22 '22 18:10

Eoghan Hynes