Re: [Rd] full copy on assignment?
Thanks, Martin and Duncan, for the quick, cleary replies. Norm __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] full copy on assignment?
In particular, Duncan's comment applies in situations where the replacement is in a loop, obviously the case one worries about. What happens in the stupid little function: > foo <- function(x) { for(i in seq_along(x)) x[i] <- x[i] +1; x} for the case > y <- 1:1e6 > y1 <- foo(y) How often does y get duplicated? Hopefully not a million times. One can look at this in gdb, by trapping calls to duplicate1. The answer is: just once, to ensure that the object is local. Then the duplicated version has only one reference and the primitive replacement doesn't copy it. Unfortunately, as Duncan said, changing the definition to a user-written replacement function: > "sub<-" <- function(x,i, value){x[i]<- value; x} > foo <- function(x) { for(i in seq_along(x)) sub(x,i) <- x[i]+1; x} does duplicate a million times, since every call to `sub<-` gets an argument with two references. John On 4/3/10 4:42 PM, Duncan Murdoch wrote: On 03/04/2010 6:34 PM, Norm Matloff wrote: Here's a basic question that doesn't seem to be completely answered in the docs, and which unfortunately I've not had time to figure out by wading through the R source code: In a vector (or array) element assignment such as z[3] <- 8 is there in actuality a full rewriting of the entire vector pointed to by z, as implied by z <- "[<-"(z,3,value=8) Assume that an element of z has already being changed previously, so that copy-on-change issues don't apply, with z being reassigned back to the same memory address. I seem to recall reading somewhere that recent R versions make some attempt to avoid rewriting the entire vector, and my timing experiments seem to suggest that it's true. So, is a full rewrite avoided? And where in the source code is this done? It depends. User-written assignment functions can't avoid the copy. They act like the expansion z <- "[<-"(z,3,value=8) and in that, R can't tell that the newly created result of "[<-"(z,3,value=8) will later overwrite z. However, if z is a regular vector without a class and you're using the built-in version of z[3] <- 8, it can take some shortcuts. This happens in multiple places; one is around line 488 of subassign.c another is around line 1336. In each of these places copies are made in some circumstances, but not in general. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] full copy on assignment?
On 03/04/2010 6:34 PM, Norm Matloff wrote: Here's a basic question that doesn't seem to be completely answered in the docs, and which unfortunately I've not had time to figure out by wading through the R source code: In a vector (or array) element assignment such as z[3] <- 8 is there in actuality a full rewriting of the entire vector pointed to by z, as implied by z <- "[<-"(z,3,value=8) Assume that an element of z has already being changed previously, so that copy-on-change issues don't apply, with z being reassigned back to the same memory address. I seem to recall reading somewhere that recent R versions make some attempt to avoid rewriting the entire vector, and my timing experiments seem to suggest that it's true. So, is a full rewrite avoided? And where in the source code is this done? It depends. User-written assignment functions can't avoid the copy. They act like the expansion z <- "[<-"(z,3,value=8) and in that, R can't tell that the newly created result of "[<-"(z,3,value=8) will later overwrite z. However, if z is a regular vector without a class and you're using the built-in version of z[3] <- 8, it can take some shortcuts. This happens in multiple places; one is around line 488 of subassign.c another is around line 1336. In each of these places copies are made in some circumstances, but not in general. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] full copy on assignment?
Here's a basic question that doesn't seem to be completely answered in the docs, and which unfortunately I've not had time to figure out by wading through the R source code: In a vector (or array) element assignment such as z[3] <- 8 is there in actuality a full rewriting of the entire vector pointed to by z, as implied by z <- "[<-"(z,3,value=8) Assume that an element of z has already being changed previously, so that copy-on-change issues don't apply, with z being reassigned back to the same memory address. I seem to recall reading somewhere that recent R versions make some attempt to avoid rewriting the entire vector, and my timing experiments seem to suggest that it's true. So, is a full rewrite avoided? And where in the source code is this done? Thanks. Norm Matloff __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] inject html code into Rd file
Sounds like a good idea. The RCurl package can also do the base64 encoding (depends on libcurl), e.g. library(RCurl) img <- function() { tf <- tempfile() tf.out <- tempfile() png(tf, width = 500, height = 500) plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2) dev.off() img <- readBin(tf, "raw", file.info(tf)[1, "size"]) b64 <- base64Encode(img, "character") sprintf("", b64) } writeLines(img(), "test.html") I saw your blog post today about your base64 package. My concern is IE (<=7) does not support data uri... Regards, Yihui -- Yihui Xie Phone: 515-294-6609 Web: http://yihui.name Department of Statistics, Iowa State University 3211 Snedecor Hall, Ames, IA On Sat, Apr 3, 2010 at 3:11 AM, Romain Francois wrote: > Le 03/04/10 02:04, Duncan Murdoch a écrit : >> >> On 02/04/2010 8:06 AM, Duncan Murdoch wrote: >>> >>> On 02/04/2010 7:13 AM, Romain Francois wrote: Le 02/04/10 13:07, Duncan Murdoch a écrit : > > On 02/04/2010 6:17 AM, Romain Francois wrote: >> >> Hello, >> >> I'm trying to inject html code into an Rd file. For example : >> >> \name{test} >> \alias{test} >> \title{test} >> \description{ >> \if{html}{ >> \Sexpr[stage=render,results=text,echo=FALSE]{ >> "hello" >> } >> } >> } >> >> when this file is rendered, instead of having "hello" in bold, I get >> hello, i.e. characters < and > are replaced with html entities >> : < and > >> >> Is there a way to turn this off ? > > Yes, if you wrap it in \out{}. The example in the manual is > > \if{latex}{\out{\alpha}}\ifelse{html}{\out{α}}{alpha} > > Duncan Murdoch yes, I saw that in WRE, I should have been more specific. what if instead of a trivial string like "hello" the text is to be computed by some function. For example: print( xtable( iris), type = "html" ) >>> >>> I think this should do it: >>> >>> \Sexpr[stage=render,results=rd,echo=FALSE]{ >>> paste("\\out{", "hello, "}", sep="")} >>> >>> but this stuff hasn't been tested much, so there might be problems... >> >> One problem is that the backslashes need to be escaped twice, so you'd >> want >> >> \Sexpr[stage=render,results=rd,echo=FALSE]{ >> paste("out{", "hello, "}", sep="")} >> >> and you'd probably want it wrapped in \if or \ifelse so that it doesn't >> show up in text or latex output: >> >> \Sexpr[stage=render,results=rd, >> echo=FALSE]{"if{html}{out{hello}}"} >> >> Duncan Murdoch > > Thanks. > This gives one way to include images in a Rd file with data uri, here is a > proof of concept (that depends on openssl to do the base 64 encoding): > > img <- function(){ > tf <- tempfile() > tf.out <- tempfile() > png( tf, width = 500, height = 500) > plot( 1:100, rnorm(100), pch = 21, bg = "red", cex =2 ) > dev.off() > system( sprintf( 'openssl base64 -in "%s" -out "%s" ', tf, tf.out ) ) > sprintf( '\\out{}', > paste( readLines( tf.out), collapse = "\n" ) ) > } > > and the Rd file: > > \name{test} > \alias{test} > \title{test} > \description{ > \if{html}{ > \Sexpr[stage=render,results=rd,echo=FALSE]{ > source( "test.R" ) > img() > } > } > } > > > > It might be interesting to have something like results=asis or something. > > Romain > > -- > Romain Francois > Professional R Enthusiast > +33(0) 6 28 91 30 30 > http://romainfrancois.blog.free.fr > |- http://tr.im/OIXN : raster images and RImageJ > |- http://tr.im/OcQe : Rcpp 0.7.7 > `- http://tr.im/O1wO : highlight 0.1-5 > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] inject html code into Rd file
Le 03/04/10 02:04, Duncan Murdoch a écrit : On 02/04/2010 8:06 AM, Duncan Murdoch wrote: On 02/04/2010 7:13 AM, Romain Francois wrote: Le 02/04/10 13:07, Duncan Murdoch a écrit : On 02/04/2010 6:17 AM, Romain Francois wrote: Hello, I'm trying to inject html code into an Rd file. For example : \name{test} \alias{test} \title{test} \description{ \if{html}{ \Sexpr[stage=render,results=text,echo=FALSE]{ "hello" } } } when this file is rendered, instead of having "hello" in bold, I get hello, i.e. characters < and > are replaced with html entities : < and > Is there a way to turn this off ? Yes, if you wrap it in \out{}. The example in the manual is \if{latex}{\out{\alpha}}\ifelse{html}{\out{α}}{alpha} Duncan Murdoch yes, I saw that in WRE, I should have been more specific. what if instead of a trivial string like "hello" the text is to be computed by some function. For example: print( xtable( iris), type = "html" ) I think this should do it: \Sexpr[stage=render,results=rd,echo=FALSE]{ paste("\\out{", "hello, "}", sep="")} but this stuff hasn't been tested much, so there might be problems... One problem is that the backslashes need to be escaped twice, so you'd want \Sexpr[stage=render,results=rd,echo=FALSE]{ paste("out{", "hello, "}", sep="")} and you'd probably want it wrapped in \if or \ifelse so that it doesn't show up in text or latex output: \Sexpr[stage=render,results=rd, echo=FALSE]{"if{html}{out{hello}}"} Duncan Murdoch Thanks. This gives one way to include images in a Rd file with data uri, here is a proof of concept (that depends on openssl to do the base 64 encoding): img <- function(){ tf <- tempfile() tf.out <- tempfile() png( tf, width = 500, height = 500) plot( 1:100, rnorm(100), pch = 21, bg = "red", cex =2 ) dev.off() system( sprintf( 'openssl base64 -in "%s" -out "%s" ', tf, tf.out ) ) sprintf( '\\out{}', paste( readLines( tf.out), collapse = "\n" ) ) } and the Rd file: \name{test} \alias{test} \title{test} \description{ \if{html}{ \Sexpr[stage=render,results=rd,echo=FALSE]{ source( "test.R" ) img() } } } It might be interesting to have something like results=asis or something. Romain -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/OIXN : raster images and RImageJ |- http://tr.im/OcQe : Rcpp 0.7.7 `- http://tr.im/O1wO : highlight 0.1-5 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel