Kelly Thompson
2022-Oct-24 16:07 UTC
[R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?
# Below, when using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ? # This is my reproducible code: #create a vector with the names of the packages I want to use packages_i_want_to_use <- c('base', 'this_pac_does_not_exist') # Here I get error messages: require( packages_i_want_to_use[1] ) #Error in if (!loaded) { : the condition has length > 1 require( packages_i_want_to_use[2] ) #Error in if (!loaded) { : the condition has length > 1 # Here I get what I expect: require('base') require('this_pac_does_not_exist') #Loading required package: this_pac_does_not_exist #Warning message: #In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : # there is no package called ?this_pac_does_not_exist?
Andrew Simmons
2022-Oct-24 16:28 UTC
[R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?
require(), similarly to library(), does not evaluate its first argument UNLESS you add character.only = TRUE require( packages_i_want_to_use[1], character.only = TRUE) On Mon, Oct 24, 2022, 12:26 Kelly Thompson <kt1572757 at gmail.com> wrote:> # Below, when using require(), why do I get the error message "Error > in if (!loaded) { : the condition has length > 1" ? > > # This is my reproducible code: > > #create a vector with the names of the packages I want to use > packages_i_want_to_use <- c('base', 'this_pac_does_not_exist') > > # Here I get error messages: > require( packages_i_want_to_use[1] ) > #Error in if (!loaded) { : the condition has length > 1 > > require( packages_i_want_to_use[2] ) > #Error in if (!loaded) { : the condition has length > 1 > > # Here I get what I expect: > require('base') > > require('this_pac_does_not_exist') > #Loading required package: this_pac_does_not_exist > #Warning message: > #In library(package, lib.loc = lib.loc, character.only = TRUE, > logical.return = TRUE, : > # there is no package called ?this_pac_does_not_exist? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Ivan Krylov
2022-Oct-24 16:34 UTC
[R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?
? Mon, 24 Oct 2022 12:07:44 -0400 Kelly Thompson <kt1572757 at gmail.com> ?????:> require( packages_i_want_to_use[1] ) > #Error in if (!loaded) { : the condition has length > 1This seems to be a bug in require(). In addition to understanding character strings as arguments, require() can load packages named by unquoted barewords: require(base) This uses non-standard evaluation, and the part that transforms an unquoted symbol into a package name doesn't expect to see what is effectively `[`(packages_i_want_to_use, 1) instead of a single symbol. If you pass the character.only = TRUE argument to require(), this additional handling is disabled, making your examples work again. It's a good idea to always pass this argument when calling require() with a variable as the first argument. -- Best regards, Ivan
Henrik Bengtsson
2022-Oct-24 16:34 UTC
[R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?
You need to pass character.only = TRUE to require() whenever you specify the package using a character variable. I agree, the error message is confusing. /Henrik On Mon, Oct 24, 2022 at 9:26 AM Kelly Thompson <kt1572757 at gmail.com> wrote:> > # Below, when using require(), why do I get the error message "Error > in if (!loaded) { : the condition has length > 1" ? > > # This is my reproducible code: > > #create a vector with the names of the packages I want to use > packages_i_want_to_use <- c('base', 'this_pac_does_not_exist') > > # Here I get error messages: > require( packages_i_want_to_use[1] ) > #Error in if (!loaded) { : the condition has length > 1 > > require( packages_i_want_to_use[2] ) > #Error in if (!loaded) { : the condition has length > 1 > > # Here I get what I expect: > require('base') > > require('this_pac_does_not_exist') > #Loading required package: this_pac_does_not_exist > #Warning message: > #In library(package, lib.loc = lib.loc, character.only = TRUE, > logical.return = TRUE, : > # there is no package called ?this_pac_does_not_exist? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Bert Gunter
2022-Oct-24 16:35 UTC
[R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?
Reread ?require more carefully. Especially for the 'package' argument. Incidentally, you don't need to require base. It's always available. -- Bert On Mon, Oct 24, 2022 at 9:25 AM Kelly Thompson <kt1572757 at gmail.com> wrote:> > # Below, when using require(), why do I get the error message "Error > in if (!loaded) { : the condition has length > 1" ? > > # This is my reproducible code: > > #create a vector with the names of the packages I want to use > packages_i_want_to_use <- c('base', 'this_pac_does_not_exist') > > # Here I get error messages: > require( packages_i_want_to_use[1] ) > #Error in if (!loaded) { : the condition has length > 1 > > require( packages_i_want_to_use[2] ) > #Error in if (!loaded) { : the condition has length > 1 > > # Here I get what I expect: > require('base') > > require('this_pac_does_not_exist') > #Loading required package: this_pac_does_not_exist > #Warning message: > #In library(package, lib.loc = lib.loc, character.only = TRUE, > logical.return = TRUE, : > # there is no package called ?this_pac_does_not_exist? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Jeff Newmiller
2022-Oct-24 16:41 UTC
[R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?
Don't load base. It is already loaded. On October 24, 2022 9:07:44 AM PDT, Kelly Thompson <kt1572757 at gmail.com> wrote:># Below, when using require(), why do I get the error message "Error >in if (!loaded) { : the condition has length > 1" ? > ># This is my reproducible code: > >#create a vector with the names of the packages I want to use >packages_i_want_to_use <- c('base', 'this_pac_does_not_exist') > ># Here I get error messages: >require( packages_i_want_to_use[1] ) >#Error in if (!loaded) { : the condition has length > 1 > >require( packages_i_want_to_use[2] ) >#Error in if (!loaded) { : the condition has length > 1 > ># Here I get what I expect: >require('base') > >require('this_pac_does_not_exist') >#Loading required package: this_pac_does_not_exist >#Warning message: >#In library(package, lib.loc = lib.loc, character.only = TRUE, >logical.return = TRUE, : ># there is no package called ?this_pac_does_not_exist? > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.