Created
September 19, 2014 18:41
-
-
Save rundel/97ab111b9cc8feb6ea91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
truncate_prettify = function(s, n = 20) | |
{ | |
# s - json character string | |
# n - number of lines | |
library(stringr) | |
stopifnot(length(s)==1) | |
stopifnot(is.character(s)) | |
# find carraige returns | |
pos = str_locate_all(s,"\n")[[1]] | |
# | |
n = min(c(nrow(pos),n)) | |
# truncate prettify string | |
new_s = str_sub(s,1,pos[n, 2]+1) | |
# avoid ellipsis if not truncating | |
if (n != nrow(pos)) | |
new_s = paste0(new_s, "\n...\n\n") | |
return(new_s) | |
} | |
# Usage examples: | |
library(rjson) | |
library(jsonlite) | |
l = list(a = list(1,2,3:4),b = TRUE, c="ABC") | |
s = prettify(rjson::toJSON(l)) | |
cat(truncate_prettify(s,10)) | |
cat(truncate_prettify(s,11)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment