[Rd] sprintf error: "only 100 arguments allowed"

Hervé Pagès hpages at fredhutch.org
Tue Aug 25 03:44:08 CEST 2015


Hi Martin,

Don't know the reason for this limitation. Here is a possible workaound:

## Work around the "only 100 arguments are allowed" error
## in base::sprintf(). Only works with 'fmt' of length 1.
sprintf2 <- function(fmt, ...)
{
     MAX_NVAL <- 99L
     args <- list(...)
     if (length(args) <= MAX_NVAL)
         return(sprintf(fmt, ...))
     stopifnot(length(fmt) == 1L)
     not_a_spec_at <- gregexpr("%%", fmt, fixed=TRUE)[[1L]]
     not_a_spec_at <- c(not_a_spec_at, not_a_spec_at + 1L)
     spec_at <- setdiff(gregexpr("%", fmt, fixed=TRUE)[[1L]], not_a_spec_at)
     nspec <- length(spec_at)
     if (length(args) < nspec)
         stop("too few arguments")
     if (nspec <= MAX_NVAL) {
         break_points <- integer(0)
     } else {
         break_points <- seq(MAX_NVAL + 1L, nspec, by=MAX_NVAL)
     }
     break_from <- c(1L, break_points)
     break_to <- c(break_points - 1L, nspec)
     fmt_break_at <- spec_at[break_points]
     fmt_chunks <- substr(rep.int(fmt, length(fmt_break_at) + 1L),
                          c(1L, fmt_break_at),
                          c(fmt_break_at - 1L, nchar(fmt)))
     ans_chunks <- mapply(
         function(fmt_chunk, from, to)
             do.call(sprintf, c(list(fmt_chunk), args[from:to])),
         fmt_chunks,
         break_from,
         break_to
     )
     paste(ans_chunks, collapse="")
}

Then:

 > fmt <- paste(rep(" %d", 250), collapse="")
 > args <- as.list(1:300)
 > do.call(sprintf2, c(list(fmt), args))
[1] " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 
242 243 244 245 246 247 248 249 250"

Cheers,
H.


On 08/21/2015 08:21 PM, Martin Bel wrote:
> I'm trying to apply a function defined in the VW R docs, that attemps to
> convert a data.table object to Vowpal Wabbit format. In the process i'm
> getting the error in printf mentioned in the subject.
> The original function is here:
> https://github.com/JohnLangford/vowpal_wabbit/blob/master/R/dt2vw.R
>
>
> Below there is a small example that reproduces the error. The function
> works great with smaller (less than 100 columns), as shown.
>
> Any suggestions of alternative methods to acomplish the same would be
> great, but I'm basically asking, is there a reason for the 100 arguments
> limitation?
>
>
> # Source this function#
> https://github.com/JohnLangford/vowpal_wabbit/blob/master/R/dt2vw.R
> # Here is a gist
> source('https://gist.githubusercontent.com/martinbel/a013c2bd2333b85eeb76/raw/7b68ede0385f3d8684a07854e8d6ca527b9cd441/dt2vw.R')
>
> library(data.table)
>
> X = cbind(matrix(rnorm(1:1010), nrow=10, ncol=101), sample(c(1, -1), 10, T))
> X = as.data.table(X)
> X[, weight:=ifelse(V102 == 1, 3, 1)]
> # check out function arguments
> head(dt2vw, 2)# function (data, fileName, namespaces = NULL, target,
> weight = NULL, #                 tag = NULL, hard_parse = F)
>
> namespaces = list(a = list(varName = sprintf('V%s',1:50), keepSpace=F),
>                    b = list(varName = sprintf('V%s',51:101), keepSpace=F))
>
> dt2vw(data=X, fileName='../out.vw', namespaces = NULL,
>        target='V102', weight = 'weight',
>        tag = NULL, hard_parse = F)
> ### error
> # [1] "sprintf('%f %f |A V1:%f V2:%f V3:%f V4:%f V5:%f V6:%f V7:%f
> V8:%f V9:%f V10:%f V11:%f V12:%f V13:%f V14:%f V15:%f V16:%f V17:%f
> V18:%f V19:%f V20:%f V21:%f V22:%f V23:%f V24:%f V25:%f V26:%f V27:%f
> V28:%f V29:%f V30:%f V31:%f V32:%f V33:%f V34:%f V35:%f V36:%f V37:%f
> V38:%f V39:%f V40:%f V41:%f V42:%f V43:%f V44:%f V45:%f V46:%f V47:%f
> V48:%f V49:%f V50:%f V51:%f V52:%f V53:%f V54:%f V55:%f V56:%f V57:%f
> V58:%f V59:%f V60:%f V61:%f V62:%f V63:%f V64:%f V65:%f V66:%f V67:%f
> V68:%f V69:%f V70:%f V71:%f V72:%f V73:%f V74:%f V75:%f V76:%f V77:%f
> V78:%f V79:%f V80:%f V81:%f V82:%f V83:%f V84:%f V85:%f V86:%f V87:%f
> V88:%f V89:%f V90:%f V91:%f V92:%f V93:%f V94:%f V95:%f V96:%f V97:%f
> V98:%f V99:%f V100:%f V101:%f ',V102, weight, V1, V2, V3, V4, V5, V6,
> V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20,
> V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34,
> V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48,
> V49, V50, V51, V52, V53, V54, V55, V56, V57, V58, V59, V60, V61, V62,
> V63, V64, V65, V66, V67, V68, V69, V70, V71, V72, V73, V74, V75, V76,
> V77, V78, V79, V80, V81, V82, V83, V84, V85, V86, V87, V88, V89, V90,
> V91, V92, V93, V94, V95, V96, V97, V98, V99, V100, V101)"
> # Error in sprintf("%f %f |A V1:%f V2:%f V3:%f V4:%f V5:%f V6:%f V7:%f
> V8:%f V9:%f V10:%f V11:%f V12:%f V13:%f V14:%f V15:%f V16:%f V17:%f
> V18:%f V19:%f V20:%f V21:%f V22:%f V23:%f V24:%f V25:%f V26:%f V27:%f
> V28:%f V29:%f V30:%f V31:%f V32:%f V33:%f V34:%f V35:%f V36:%f V37:%f
> V38:%f V39:%f V40:%f V41:%f V42:%f V43:%f V44:%f V45:%f V46:%f V47:%f
> V48:%f V49:%f V50:%f V51:%f V52:%f V53:%f V54:%f V55:%f V56:%f V57:%f
> V58:%f V59:%f V60:%f V61:%f V62:%f V63:%f V64:%f V65:%f V66:%f V67:%f
> V68:%f V69:%f V70:%f V71:%f V72:%f V73:%f V74:%f V75:%f V76:%f V77:%f
> V78:%f V79:%f V80:%f V81:%f V82:%f V83:%f V84:%f V85:%f V86:%f V87:%f
> V88:%f V89:%f V90:%f V91:%f V92:%f V93:%f V94:%f V95:%f V96:%f V97:%f
> V98:%f V99:%f V100:%f V101:%f ",  : # only 100 arguments are allowed
> # works
> dt2vw(data=X[, 50:dim(X)[2], with=FALSE],
>        fileName='../out.vw', namespaces = NULL,
>        target='V102', weight = 'weight',
>        tag = NULL, hard_parse = F)
>
> system('head -3 ../out.vw')
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fredhutch.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-devel mailing list