| 1 |
#' An alternative to sessionInfo() to print session information |
| 2 |
#' |
| 3 |
#' This function tweaks the output of [sessionInfo()]: (1) It adds |
| 4 |
#' the RStudio version information if running in the RStudio IDE; (2) It removes |
| 5 |
#' the information about matrix products, BLAS, and LAPACK; (3) It removes the |
| 6 |
#' names of base R packages; (4) It prints out package versions in a single |
| 7 |
#' group, and does not differentiate between loaded and attached packages. |
| 8 |
#' |
| 9 |
#' It also allows you to only print out the versions of specified packages (via |
| 10 |
#' the `packages` argument) and optionally their recursive dependencies. |
| 11 |
#' For these specified packages (if provided), if a function |
| 12 |
#' `xfun_session_info()` exists in a package, it will be called and |
| 13 |
#' expected to return a character vector to be appended to the output of |
| 14 |
#' `session_info()`. This provides a mechanism for other packages to inject |
| 15 |
#' more information into the `session_info` output. For example, |
| 16 |
#' \pkg{rmarkdown} (>= 1.20.2) has a function `xfun_session_info()` that |
| 17 |
#' returns the version of Pandoc, which can be very useful information for |
| 18 |
#' diagnostics. |
| 19 |
#' @param packages A character vector of package names, of which the versions |
| 20 |
#' will be printed. If not specified, it means all loaded and attached |
| 21 |
#' packages in the current R session. |
| 22 |
#' @param dependencies Whether to print out the versions of the recursive |
| 23 |
#' dependencies of packages. |
| 24 |
#' @return A character vector of the session information marked as |
| 25 |
#' [raw_string()]. |
| 26 |
#' @export |
| 27 |
#' @examplesIf interactive() |
| 28 |
#' xfun::session_info() |
| 29 |
#' if (xfun::loadable('MASS')) xfun::session_info('MASS') |
| 30 |
session_info = function(packages = NULL, dependencies = TRUE) { |
| 31 |
res = sessionInfo() |
| 32 |
res$matprod = res$BLAS = res$LAPACK = NULL |
| 33 |
res$running = paste(c(res$running, if (Sys.getenv('POSITRON') == '1') { |
| 34 |
c(', Positron ', Sys.getenv('POSITRON_VERSION')) |
| 35 |
} else if (loadable('rstudioapi') && rstudioapi::isAvailable()) { |
| 36 |
c(', RStudio ', as.character(rstudioapi::getVersion())) |
| 37 |
}), collapse = '') |
| 38 |
|
| 39 |
tweak_info = function(obj, extra = NULL) { |
| 40 |
res = capture.output(print(obj, tzone = FALSE)) |
| 41 |
i = grep('^(attached base packages|Matrix products):\\s*$', res, ignore.case = TRUE) |
| 42 |
if (length(i)) res = res[-c(i, i + 1)] |
| 43 |
res = gsubi('^\\s*locale:\\s*$', 'Locale:', res) |
| 44 |
res = gsub('^\\s*\\[[0-9]+]\\s*', ' ', res) # remove vector indices like [1] |
| 45 |
res = gsubi('^\\s*other attached packages:\\s*$', 'Package version:', res) |
| 46 |
# print the locale info on a single line if possible |
| 47 |
if (length(i <- which(res == 'Locale:')) == 1 && res[i + 2] == '') { |
| 48 |
res[i] = paste(res[i], gsub('\\s*/\\s*', ' / ', gsub('^\\s+', '', res[i + 1]))) |
| 49 |
res = res[-(i + 1)] |
| 50 |
} |
| 51 |
# remove extra blank lines |
| 52 |
if ((n <- length(res)) > 1) { |
| 53 |
i = is_blank(res) |
| 54 |
res = res[!c(FALSE, i[1:(n-1)] & i[2:n])] |
| 55 |
} |
| 56 |
raw_string(c(res, extra)) |
| 57 |
} |
| 58 |
|
| 59 |
version_info = function(pkgs) { |
| 60 |
res = lapply(pkgs, function(p) { |
| 61 |
list(Version = as.character(packageVersion(p)), Package = p) |
| 62 |
}) |
| 63 |
as.list(setNames(res, pkgs)) |
| 64 |
} |
| 65 |
|
| 66 |
res$basePkgs = raw_string(list()) |
| 67 |
info = c(res$otherPkgs, res$loadedOnly) |
| 68 |
packages = setdiff(packages, '') # remove empty strings (#65) |
| 69 |
if (length(packages) > 0) { |
| 70 |
info = info[intersect(names(info), packages)] |
| 71 |
info = c(info, version_info(setdiff(packages, names(info)))) |
| 72 |
} |
| 73 |
res$loadedOnly = NULL |
| 74 |
if (dependencies) { |
| 75 |
deps = pkg_dep(names(info), installed.packages(), recursive = TRUE) |
| 76 |
deps = sort(setdiff(deps, names(info))) |
| 77 |
info = c(info, version_info(deps)) |
| 78 |
} |
| 79 |
if (length(packages) > 0 || dependencies) info = info[sort(names(info))] |
| 80 |
res$otherPkgs = info |
| 81 |
extra = unlist(lapply(packages, function(p) tryCatch( |
| 82 |
c('', getFromNamespace('xfun_session_info', p)()), error = function(e) NULL) |
| 83 |
)) |
| 84 |
|
| 85 |
tweak_info(res, extra) |
| 86 |
} |
| 87 |
|
| 88 |
#' Perform a task once in an R session |
| 89 |
#' |
| 90 |
#' Perform a task once in an R session, e.g., emit a message or warning. Then |
| 91 |
#' give users an optional hint on how not to perform this task at all. |
| 92 |
#' @param task Any R code expression to be evaluated once to perform a task, |
| 93 |
#' e.g., `warning('Danger!')` or `message('Today is ', Sys.Date())`. |
| 94 |
#' @param option An R option name. This name should be as unique as possible in |
| 95 |
#' [options()]. After the task has been successfully performed, |
| 96 |
#' this option will be set to `FALSE` in the current R session, to |
| 97 |
#' prevent the task from being performed again the next time when |
| 98 |
#' `do_once()` is called. |
| 99 |
#' @param hint A character vector to provide a hint to users on how not to |
| 100 |
#' perform the task or see the message again in the current R session. Set |
| 101 |
#' `hint = ""` if you do not want to provide the hint. |
| 102 |
#' @return The value returned by the `task`, invisibly. |
| 103 |
#' @export |
| 104 |
#' @examples |
| 105 |
#' do_once(message("Today's date is ", Sys.Date()), "xfun.date.reminder") |
| 106 |
#' # if you run it again, it will not emit the message again |
| 107 |
#' do_once(message("Today's date is ", Sys.Date()), "xfun.date.reminder") |
| 108 |
#' |
| 109 |
#' do_once({Sys.sleep(2); 1 + 1}, "xfun.task.1plus1") |
| 110 |
#' do_once({Sys.sleep(2); 1 + 1}, "xfun.task.1plus1") |
| 111 |
do_once = function(task, option, hint = c( |
| 112 |
'You will not see this message again in this R session.', |
| 113 |
'If you never want to see this message,', |
| 114 |
sprintf('you may set options(%s = FALSE) in your .Rprofile.', option) |
| 115 |
)) { |
| 116 |
if (identical(getOption(option), FALSE)) return(invisible()) |
| 117 |
task |
| 118 |
hint = paste(hint, collapse = ' ') |
| 119 |
if (hint != '') message(hint) |
| 120 |
options(setNames(list(FALSE), option)) |
| 121 |
invisible(task) |
| 122 |
} |