[R] sprintf does not print

(Ted Harding) ted.harding at wlandres.net
Wed Feb 16 11:56:42 CET 2011


To expand on Mario's response below:

sprintf() is a function which returns a value which
is a [vector of] character string[s]. The "print" in its
name is there because its name is the same as the C function
sprintf whose purpose is to create a character string ("s")
formatted ("f") according to the format argument. sprintf()
does not perform the operation of "printing" its value to
the screen -- never!

However, on may get the impression that it does so if one
uses it "stand-alone" in a command line:

  sprintf("%.4f",pi)
  # [1] "3.1416"

but this is because *anything* which has a value, when entered
stand-alone in a command line, will result in the value being
"printed" to the screen. It is R which does this, not the
"anything".

  X <- sprintf("%.4f",pi)  ## Note that this does not print either
  X
  # [1] "3.1416"

Inside a loop, or inside a function, you need to wrap the
"sprintf(...)" inside a cat() or print() statement; when
something which has a value is named in such a context, the
"printing to screen" is suppressed. It is R which suppresses it.

  for(i in (1:2)){
    X <- sprintf("%.4f",pi)
    X
  }

produces nothing on screen. However,

  for(i in (1:2)){
    X <- sprintf("%.4f",pi)
    print(X)
  }
  # [1] "3.1416"
  # [1] "3.1416"

See ?sprintf for more detail on its relation with the C function
of the same name.

Hoping this helps,
Ted.

On 16-Feb-11 10:25:31, Mario Valle wrote:
> Use
> cat(sprintf('I did the the %d,%d \n',k,l))
> The functions do not print in non interactive mode
> Hope it helps
>                                  mario
> On 16-Feb-11 11:15, Alaios wrote:
>> Dear all I have an sprintf inside a loop to track changes in
>> variable's value.
>>
>> This sprintf statement works if I copy and paste it inside R
>> sprintf('I did the the %d,%d \n',k,l)
>>
>> but when this is inside a loop I get no message.
>>
>> listcounter<-1
>> for (k in c(1:mmax)){ #
>>    for (l in c(1:nmax)){
>>
>>      lst[[listcounter]]<-fun(estimatedsr)
>>      listcounter<-listcounter+1
>>      sprintf('I did the the %d,%d \n',k,l)
>>    }
>> }
>>
>>
>> When I paste the code above I never get any message printed. I know
>> that the loop works. When I kill the execution of the loop the
>> listcounter variable has value different from the initial one.
>>
>> What might be the solution to that strange problem?
>>
>> Best Regards
>> Alex
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> -- 
> Ing. Mario Valle
> Data Analysis and Visualization Group            |
> http://www.cscs.ch/~mvalle
> Swiss National Supercomputing Centre (CSCS)      | Tel:  +41 (91)
> 610.82.60
> v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax:  +41 (91)
> 610.82.82
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 16-Feb-11                                       Time: 10:56:35
------------------------------ XFMail ------------------------------



More information about the R-help mailing list