[R-pkg-devel] Printing examples conditionally on another package in Suggests

Alexandre Courtiol @lex@ndre@courtiol @ending from gm@il@com
Fri Aug 10 13:14:13 CEST 2018


Perhaps then something like:
Print <- function(x) if (requireNamespace("some.package", quietly = TRUE))
print(x)
Print("Hi")
Print("Hello")
Print("Goodbye")



On Fri, 10 Aug 2018 at 12:33, Zhian Kamvar <zkamvar using gmail.com> wrote:

> Mainly, I would like to see the value printed after the print statement
> like it would appear in a normal R session:
>
> print("Hi")
> #> [1] "Hi"
> print("Hello")
> #> [1] "Hello"
> print("Goodbye")
> #> [1] "Goodbye"
>
>
> On Fri, Aug 10, 2018 at 11:28 AM Alexandre Courtiol <
> alexandre.courtiol using gmail.com> wrote:
>
>> Hi Zhian,
>> Could you please explain what behaviour you would like to obtain?
>> I really don't understand what your problem is from your description...
>> Alex
>>
>> On Fri, 10 Aug 2018 at 12:18, Zhian Kamvar <zkamvar using gmail.com> wrote:
>>
>>> Hello,
>>>
>>> I know it's good practice to use
>>>
>>> if (require("some_package")) {
>>>   # some code that needs some_package
>>> }
>>>
>>> In R examples that needs a package listed in Suggests.
>>>
>>> The problem with this approach is that if there are any print statements
>>> within this structure, then they only get printed after the braces and
>>> not
>>> after the lines like so:
>>>
>>> if (TRUE) {
>>>   print("Hi")
>>>   print("Hello")
>>>   print("Goodbye")
>>> }
>>> #> [1] "Hi"
>>> #> [1] "Hello"
>>> #> [1] "Goodbye"
>>>
>>> The only way I can think of circumventing this is by replacing the if
>>> statement with a stopifnot statement:
>>>
>>> stopifnot(require("some_package"))
>>> # some code that needs some_package
>>>
>>> But, I'm not sure if that's okay to do in a function example. Does anyone
>>> have any ideas or suggestions on how to help with this kind of thing?
>>>
>>> Cheers,
>>> Zhian
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-package-devel using r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>>>
>>
>>
>> --
>> Alexandre Courtiol
>>
>> http://sites.google.com/site/alexandrecourtiol/home
>>
>> *"Science is the belief in the ignorance of experts"*, R. Feynman
>>
>

-- 
Alexandre Courtiol

http://sites.google.com/site/alexandrecourtiol/home

*"Science is the belief in the ignorance of experts"*, R. Feynman

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list