Skip to content

Commit

Permalink
Merge pull request #2544 from LueJian/master
Browse files Browse the repository at this point in the history
Update microservice.R for supporting jsonData input in R
  • Loading branch information
axsaucedo authored Oct 27, 2020
2 parents 155623d + a40ea0c commit 5b1b2b1
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions incubating/wrappers/s2i/R/microservice.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ parseQS <- function(qs){
v <- function(...) cat(sprintf(...), sep='', file=stdout())

validate_json <- function(jdf) {
if (!"data" %in% names(jdf)) {
return("data field is missing")
}
else if (!("ndarray" %in% names(jdf$data) || "tensor" %in% names(jdf$data)) ) {
return("data field must contain ndarray or tensor field")
}
else{
if ("data" %in% names(jdf)){
if (!("ndarray" %in% names(jdf$data) || "tensor" %in% names(jdf$data)) ){
return("data field must contain 'ndarray' or 'tensor' field")
} else {
return("OK")
}
} else if ("jsonData" %in% names(jdf)){
return("OK")
} else {
return("input must contain 'data' or 'jsonData' field")
}
}

Expand All @@ -64,14 +66,16 @@ validate_feedback <- function(jdf) {
{
return("reward field is missing")
}
else if (!"data" %in% names(jdf$request)) {
return("data request field is missing")
}
else if (!("ndarray" %in% names(jdf$request$data) || "tensor" %in% names(jdf$request$data)) ) {
return("data field must contain ndarray or tensor field")
}
else{
else if ("data" %in% names(jdf)){
if (!("ndarray" %in% names(jdf$data) || "tensor" %in% names(jdf$data)) ){
return("data field must contain 'ndarray' or 'tensor' field")
} else {
return("OK")
}
} else if ("jsonData" %in% names(jdf)){
return("OK")
} else {
return("input must contain 'data' or 'jsonData' field")
}
}

Expand All @@ -94,27 +98,40 @@ extract_names <- function(jdf) {
}

create_response <- function(req_df,res_df){
if ("ndarray" %in% names(req_df$data)){
templ <- '{"data":{"names":%s,"ndarray":%s}}'
names <- toJSON(colnames(res_df))
values <- toJSON(res_df, dataframe = "values", na = "null") # The "dataframe" argument is for data type persistence and "na" argument is for null value persistence
sprintf(templ,names,values)
} else {
templ <- '{"data":{"names":%s,"tensor":{"shape":%s,"values":%s}}}'
names <- toJSON(colnames(res_df))
values <- toJSON(c(res_df))
dims <- toJSON(dim(res_df))
sprintf(templ,names,dims,values)
if ("data" %in% names(req_df)){
if ("ndarray" %in% names(req_df$data)){
templ <- '{"data":{"names":%s,"ndarray":%s}}'
names <- toJSON(colnames(res_df))
values <- toJSON(res_df, dataframe = "values", na = "null") # The "dataframe" argument is for data type persistence and "na" argument is for null value persistence
sprintf(templ,names,values)
} else {
templ <- '{"data":{"names":%s,"tensor":{"shape":%s,"values":%s}}}'
names <- toJSON(colnames(res_df))
values <- toJSON(c(res_df))
dims <- toJSON(dim(res_df))
sprintf(templ,names,dims,values)
}
} else if ("jsonData" %in% names(req_df)){
templ <- '{"jsonData":{%s}}'
jdata <- toJSON(res_df, na = "null") # The "na" argument is for null value persistence
jdata <- substr(jdata, 3, nchar(jdata) - 2) # Remove {} and [] for jsonData format like {"jsonData":{"key1":value1, "key2":value2}}
sprintf(templ, jdata)
}
}

create_dataframe <- function(jdf) {
data = extract_data(jdf)
names = extract_names(jdf)
df <- data.frame(do.call(rbind, lapply(data, rbind))) # The step is to binding the output from fromJSON(json, simplifyVector = F) in endpoints
df[df == "NULL"] <- NA # Replace NULL value by NA if input value contain null
df <- data.frame(lapply(df, unlist), stringsAsFactors = F) # unlist all columns because columns are list structure
colnames(df) <- names
if("data" %in% names(jdf)){
data = extract_data(jdf)
names = extract_names(jdf)
df <- data.frame(do.call(rbind, lapply(data, rbind))) # The step is to binding the output from fromJSON(json, simplifyVector = F) in endpoints
df[df == "NULL"] <- NA # Replace NULL value by NA if input value contain null
df <- data.frame(lapply(df, unlist), stringsAsFactors = F) # unlist all columns because columns are list structure
colnames(df) <- names
}else if("jsonData" %in% names(jdf)){
ls <- jdf$jsonData
ls[names(ls)[unlist(lapply(ls, is.null))]] <- NA # Replace NULL value by NA if input value contain null
df <- data.frame(ls, stringsAsFactors = F)
}
df
}

Expand Down

0 comments on commit 5b1b2b1

Please sign in to comment.