replace - R sprintf maximal input length -
r's sprintf
has preset maximal input length set 8192 bytes:
sprintf(x, y) error in sprintf(x, y) : 'fmt' length exceeds maximal format length 8192
what workaround longer input strings? learned in c there snprintf function such cases, seems not available in r, isn't it?
example
the input file like
select * some_table = '%s' , b = %d , c in (%s) , d = %s , e >= %0.2f
but much more complicated , much longer. need replace values values. sprintf
great 1 of input scripts working long. need multiple different arguments replaced , arguments have different formats (string, double, float). values replaced located in different parts of input file need search , replace them in given order. know this approach parametrized queries, need use different library (rpostgresql
).
i wrote little function cuts fmt
argument smaller pieces, looks variables associated %...
expressions, , calls sprintf
each of these pieces. match.call
used arguments hidden in ...
. sprintf
called via do.call
, because priori don't know of further arguments in ...
belong current piece of fmt
. function not @ perfect. example far fmt
cut pieces of fixed length. in general doesn't work because have keep %...
expressions intact. see if works, left result list of strings.
f <- function(n, fmt, ...) { cl <- as.list(match.call()) n <- nchar(fmt) p <- which(unlist(strsplit(fmt,""))=="%") result <- list() ( in 0:(n%/%n)) { start <- i*n+1 end <- min((i+1)*n,n) fm <- substr(fmt,start,end) k <- which(p %in% (start:end)) v <<- c(list(fm),cl[k+3]) result[[i+1]] <- do.call("sprintf", v ) } return(result) }
a small example using pieces of length 10:
> f( 10, "xyz: %i -ää %s 3 %i %f );", 3, "+++", 12, 0.123 ) [[1]] [1] "xyz: 3 -ä" [[2]] [1] "ä +++ 3 12 " [[3]] [1] "0.123000 );" > paste(f( 10, "xyz: %i -ää %s 3 %i %f );", 3, "+++", 12, 0.123 ),collapse="") [1] "xyz: 3 -ää +++ 3 12 0.123000 );"
perhaps 1 can use regular expressions detect %...
's , find right cutting points.
Comments
Post a Comment