Installing a package offline from GitHub

前端 未结 5 1906
[愿得一人]
[愿得一人] 2020-12-14 18:06

I\'m trying to port some packages to an R installation on an offline (Windows) computer.

From CRAN (let\'s say data.table), the process: 1) download .zip

相关标签:
5条回答
  • 2020-12-14 18:49

    You can use install_local from devtools to install a Github package with the zip you've downloaded, e.g. :

    library("devtools")
    install_local(path = "data.table-master.zip")
    

    But you'll have to install all dependancies first.

    0 讨论(0)
  • 2020-12-14 18:49

    The following function extracts a GitHub .zip file located at path to a temporary directory and installs the package straight from there, i.e. does not create an interim .tar.gz archive. The function tries to guess the package name but it can also be given through pkg. Temporary files are removed at exit.

    This function does not install any dependencies.

    install_zip <- function(path,
                            pkg = sub("(-[^-]+)?\\.[^.]+$", "", basename(path))) {
        dir1 <- tempfile()
        dir2 <- file.path(dir1, pkg)
        dir.create(dir2, recursive = TRUE)
        on.exit(unlink(dir1, recursive = TRUE, force = TRUE))
        suppressMessages(unzip(path, exdir = dir2,
                               unzip = getOption("unzip")))
        temp_contents <- dir(dir2)
        if (length(temp_contents) == 1L) {
            dir3 <- file.path(dir2, temp_contents)
            if (file.info(dir3, extra_cols=FALSE)[["isdir"]]) {
                dir2 <- file.path(dir2, pkg)
                file.rename(dir3, dir2)
            }
        }
        install.packages(dir2, repos = NULL, type = "source")
    }
    
    0 讨论(0)
  • 2020-12-14 18:51

    Not sure if this is a solution or a workaround. Given a zip file of the source of an R package directory structure:

    On a shell:

    ~$ unzip data.table-master.zip
    ## optional renaming
    ~$ mv data.table-master data.table
    ## create the new
    ~$ tar czf data.table.tar.gz data.table
    

    There are likely other tools that allow you to extract and re-archive them in a different format. Since I tend towards shell-level access and control, I tend towards these simple tools.

    In R:

    install.packages("data.table.tar.gz", type="source", repos=NULL)
    

    (This will not succeed unless all dependencies are already installed.)

    0 讨论(0)
  • 2020-12-14 19:01

    Let's assume that you have Rtools and devtools on the win machine.

    Step 1: Download the source zip.

    Step 2: Copy to the win machine and unzip the content there.

    Step 3: Run the following code (adjust the path as necessary):

    library(devtools)
    source <- devtools:::source_pkg("E:/temp/data.table-master")
    install(source)
    
    library(data.table)
    #loads 1.9.7
    
    0 讨论(0)
  • 2020-12-14 19:04

    So I've finally come up with a workaround that works in some cases... but it's admittedly a bit macgyvered. Would love to hear some feedback about how to make this approach work for any package.

    Since I have a package where all of the code is in one .R file, I spoofed a package environment for that package and sourced the functions into that environment. Suppose the package is all in test.R:

    #add an appropriately named package to the search() path
    attach(environment(), name = "package:pkg")
    
    #source the functions
    sys.source("test.R", envir = as.environment("package:pkg"))
    

    Voila. I don't know enough about how packages are loaded to say how to do this, for example, if the package has src code in C or something else.

    0 讨论(0)
提交回复
热议问题