Can't Find Function After requireNamespace

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't Find Function After requireNamespace

Dario Strbenac-2
I have a simple function :

f <- function()
{
  if(requireNamespace("ggplot2"))
    qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
  else
    message("No graphics, just text.")
}

ggplot2 is in the Suggests field of the DESCRIPTION file. When I load the package, and run the function, I get an error :

> f()
Loading required namespace: ggplot2
Error in f() : could not find function "qplot"

I don't want all ggplot2 functions to be added to the search path, so I am not using require(ggplot2). How can I use it correctly ?

R version 3.1.1 (2014-07-10)
Platform: x86_64-unknown-linux-gnu (64-bit)

--------------------------------------
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Can't Find Function After requireNamespace

Simon Urbanek

On Sep 17, 2014, at 10:00 PM, Dario Strbenac <[hidden email]> wrote:

> I have a simple function :
>
> f <- function()
> {
>  if(requireNamespace("ggplot2"))
>    qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
>  else
>    message("No graphics, just text.")
> }
>
> ggplot2 is in the Suggests field of the DESCRIPTION file. When I load the package, and run the function, I get an error :
>
>> f()
> Loading required namespace: ggplot2
> Error in f() : could not find function "qplot"
>
> I don't want all ggplot2 functions to be added to the search path, so I am not using require(ggplot2). How can I use it correctly ?
>

ggplot2::qplot(Sepal.Length, Petal.Length, data = iris, color = Species)

Cheers,
S


> R version 3.1.1 (2014-07-10)
> Platform: x86_64-unknown-linux-gnu (64-bit)
>
> --------------------------------------
> Dario Strbenac
> PhD Student
> University of Sydney
> Camperdown NSW 2050
> Australia
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Can't Find Function After requireNamespace

Simon Urbanek
BTW: you probably also want to use requireNamespace(..., quietly=TRUE) otherwise if sort of defeats the purpose ...

On Sep 17, 2014, at 10:46 PM, Simon Urbanek <[hidden email]> wrote:

>
> On Sep 17, 2014, at 10:00 PM, Dario Strbenac <[hidden email]> wrote:
>
>> I have a simple function :
>>
>> f <- function()
>> {
>> if(requireNamespace("ggplot2"))
>>   qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
>> else
>>   message("No graphics, just text.")
>> }
>>
>> ggplot2 is in the Suggests field of the DESCRIPTION file. When I load the package, and run the function, I get an error :
>>
>>> f()
>> Loading required namespace: ggplot2
>> Error in f() : could not find function "qplot"
>>
>> I don't want all ggplot2 functions to be added to the search path, so I am not using require(ggplot2). How can I use it correctly ?
>>
>
> ggplot2::qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
>
> Cheers,
> S
>
>
>> R version 3.1.1 (2014-07-10)
>> Platform: x86_64-unknown-linux-gnu (64-bit)
>>
>> --------------------------------------
>> Dario Strbenac
>> PhD Student
>> University of Sydney
>> Camperdown NSW 2050
>> Australia
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel