Skip to content

Instantly share code, notes, and snippets.

@dpastoor
Created February 24, 2015 18:15
Show Gist options
  • Select an option

  • Save dpastoor/c4c30d49c06d5acd39ef to your computer and use it in GitHub Desktop.

Select an option

Save dpastoor/c4c30d49c06d5acd39ef to your computer and use it in GitHub Desktop.
r to json`
#'@title
#'Data To JSON
#'
#'@description
#'This function convert data to JSON format
#'
#'@details
#'This function converts data to JSON format that can be used in Shiny and Highcharts
#'
#'@param data data.frame that contains data
#'@param col_to_json columns that should be converted to JSON
#'@param col_names names of the columns
#'
#'@examples
#'data <- data.frame(one = runif(5), two = runif(5))
#'data <- to_json(data, col_to_json=c('one', 'two'))
#'
#'@export
data_to_json <- function(data, col_to_json=NULL, col_names=NULL) {
if(!is.null(col_to_json)) {
if(length(col_to_json)==1) {
data <- data[col_to_json]
} else {
data <- data[,col_to_json]
}
}
lapply(1:nrow(data), function(i) {
res <- as.list(data[i,])
names(res) <- col_names
return(res)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment