pacman
pacman copied to clipboard
Suggested check for pacman install
Someone who uses pacman's p_load
in a script is doing so for longer terms sustainability (i.e., they can just run the script). This of course requires someone has pacman installed. I have two possible means we could suggest doing this for people (the line before using p_load
in a long term script). I think we'll want to be consistent in our uses online (TalkStats, StackOverflow, blogging, etc.). Do you like one over the other? Additional options?
if (!require("pacman")) install.packages("pacman")
if (!"pacman" %in% dir(.libPaths())) install.packages("pacman")
I like the first because it's less obscure and shorter to me.
I use pacman exactly for this reason. I found the package through an SE answer of your's where you also use the first option. So there is already our fait accompli.
I also prefer the first one but only because I know that require("pacman")
returns a boolean variable.
@Michael-E-Rose Thanks for the feedback :+1:
There's an additional difference. The first loads pacman while the later keeps the workspace clean and only loads the namespace.
The whole point is to bootstrap the process of getting pacman on the system and loaded so I'm not sure why we would want to only load the namespace if we're just going to load the package anyways.
if (!require("pacman")) install.packages("pacman"); library("pacman")
We probably need to add that additional call to load pacman after we install it. We probably could/should make something like p_boot which exports these commands as text to make it easy to copy/paste to the top of a script.
@Dason Good call. I agree.
Here's a proposed p_boot
:
#' Script Header: Ensure \pkg{pacman} is Installed
#'
#' A standard \pkg{pacman} header for scripts that will ensure \pkg{pacman} is
#' installed before attempting to use it. These commands will attempt to be
#' copied to the clipboard for easy cut and paste if used interactively.
#'
#' @details The script header takes the form of:
#'
#' \code{if (!require("pacman")) install.packages("pacman"); library(pacman)}
#'
#' @param load logical. If \code{TRUE} \code{; library(pacman)} is added to the
#' end of the script header.
#' @param copy2clip logical. If \code{TRUE} attempts to copy the output to the
#' clipboard.
#' @return Returns a script header string (optionally copies to the clipboard).
#' @keywords header
#' @export
#' @examples
#' p_boot()
p_boot <- function(load = TRUE, copy2clip = interactive()) {
out <- sprintf("if (!require(\"pacman\")) install.packages(\"pacman\")%s",
ifelse(isTRUE(load), "; library(pacman)", ""))
if(copy2clip){
writeToClipboard(paste(out, collapse="\n"))
}
message(out)
return(invisible(out))
}
I wonder if p_header
, p_script
, p_ensure_pacman
or something is a better name.
How is that going to work? You want users to source on a script just to ensure that pacman is installed/loaded? What's the advantage over if (!require("pacman")) install.packages("pacman"); library("pacman")
?
@Michael-E-Rose It is if (!require("pacman")) install.packages("pacman"); library("pacman")
but that way the user can just use p_boot
and that snippet appears and is attempted to be copied to the clipboard (may not work for some Linux users). They manually paste it at the top of their script.
But if you didn't get that that tells me the description needs some work.
But as a user I would have to copy the script above first, wouldn't I? Or how do I get the code?
@Michael-E-Rose This will become a function in the pacman package. For now you have to copy it which is annoying (plus wouldn't work as you'd need to load the internal functions likewriteToClipboard
first) , but once it's added and merged you can use it from the dev version simply by typing boot
. I've tweaked the description to make the function's purposes clearer.
#' Script Header: Ensure \pkg{pacman} is Installed
#'
#' Generate a string for the standard \pkg{pacman} script header that, when
#' added to scripts, will ensure \pkg{pacman} is installed before attempting to
#' use it. \pkg{pacman} will attempt to copy this string (standard script
#' header) to the clipboard for easy cut and paste.
#'
#' @details The script header takes the form of:
#'
#' \code{if (!require("pacman")) install.packages("pacman"); library(pacman)}
#'
#' This can be copied to the top of scripts to make it easy to run scripts if
#' the user shares them with others or to aid in long term script management.
#' This may also be useful for blog posts and \pkg{R} help sites like
#' \href{http://www.talkstats.com/}{TalkStats} or
#' \href{http://stackoverflow.com/questions/tagged/r}{StackOverflow}. In this
#' way functions like `p_load` can be used without fear that the user doesn't
#' have \pkg{pacman} installed.
#' @param load logical. If \code{TRUE} \code{; library(pacman)} is added to the
#' end of the script header.
#' @param copy2clip logical. If \code{TRUE} attempts to copy the output to the
#' clipboard.
#' @return Returns a script header string (optionally copies to the clipboard).
#' @keywords header
#' @export
#' @examples
#' p_boot()
p_boot <- function(load = TRUE, copy2clip = interactive()) {
out <- sprintf("if (!require(\"pacman\")) install.packages(\"pacman\")%s",
ifelse(isTRUE(load), "; library(pacman)", ""))
if(copy2clip){
writeToClipboard(paste(out, collapse="\n"))
}
message(out)
return(invisible(out))
}