Skip to content

Instantly share code, notes, and snippets.

@DavZim
Last active January 6, 2023 12:26
Show Gist options
  • Save DavZim/9b5e1705c726346f5a50cdec1f61509f to your computer and use it in GitHub Desktop.
Save DavZim/9b5e1705c726346f5a50cdec1f61509f to your computer and use it in GitHub Desktop.

First attempt using import version

setwd("~/projects/Github/")
packageVersion("import")
#> [1] ‘1.3.0.9004’

if (dir.exists("pkgs")) unlink("pkgs", recursive = TRUE)

dir.create("pkgs")
dir.create("pkgs/0.1.0")
dir.create("pkgs/0.2.0")

# Download Files from https://github.com/DavZim/testPkg
if (!file.exists("testPkg_0.1.0.tar.gz"))
  download.file("https://github.com/DavZim/testPkg/releases/download/v0.1.0/testPkg_0.1.0.tar.gz", "testPkg_0.1.0.tar.gz")
if (!file.exists("testPkg_0.2.0.tar.gz"))
  download.file("https://github.com/DavZim/testPkg/releases/download/v0.2.0/testPkg_0.2.0.tar.gz", "testPkg_0.2.0.tar.gz")

install.packages("testPkg_0.1.0.tar.gz", "pkgs/0.1.0", NULL)
install.packages("testPkg_0.2.0.tar.gz", "pkgs/0.2.0", NULL)

import::from("testPkg", .library = "pkgs/0.1.0", .into = "A", foo)
import::from("testPkg", .library = "pkgs/0.2.0", .into = "B", foo)

search()
#>  [1] ".GlobalEnv"        "B"                 "A"                
#>  [4] "package:stats"     "package:graphics"  "package:grDevices"
#>  [7] "package:utils"     "package:datasets"  "package:methods"  
#> [10] "Autoloads"         "tools:callr"       "package:base"
ls("A")
#> [1] "?"   "foo"
ls("B")
#> [1] "?"   "foo"

get("foo", "A")
#> function () 
#> {
#>     "Hello World from Version 0.1.0"
#> }
#> <bytecode: 0x000000002159b1b0>
#> <environment: namespace:testPkg>
get("foo", "B")
#> function () 
#> {
#>     "Hello World from Version 0.1.0"
#> }
#> <bytecode: 0x000000002159b1b0>
#> <environment: namespace:testPkg>

# NO GOOD should read 0.2.0 for the second one

Second attempt with libPaths changes

lb <- .libPaths()

.libPaths("pkgs/0.1.0")
import::from("testPkg", .library = "pkgs/0.1.0", .into = "A", foo)

.libPaths("pkgs/0.2.0")
import::from("testPkg", .library = "pkgs/0.2.0", .into = "B", foo)

.libPaths(lb)

get("foo", "A")
#> function () 
#> {
#>     "Hello World from Version 0.1.0"
#> }
#> <bytecode: 0x000000002159b1b0>
#> <environment: namespace:testPkg>
get("foo", "B")
#> function () 
#> {
#>     "Hello World from Version 0.1.0"
#> }
#> <bytecode: 0x000000002159b1b0>
#> <environment: namespace:testPkg>

# NO GOOD should read 0.2.0 for the second one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment