Hi all, Because str uses the generic version of length and names, it's currently very easy to create objects that break str: a <- structure(list(1:5), class = "a") length.a <- function(x) 2L str(a) b <- structure(list(1:5), class = "b") names.b <- function(x) character() str(b) These are obvious toy examples, but it is a real problem if you want to create a class that defines names or length in a meaningful way, that is incompatible with the underlying data structure. Hadley -- Chief Scientist, RStudio http://had.co.nz/
On 13-06-19 4:44 PM, Hadley Wickham wrote:> Hi all, > > Because str uses the generic version of length and names, it's > currently very easy to create objects that break str: > > a <- structure(list(1:5), class = "a") > length.a <- function(x) 2L > > str(a) > > b <- structure(list(1:5), class = "b") > names.b <- function(x) character() > > str(b) > > > These are obvious toy examples, but it is a real problem if you want > to create a class that defines names or length in a meaningful way, > that is incompatible with the underlying data structure.There are a few problems in the package; I'm not sure if all of them have any effect, but it works if I fix them all: - you have both inst/doc and vignettes directories. You should only have one of those. - Your vignette is named simple.rmd. The list_files_with_type() function isn't looking for the .rmd extension; it only recognizes .Rmd. That function is used for vignette() and browseVignettes(), but I think not for the HTML page you get to from help.start(). It should probably be fixed to work with the optional engines, which specify their own patterns. I'll put it on the list of things to fix. Duncan
>>>>> Hadley Wickham <h.wickham at gmail.com> >>>>> on Wed, 19 Jun 2013 15:44:05 -0500 writes:> Hi all, Because str uses the generic version of length and > names, it's currently very easy to create objects that > break str: > a <- structure(list(1:5), class = "a") > length.a <- function(x) 2L > str(a) > b <- structure(list(1:5), class = "b") > names.b <-function(x) character() > str(b) > These are obvious toy examples, but it is a real problem > if you want to create a class that defines names or length > in a meaningful way, that is incompatible with the > underlying data structure. Yes indeed, (and "well know"). I'm arguing that in such a situation, i.e., where you explicitly let length() --- or `[` --- behave incompatibly with respect to the underlying data structure, you have to define your own str() S3 method. I/we have done so ourselves in quite a few cases; typically this is a 1- or 2-liner, possibly calling utils:::str.default(). I do actually think we should *un*hide str.default() for such situations. In such a case (and not only here), using an S4 class instead of an S3 is much more natural IMO. But of course, we can discuss the issue here; that's exactly what I think R-devel is for. With regards (also to the Rstudio team), Martin Maechler, ETH Zurich