stryx
stryx

Reputation: 11

How to install Hmisc package? (Line 108(chunk 10) package 'Hmisc' could not be loaded)

I am trying to run the below code in R Studio. I am unable to install Hmisc package. You may ask me for more info about something and I will reply. Windows 11, R 4.3.2 and RStudio is the latest version, Here is the code:

install.packages("UsingR", repos = "http://cran.us.r-project.org")

install.packages("Hmisc", repos = "http://cran.us.r-project.org")

library("UsingR")

install.packages("tidyverse", repos = "http://cran.us.r-project.org")

library("tidyverse")

library("viridis")

The error:

(Line 108(chunk 10) package 'Hmisc' could not be loaded)

Upvotes: 0

Views: 597

Answers (1)

medium-dimensional
medium-dimensional

Reputation: 2253

One can not load a package if it is not installed properly. So, please make sure the package Hmisc is installed properly. You can use .packages to check the package availability:

> "Hmisc" %in% .packages(all.available = T)
# Returns TRUE if Hmisc is installed 

Installation of packages can fail because of many unknown causes. You need to understand what specific cause is hindering the installation of the package in your system.

For example, if I run the following code in R console* on my computer:

> install.packages("Hmisc", repos = "http://cran.us.r-project.org")

I get the following error:

trying URL 'http://cran.us.r-project.org/bin/macosx/big-sur-arm64/contrib/4.3/Hmisc_5.1-1.tgz'
Content type 'application/x-gzip' length 3556151 bytes (3.4 MB)
===========================
downloaded 1.8 MB

Error in download.file(url, destfile, method, mode = "wb", ...) : 
  download from 'http://cran.us.r-project.org/bin/macosx/big-sur-arm64/contrib/4.3/Hmisc_5.1-1.tgz' failed
In addition: Warning messages:
1: In download.file(url, destfile, method, mode = "wb", ...) :
  downloaded length 0 != reported length 0
2: In download.file(url, destfile, method, mode = "wb", ...) :
  URL 'http://lib.stat.cmu.edu/R/CRAN/bin/macosx/big-sur-arm64/contrib/4.3/Hmisc_5.1-1.tgz': Timeout of 60 seconds was reached
Warning in download.packages(pkgs, destdir = tmpd, available = available,  :
  download of package ‘Hmisc’ failed

If one just reads through the installation error, they might be curios to understand the phrase Timeout of 60 seconds was reached.

One then can see what this Timeout is in the context of install.packages by utilising the help operator ? in R Console:

> ?install.packages

...
...
Timeouts
A limit on the elapsed time for each call to R CMD INSTALL (so for source installs) can be set via environment variable R_INSTALL_PACKAGES_ELAPSED_TIMEOUT: in seconds (or in minutes or hours with optional suffix ‘⁠m⁠’ or ‘⁠h⁠’, suffix ‘⁠s⁠’ being allowed for the default seconds) with 0 meaning no limit.

The help call hints that setting time limit to 0 might be helpful if the installation procedure needs more than just 60 seconds to install the package.** But how do I inform the function call to set no time limit for installation? From the output of ?install.packages related to Timeout, I see that a phrase 'environment variable' might be helpful to know more about.

A quick search on the internet with "environment variable timeout in r" and couple of clicks here and there took me to the base function options. I searched if options can help me change the time limit by looking at anything that resembled to "timeout" or "time limit". options indeed allows to set an argument timeout:

...
...
timeout:
positive integer. The timeout for some Internet operations, in seconds. Default 60 (seconds) but can be set from environment variable R_DEFAULT_INTERNET_TIMEOUT. (Invalid values of the option or the variable are silently ignored: non-integer numeric values will be truncated.) See download.file and connections.

Perfect. Then I tried:

> options(timeout = 5 * 60) # setting time limit to 5 minutes
> install.packages("Hmisc", repos = "http://cran.us.r-project.org")

No error was generated and Hmisc was successfully installed:

trying URL 'http://cran.us.r-project.org/bin/macosx/big-sur-arm64/contrib/4.3/Hmisc_5.1-1.tgz'
Content type 'application/x-gzip' length 3556151 bytes (3.4 MB)
================================================== downloaded 3.4 MB

The downloaded binary packages are in /var/folders/dx/#########

Then I tried library(Hmisc) which led to successful loading of Hmisc in to the current workspace:

Attaching package: ‘Hmisc’

The following objects are masked from ‘package:base’:

format.pval, units


*R console can be accessed inside R Studio: Go to View > Move Focus To Console. See this set of instructions to see where it is located in R Studio.

**If you are located in a country other than USA, you can also try that country's CRAN mirror manually after running:

> install.packages("Hmisc")

Upvotes: 0

Related Questions