3

I'm creating an active binding in an R6 class to print a private element.

When I'm calling print(private$privMail), the result is printed twice but only on private elements, not on public.

If I'm not using print, no problem.

Here's a reprex :

team <- R6::R6Class("Team",
                public = list( mail = "colin@thinkr.fr"),
                private = list(privMail = "colin@thinkr.fr"), 
                active = list(
                  MailPriv = function(){ print(private$privMail) },
                  Mail = function(){ print(self$mail) }
                )
)
a <- team$new()
a$MailPriv
#> [1] "colin@thinkr.fr"
#> [1] "colin@thinkr.fr"
a$Mail
#> [1] "colin@thinkr.fr"
#> [1] "colin@thinkr.fr"
#Without using print 
team2 <- R6::R6Class("Team",
                    public = list( mail = "colin@thinkr.fr"),
                    private = list(privMail = "colin@thinkr.fr"), 
                    active = list(
                      MailPriv = function(){ private$privMail },
                      Mail = function(){self$mail }
                    )
)
abis <- team2$new()
abis$MailPriv
#> [1] "colin@thinkr.fr"
abis$Mail
#> [1] "colin@thinkr.fr"


devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.4 (2018-03-15)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  fr_FR.UTF-8                 
#>  tz       Europe/Paris                
#>  date     2018-05-24
#> Packages -----------------------------------------------------------------
#>  package   * version date       source                          
#>  backports   1.1.2   2017-12-13 CRAN (R 3.4.3)                  
#>  base      * 3.4.4   2018-03-15 local                           
#>  compiler    3.4.4   2018-03-15 local                           
#>  datasets  * 3.4.4   2018-03-15 local                           
#>  devtools    1.13.5  2018-02-18 CRAN (R 3.4.3)                  
#>  digest      0.6.15  2018-01-28 CRAN (R 3.4.3)                  
#>  evaluate    0.10.1  2017-06-24 CRAN (R 3.4.1)                  
#>  graphics  * 3.4.4   2018-03-15 local                           
#>  grDevices * 3.4.4   2018-03-15 local                           
#>  htmltools   0.3.6   2017-04-28 CRAN (R 3.4.0)                  
#>  knitr       1.20    2018-02-20 CRAN (R 3.4.3)                  
#>  magrittr    1.5     2014-11-22 CRAN (R 3.4.0)                  
#>  memoise     1.1.0   2017-04-21 CRAN (R 3.4.0)                  
#>  methods   * 3.4.4   2018-03-15 local                           
#>  R6          2.2.2   2017-06-17 CRAN (R 3.4.0)                  
#>  Rcpp        0.12.16 2018-03-13 CRAN (R 3.4.4)                  
#>  rmarkdown   1.9     2018-03-01 CRAN (R 3.4.3)                  
#>  rprojroot   1.3-2   2018-01-03 CRAN (R 3.4.3)                  
#>  stats     * 3.4.4   2018-03-15 local                           
#>  stringi     1.1.7   2018-03-12 CRAN (R 3.4.4)                  
#>  stringr     1.3.0   2018-02-19 CRAN (R 3.4.3)                  
#>  tools       3.4.4   2018-03-15 local                           
#>  utils     * 3.4.4   2018-03-15 local                           
#>  withr       2.1.2   2018-04-13 Github (jimhester/withr@79d7b0d)
#>  yaml        2.1.18  2018-03-08 CRAN (R 3.4.4)

Edit

To be precise, my question is not about how to solve this issue (I can do it by removing the print, as it is done in the second class), but about why using print() print twice the content.

Colin FAY
  • 4,849
  • 1
  • 12
  • 29

2 Answers2

2

print returns the value it printed, try this modified version out:

team <- R6::R6Class("Team",
                    public = list( mail = "colin@thinkr.fr"),
                    private = list(privMail = "colin@thinkr.fr"), 
                    active = list(
                        MailPriv = function(){ print(private$privMail); "hi!" },
                        Mail = function(){ print(self$mail) }
                    )
)
a <- team$new()
> a$MailPriv
[1] "colin@thinkr.fr"
[1] "hi!"

Apparently the active binding doesn't support invisible, so changing "hi!" to invisible() won't help.

Alexis
  • 4,950
  • 1
  • 18
  • 37
  • So it ignores the invisible(x). Thanks for your answer. I'm accepting Florian answer as he was the quickest. Thanks! – Colin FAY May 24 '18 at 09:04
1

I'm not sure I understood perfectly your question, but I think that you have one print from the print and one from the returned element of the print.

Try e.g. (print(1)).

So, basically, I would remove the print here.

F. Privé
  • 11,423
  • 2
  • 27
  • 78