# User agent diagnostic script for Posit Package Manager binary packages local({ if (.Platform$OS.type != "unix" || Sys.info()["sysname"] == "Darwin") { message("Success! Posit Package Manager does not require additional configuration to install binary packages on macOS or Windows.") return(invisible()) } dl_method <- getOption("download.file.method", "") dl_extra_args <- getOption("download.file.extra", "") user_agent <- getOption("HTTPUserAgent", "") if (dl_method == "") { dl_method <- if (isTRUE(capabilities("libcurl"))) "libcurl" else "internal" } default_ua <- sprintf("R (%s)", paste(getRversion(), R.version$platform, R.version$arch, R.version$os)) instruction_template <- 'You must configure your HTTP user agent in R to install binary packages. In your user startup file (~/.Rprofile) or site-wide startup file (Rprofile.site), add: %s Then restart your R session and run this diagnostic script again. ' message(c( sprintf("R installation path: %s\n", R.home()), sprintf("R version: %s\n", R.version.string), sprintf("OS version: %s\n", utils::sessionInfo()$running), sprintf("HTTPUserAgent: %s\n", user_agent), sprintf("Download method: %s\n", dl_method), sprintf("Download extra args: %s\n", dl_extra_args), "\n----------------------------\n" )) if (dl_method == "libcurl") { if (!grepl(default_ua, user_agent, fixed = TRUE) || (getRversion() >= "3.6.0" && substr(user_agent, 1, 3) == "R (")) { config <- '# Configure the R user agent header to install Linux binary packages options(HTTPUserAgent = sprintf("R/%s R (%s)", getRversion(), paste(getRversion(), R.version["platform"], R.version["arch"], R.version["os"])))' message(sprintf(instruction_template, config)) return(invisible()) } } else if (dl_method == "curl") { if (!grepl(sprintf("User-Agent: %s", default_ua), dl_extra_args, fixed = TRUE)) { ua_arg <- "sprintf(\"-L --header 'User-Agent: R (%s)'\", paste(getRversion(), R.version[\"platform\"], R.version[\"arch\"], R.version[\"os\"]))" if (dl_extra_args != "") { old_args <- sprintf("\n %s,", shQuote(dl_extra_args)) } else { old_args <- "" } config <- paste0( 'options(download.file.extra = paste(', old_args, ' # Follow redirects, show errors, and display the HTTP status and URL "-fsSL -w \'%{stderr}curl: HTTP %{http_code} %{url_effective}\\n\'", # Configure the R user agent header to install Linux binary packages sprintf("--header \'User-Agent: R (%s)\'", paste(getRversion(), R.version["platform"], R.version["arch"], R.version["os"])) ))') message(sprintf(instruction_template, config)) return(invisible()) } } else if (dl_method == "wget") { if (!grepl(sprintf("User-Agent: %s", default_ua), dl_extra_args, fixed = TRUE)) { ua_arg <- "sprintf(\"--header 'User-Agent: R (%s)'\", paste(getRversion(), R.version[\"platform\"], R.version[\"arch\"], R.version[\"os\"]))" if (dl_extra_args == "") { config <- sprintf("# Configure the R user agent header to install Linux binary packages options(download.file.extra = %s)", ua_arg) } else { config <- sprintf("# Configure the R user agent header to install Linux binary packages options(download.file.extra = paste(%s, %s))", shQuote(dl_extra_args), ua_arg) } message(sprintf(instruction_template, config)) return(invisible()) } } message("Success! Your user agent is correctly configured.") })