tests/testthat/tt_package_tools.R

## What is useful?  salvagable?

#  PURPOSE:   create, remove of fake files USING tinytest::
#  TINYTEST::
if (F) {
file <- "../tests/testthat/tt_package_tools.R"

##
library(tinytest)
library(data.table)

{
  ## find packages which depend on this one
  jr_package_dependencies_reverse("tibble")
  jr_package_dependencies_reverse("data.table")
  jr_package_dependencies_reverse("magrittr")
}

{
## .libPaths() to find user and `common` library locations
# in my home directory, and
# in /usr/local/lib/R/library
.libPaths()
dt  <- as.data.table(.libPaths())
dt
}

{
## list packages in user library
# top level dir only (so not recursive)
pkgs  <- lapply(.libPaths(), list.dirs, recursive=FALSE)

# user - 288!
length(pkgs[[1]])

# common - 30  
length(pkgs[[2]])
}



{
## What repos are packages from?
options("repos")
}

{
### remove a package
  if (F) {
    pkg = 
  remove.packages(pkg)
  }
}

## Examine available.packages() (>17k)
{
if (F) versions  <- available.packages()

library(tibble)
t  <- as_tibble(versions)
t[,c(1,2,12:13,15:16)]
str(versions)
names(versions)
typeof(versions)
length(versions)
versions[[4]]
head(versions)
}

### installed.packages
{
installed  <- as_tibble(installed.packages())

# 6 x 16
head(installed) 
str(installed)

# 317
installed[,"NeedsCompilation" == "no"]

installed[,"NeedsCompilation" == "yes"]
}

## Where are we now?
#  * which dir are used? (for user pkgs, for common pkgs)
#  * status of pkgs in each dir
# packages that can be upgraded
  #
{
old.packages()

# x is list
x  <- packageStatus()
x
typeof(x)
str(x)
print(x)

# details
summary(x)
}

## BEGIN DT:  install.packages (using DT)

library(microbenchmark)

# 4.5 ms
microbenchmark( installed  <- tibble::as_tibble(installed.packages()))

dt  <- as.data.table(installed.packages()) 
str(dt)
head(dt)


# remove uneeded fields
# .()  is alias for list()
dt  <- dt[, .(Package, Version, Depends, Imports, LinkingTo, Suggests, Enhances,  OS_type, NeedsCompilation, Built)]
dt %>% head()

# count, group by version of Build ( = version)
dt[,.(.N)    , by=.(Built)]

# same
dt[, .N, by = .(Built)]   

## SAME
  dt[, .N, by = "OS_type"]  # all <NA>
  dt[, .N, by = .(OS_type)]  # all <NA>

## Compilation
  dt[, .N, by = .(NeedsCompilation)]   # 141 need!

## Suggests (hmmm, not normalized)
  dt[, .N, by = .(Suggests)][order(-N)] %>% head()






# create subset, needs compilation
x  <- dt[NeedsCompilation == "yes"]

### Seems to be repeat of information
{
  # list, not data.frame
  status  <- packageStatus()
  status
  s  <- as.data.table(status$inst)
  s
  s[, .N, by= .( Status)]
}

if (F) status$avail    # long list



## More advanced package managment:

## List all packages + description, by library location

## list all packages in lib.loc
   obj  <- library()

   class(obj)
# [1] "libraryIQR"


## packages currently attached
  (.packages())

## packages available
  (.packages(all.available  = TRUE))

## List Default Packages ?? Explain
getOption("defaultPackages")

## Basic installation
  *  sudo R CMD INSTALL *.tar.gz
  #install.packages("igraph", verbose=TRUE, dependencies=TRUE)


## workhorse
{
begin  <- Sys.time()
.libPaths()

update.packages(ask=FALSE )
end  <- Sys.time()
diff  <- end - begin
diff
}

## Troubleshoot:  install packages one at a time
{
begin  <- Sys.time()

# p  <- c("digest")
# p  <- c("future")
# p  <- c("httpuv")
# p  <- c("jsonlite")
# p  <- c("servr")
## p  <- c("lattice")
#p  <- c("utf8")
# p  <- c("rlang")
if (F) {
install.packages(p, verbose=TRUE, dependencies = TRUE)
}
# upgrade (p, ask = FALSE , checkBuild=TRUE)

end  <- Sys.time()
diff  <- end - begin
diff
}
# print updated summary table


### Pkg Dependencies
pkg = "purrr"
packageDescription(pkg)
# subset vector
packageDescription(pkg)[c("Imports", "Suggests")]


## sessioninfo::package_info()
# For Installed Packages, yields dependencies
  sessioninfo::package_info(pkg="tibble")

## currently loaded packages
  sessioninfo::package_info()


# ---- 004 R startup ----

# In this order, R seeks .Rprofile $R_HOME, $HOME, project directory
R.home() # /usr/lib/R
site_path = R.home(component = "home") 
site_path

# file.path constructs path to a file
fname = file.path(site_path, "etc", "Rprofile.site") 
fname

file.exists(fname) 


### if pkg is installed, it will have `DESCRIPTION` file
# returns char[] , logical for each path
does_pkg_exist  <- function(path = .libPaths(), pkg = NULL,
                            file_name = "DESCRIPTION")

  file.exists(file.path(.libPaths(), pkg=pkg, "DESCRIPTION"))

pkg = "jimTools"
pkg = "foolish"
x  <- does_pkg_exist(pkg = pkg)
x

}
jimrothstein/jimTools documentation built on Jan. 19, 2025, 3:23 p.m.