Hello I tried to define replacement functions for the class "mylist". When I test them in an active R session, they work -- however, when I put them into a package, they don't. Why and how to fix? make_my_list <- function( x, y ) { return(structure(list(x, y, class="mylist"))) } mylist <- make_my_list(1:4, letters[3:7]) mylist mylist[['x']] <- 4:6 mylist "[[<-" <- function(x, field, value) { UseMethod('[[<-', x) } "[[<-.mylist" <- function(x, field, value) { stop( "Do not assign." ) } mylist[['x']] <- 1:10 mylist mylist$y <- LETTERS[1:3] mylist "$<-" <- function(x, field, value) { UseMethod('$<-', x) } "$<-.mylist" <- function(x, field, value) { stop( "Do not assign." ) } mylist$y <- LETTERS[10:15] Thanks for help S?ren
Did you export the S3 methods in the NAMESPACE? Hadley On Mon, May 4, 2015 at 7:52 AM, <soeren.vogel at posteo.ch> wrote:> Hello > > I tried to define replacement functions for the class "mylist". When I test them in an active R session, they work -- however, when I put them into a package, they don't. Why and how to fix? > > > make_my_list <- function( x, y ) { > return(structure(list(x, y, class="mylist"))) > } > mylist <- make_my_list(1:4, letters[3:7]) > mylist > mylist[['x']] <- 4:6 > mylist > "[[<-" <- function(x, field, value) { > UseMethod('[[<-', x) > } > "[[<-.mylist" <- function(x, field, value) { > stop( "Do not assign." ) > } > mylist[['x']] <- 1:10 > mylist > mylist$y <- LETTERS[1:3] > mylist > "$<-" <- function(x, field, value) { > UseMethod('$<-', x) > } > "$<-.mylist" <- function(x, field, value) { > stop( "Do not assign." ) > } > mylist$y <- LETTERS[10:15] > > > Thanks for help > S?ren > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- http://had.co.nz/
No. I fixed that, the NAMESPACE file now contains: S3method("[[<-", mylist) S3method("$<-", mylist) It still does not work. I also created a print method (print.mylist) which did work out of the box, regardless of being in the NAMESPACE file or not. Could it be somehow in here (also in my NAMESPACE file): exportPattern("^[[:alpha:]]+") Or could it be that the type of quoting causes the problems? Like in the NAMESPACE I used double quotes for function definition. Do I need single quotes or back-ticks? Or is there a package where I can look how others implemented this kind of functionality? S?ren> On 04.05.2015, at 16:17, Hadley Wickham <h.wickham at gmail.com> wrote: > > Did you export the S3 methods in the NAMESPACE? > > Hadley > > On Mon, May 4, 2015 at 7:52 AM, <soeren.vogel at posteo.ch> wrote: >> Hello >> >> I tried to define replacement functions for the class "mylist". When I test them in an active R session, they work -- however, when I put them into a package, they don't. Why and how to fix? >> >> >> make_my_list <- function( x, y ) { >> return(structure(list(x, y, class="mylist"))) >> } >> mylist <- make_my_list(1:4, letters[3:7]) >> mylist >> mylist[['x']] <- 4:6 >> mylist >> "[[<-" <- function(x, field, value) { >> UseMethod('[[<-', x) >> } >> "[[<-.mylist" <- function(x, field, value) { >> stop( "Do not assign." ) >> } >> mylist[['x']] <- 1:10 >> mylist >> mylist$y <- LETTERS[1:3] >> mylist >> "$<-" <- function(x, field, value) { >> UseMethod('$<-', x) >> } >> "$<-.mylist" <- function(x, field, value) { >> stop( "Do not assign." ) >> } >> mylist$y <- LETTERS[10:15] >> >> >> Thanks for help >> S?ren >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel > > > > -- > http://had.co.nz/
Seemingly Similar Threads
- Define replacement functions
- Define return values of a function
- [R-pkg-devel] Three-argument S3method declaration does not seem to affect dispatching from inside the package.
- Summary grouped by factor
- [R-pkg-devel] Three-argument S3method declaration does not seem to affect dispatching from inside the package.