Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Kelly Thompson
Andrew,
Thanks. I reviewed the code for "require" and saw:
"if (!character.only)
package <- as.character(substitute(package))"

#This helps me better understand what is going on. I am sharing this
here because I think it might help others understand.
as.character( substitute("this_pac_does_not_exist") ) #quoted

as.character( substitute( this_pac_does_not_exist ) ) #not quoted

as.character( substitute("this_pac_does_not_exist") ) == as.character(
substitute( this_pac_does_not_exist ) )

#

packages_i_want_to_use <- c("this_pac_does_not_exist", "abcz")
as.character( substitute(packages_i_want_to_use[1] ) )
packages_i_want_to_use[1]
as.character( substitute(packages_i_want_to_use[1] ) ) ==
packages_i_want_to_use[1]

#To prevent substitute(packages_i_want_to_use[1] from getting changed
to as.character( substitute(packages_i_want_to_use[1] ) ), we need to
set character.only = TRUE

On Mon, Oct 24, 2022 at 12:53 PM Andrew Simmons  wrote:
>
> In the first one, the argument is a character vector of length 1, so the code 
> works perfectly fine.
>
> The second is a call, and when coerced to a character vector should look like
>
> c("[", "packages_i_want_to_use", "1")
>
> You can try this yourself with quote(packages_i_want_to_use[1]) which returns 
> its first argument, unevaluated.
>
> On Mon, Oct 24, 2022, 12:46 Kelly Thompson  wrote:
>>
>> Thanks!
>>
>> # Please, can you help me understand why
>> require( 'base' ) # works, but
>> require( packages_i_want_to_use[1] ) # does not work?
>>
>> # In require( 'base' ), what is the "first argument"?
>>
>> On Mon, Oct 24, 2022 at 12:29 PM Andrew Simmons  wrote:
>> >
>> > 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  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@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.
>>
>> __
>> R-help@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.

__
R-help@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.


Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Andrew Simmons
In the first one, the argument is a character vector of length 1, so the
code works perfectly fine.

The second is a call, and when coerced to a character vector should look
like

c("[", "packages_i_want_to_use", "1")

You can try this yourself with quote(packages_i_want_to_use[1]) which
returns its first argument, unevaluated.

On Mon, Oct 24, 2022, 12:46 Kelly Thompson  wrote:

> Thanks!
>
> # Please, can you help me understand why
> require( 'base' ) # works, but
> require( packages_i_want_to_use[1] ) # does not work?
>
> # In require( 'base' ), what is the "first argument"?
>
> On Mon, Oct 24, 2022 at 12:29 PM Andrew Simmons 
> wrote:
> >
> > 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  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@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.
>
> __
> R-help@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]]

__
R-help@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.


Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Kelly Thompson
Thanks!

# Please, can you help me understand why
require( 'base' ) # works, but
require( packages_i_want_to_use[1] ) # does not work?

# In require( 'base' ), what is the "first argument"?

On Mon, Oct 24, 2022 at 12:29 PM Andrew Simmons  wrote:
>
> 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  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@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.

__
R-help@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.


Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Jeff Newmiller
Don't load base. It is already loaded.

On October 24, 2022 9:07:44 AM PDT, Kelly Thompson  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@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.

__
R-help@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.


Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Bert Gunter
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  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@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.

__
R-help@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.


Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Henrik Bengtsson
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  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@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.

__
R-help@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.


Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Andrew Simmons
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  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@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]]

__
R-help@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.


Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Ivan Krylov
В Mon, 24 Oct 2022 12:07:44 -0400
Kelly Thompson  пишет:

> require( packages_i_want_to_use[1] )
> #Error in if (!loaded) { : the condition has length > 1

This 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

__
R-help@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.