[R] Output to connections

Henrik Bengtsson hb at maths.lth.se
Tue May 20 13:03:31 CEST 2003


In R '\' has to be escaped, i.e. '\\' which means '\\' has to be '\\\\'
(this was probably there before the help page was generated!?)

The following works

  ## convert decimal point to comma in output, using a pipe (Unix)
  zz <- pipe(paste("sed s/\\\\./,/ >", "outfile"), "w")
  cat(format(round(rnorm(100), 4)), sep = "\n", file = zz)
  close(zz)
  ## now look at the output file:
  file.show("outfile", delete.file = TRUE)

Henrik Bengtsson

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of 
> Adelchi Azzalini
> Sent: den 20 maj 2003 12:44
> To: r-help at stat.math.ethz.ch
> Subject: [R] Output to connections
> 
> 
> 
> In the document "R Data Import/Export", section "Output to 
> connections", there is the following portion of code:
> 
>   ## convert decimal point to comma in output, using a pipe (Unix)
>   zz <- pipe(paste("sed s/\\./,/ >", "outfile"), "w")
>   cat(format(round(rnorm(100), 4)), sep = "\n", file = zz)
>   close(zz)
>   ## now look at the output file:
>   file.show(outfile, delete.file = TRUE)
> 
> Surely the last fine must be 
>   file.show("outfile", delete.file = TRUE)
> However this is not the problem, but the fact that I get 
> something like ,1.6861 ,0.1934 ,0.5640 ,0.5741 ,0.1920 
> ,0.1898 ,1.4788 ,0.1706 ,0.9953 <..snipped..>
> 
> If I run from R:
>   zz <- file("outfile", "w")
>   cat(format(round(rnorm(10), 4)), sep = "\n", file = zz)
>   close(zz)
> and then from outside R:
>   sed s/\\./,/ outfile 
> then I get it right (of course), something like
> -1,3612
> -0,9772
>  0,1524
>  2,4046
>  0,4741
>  0,6659
> -0,8277
>  0,5071
>  0,7190
>  0,4088
> 
> This is fine, but it would be good to have it working in first form.
>  
> environment: R 1.7.0 on Debian linux.
> 
> regards,
> 
> Adelchi Azzalini
> 
> -- 
> Adelchi Azzalini  <azzalini at stat.unipd.it>
> Dipart.Scienze Statistiche, Università di Padova, Italia 
http://azzalini.stat.unipd.it/

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list