简体   繁体   中英

Get package name when installing from source

I know you can install a .tar.gz or .zip from source using the following.

install.packages(SOURCE_FILE, repos = NULL, type="source")

I want to be able to determine the name of the package that's being installed. So for instance we could download this amazing package: https://github.com/Dasonk/findPackage/tarball/master?download .

This would give us a .tar.gz called Dasonk-findPackage-61907b1.tar.gz . We could actually rename it to beep.tar.gz and still install as:

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

How can I get the actual package's name that was installed. I thought I could use capture.output but that does not seem to work either. So after using install.packages I'd like to know "findPackage" was just installed.

I hate this but it's all I could come up with:

package_name <- function(package) {
    temp <- tempdir()
    untar(package, exdir = temp)
    out <- c(read.dcf(list.files(temp, pattern="DESCRIPTION", 
        recursive=TRUE, full.names=TRUE), "Package"))
    unlink(temp, recursive = TRUE, force = FALSE)
    out
}

package_name("beep.tar.gz")

If you're willing to install the package, then I think the easiest way seems like just getting the list of installed packages before and after:

# Grab previously installed packages
start.packages <- installed.packages()[,1]

# Install your new package
install.packages("beep.tar.gz", repos=NULL, type="source")

# Find all new packages
setdiff(installed.packages()[,1], start.packages)

Obvious downsides are that you need to install the new package, and that you'll also get all the newly installed dependencies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM