GithubHelp home page GithubHelp logo

kirillseva / super Goto Github PK

View Code? Open in Web Editor NEW

This project forked from robertzk/super

0.0 1.0 0.0 105 KB

Giving R the ability to invoke "parent" methods, solving the namespace collision issue

R 100.00%

super's Introduction

Fix R namespace collision Build Status Coverage Status

If two packages are loaded which contain namespace collisions (i.e., export a function with the same name), R has no good strategy for providing the ability to call the overwritten function. This package aims to solve that problem by defining a super method which tries to find the next appropriate function to call. You should be familiar with how R looks for functions.

For example, consider the following scenario.

# Exported in some attached package.
source <- function(file, ...) {
  cat("Sourcing file ", sQuote(file), "\n")
  base::source(file, ...)
}

By explicitly calling base::source, this package has effectively monopolized its interception of the source function: if a newly loaded package were to take the same approach, the first package's work would be permanently undone. Imagine a second package were attached with the following overwrite.

source <- function(file, ...) {
  if (otherpackage::has_cached(file, ...)) { otherpackage::cache(file, ...) }
  else { base::source(file, ...) }
}

Ideally, instead of calling base::source directly, it would call the next source function in the attached search path.

To accomplish this, we can instead use super:

source <- function(file, ...) {
  if (otherpackage::has_cached(file, ...)) { otherpackage::cache(file, ...) }
  else { super::super(file, ...) }
}

By using super, base::source will be selected if no other package has overwritten source -- otherwise, the source exported by the nearest package in the search path will be selected.

Note: Obviously, the function that super selects needs to be commutative with the current function. If the attach order in the search path matters, there is no good solution as packages in general do not have control over their attachment order. In the above example, this is already somewhat apparent: if the function which caches the source call is called first, then "sourcing file" will only be printed once. On the other hand, if the first package is attached closer to the global environment, then "sourcing file" will be printed even on cache hits.

Installation

This package is in development and is not yet available on CRAN (as of February 28, 2015). To get the latest development build directly from Github, use the following code snippet.

if (!require("devtools")) install.packages("devtools")
devtools::install_github("robertzk/super")

Another example

Note that super merely looks up the parent environment chain of the calling frame. Thus, we can use it for local functions as well as attached packages.

function1 <- function() {
  print("Top-level")
  invisible(NULL)
}

local({
  function2 <- function() {
    function1 <- function() {
      print("Mid-level")
      super::super()
    }

    function3 <- function() {
      function1 <- function() {
        print("Low-level")
        super::super()
      }
      function1()
    }

    function3()
  }

  function2()
})
# Will print
# [1] "Low-level"
# [1] "Mid-level"
# [1] "Top-level"

super's People

Contributors

kirillseva avatar robertzk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.