R/lyt2tbl.R

Defines functions lyt2tbl

lyt2tbl <- function(res, rclass) {
  lst <- jsonlite::fromJSON(txt = res)
  datJSON <- jsonlite::toJSON(x = lst$result$data)
  df <- jsonlite::fromJSON(txt = datJSON)
  # split off additionalInfo from df
  dfAddInf <- df[["additionalInfo"]]
  df[["additionalInfo"]] <- NULL
  if (length(dfAddInf) == 0) {
    df <- df
  } else {
    dfAddInf <- as.data.frame(lapply(X = dfAddInf,
                                     FUN = function(x) {
                                       if (class(x) == "list") {
                                         x <- sapply(X = x ,
                                                     FUN = paste0,
                                                     collapse = "; ")
                                       } else {
                                         x <- x
                                       }
                                     }),
                              stringsAsFactors = FALSE)
    df <- cbind(df, dfAddInf)
  }
  if (rclass == "tibble") {
    df <- tibble::as_tibble(x = df)
  }
  return(df)
}
CIP-RIU/brapi documentation built on Oct. 17, 2019, 2 p.m.