Re: [R-pkg-devel] Calling R's tempdir() from C
Hi, On 10.08.18 3:10 PM, Duncan Murdoch wrote: On 10/08/2018 12:01 AM, Rodrigo Tobar wrote: [...] Why not pass the result of tempdir() in your call from R to your C++ function, or in an initialization call for your package? It won't change during a session. This is indeed a good suggestion, but (my bad!) I forgot to mention: the place I need to use this is at dynlib loading time (i.e., when R_init_mypackage is called), so I don't have the flexibility to pass arbitrary arguments. I guess this is a separate but related question, but it's not clear to me if in the process of loading a package, any particular piece of R code is guaranteed to execute before the library is loaded; otherwise I could pass down the information by setting an environment variable in R and read it from C++, or even perform all the directory creation logic at the R level. Thanks again, Rodrigo __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
Re: [R-pkg-devel] Printing examples conditionally on another package in Suggests
This was what I was looking for! Thank you! I apologize for my insufficient google-fu today. On Fri, Aug 10, 2018 at 12:55 PM Georgi Boshnakov < georgi.boshna...@manchester.ac.uk> wrote: > Maybe Martin Maechler's post in response to a similar question answers > your question: > > https://stat.ethz.ch/pipermail/r-package-devel/2018q2/002780.html > > In summary, wrap the code in: > > if (requireNamespace("MASS", quietly = TRUE)) withAutoprint({ > > your code > > }) > > > Georgi Boshnakov > > -Original Message- > From: R-package-devel [mailto:r-package-devel-boun...@r-project.org] On > Behalf Of Zhian Kamvar > Sent: 10 August 2018 12:26 > To: Alexandre Courtiol > Cc: R-list Package Devel > Subject: Re: [R-pkg-devel] Printing examples conditionally on another > package in Suggests > > Thanks, but my use of print here is really a toy example, not necessarily > the end-goal. This strategy would fail if I were to attempt load a data set > from some_package or use any functions from some_package. > > A more specific example of what I'm dealing with is here: > > http://www.repidemicsconsortium.org/incidence/reference/get_counts.html#examples > . > This is what the user would see if they were to use example(get_counts). > > On Fri, Aug 10, 2018 at 12:14 PM Alexandre Courtiol < > alexandre.court...@gmail.com> wrote: > > > 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 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.court...@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 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@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]] > > __ > R-package-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-package-devel > [[alternative HTML version deleted]] __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
Re: [R-pkg-devel] Printing examples conditionally on another package in Suggests
Maybe Martin Maechler's post in response to a similar question answers your question: https://stat.ethz.ch/pipermail/r-package-devel/2018q2/002780.html In summary, wrap the code in: if (requireNamespace("MASS", quietly = TRUE)) withAutoprint({ your code }) Georgi Boshnakov -Original Message- From: R-package-devel [mailto:r-package-devel-boun...@r-project.org] On Behalf Of Zhian Kamvar Sent: 10 August 2018 12:26 To: Alexandre Courtiol Cc: R-list Package Devel Subject: Re: [R-pkg-devel] Printing examples conditionally on another package in Suggests Thanks, but my use of print here is really a toy example, not necessarily the end-goal. This strategy would fail if I were to attempt load a data set from some_package or use any functions from some_package. A more specific example of what I'm dealing with is here: http://www.repidemicsconsortium.org/incidence/reference/get_counts.html#examples. This is what the user would see if they were to use example(get_counts). On Fri, Aug 10, 2018 at 12:14 PM Alexandre Courtiol < alexandre.court...@gmail.com> wrote: > 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 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.court...@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 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@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]] __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
Re: [R-pkg-devel] Printing examples conditionally on another package in Suggests
Thanks, but my use of print here is really a toy example, not necessarily the end-goal. This strategy would fail if I were to attempt load a data set from some_package or use any functions from some_package. A more specific example of what I'm dealing with is here: http://www.repidemicsconsortium.org/incidence/reference/get_counts.html#examples. This is what the user would see if they were to use example(get_counts). On Fri, Aug 10, 2018 at 12:14 PM Alexandre Courtiol < alexandre.court...@gmail.com> wrote: > 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 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.court...@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 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@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]] __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
Re: [R-pkg-devel] Printing examples conditionally on another package in Suggests
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 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.court...@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 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@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]] __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
Re: [R-pkg-devel] Printing examples conditionally on another package in Suggests
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 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@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 [[alternative HTML version deleted]] __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
[R-pkg-devel] Printing examples conditionally on another package in Suggests
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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
Re: [R-pkg-devel] Calling R's tempdir() from C
On 10/08/2018 12:01 AM, Rodrigo Tobar wrote: Dear all, I want to create a temporary directory from within my C++ R extension. On the other hand, CRAN's policy dictates that one cannot write anything outside the boundaries of the session's temporary directory, so I need to create it exactly there. The R API offers R_tmpnam and R_tmpnam2, both of which require the path of the directory where the new name will reside. This can be obtained using tempdir() in R, but I couldn't find anything similar in the R API. Am I missing anything too obvious? Why not pass the result of tempdir() in your call from R to your C++ function, or in an initialization call for your package? It won't change during a session. Duncan Murdoch I tried out giving a NULL dirname to R_tmpnam{,2}, hoping that even though it's not mentioned in the docs, they would internally use the session's tmp dir, but this simply crashed. When embedding R one could also use the R_TempDir global variable, but this is flagged as unofficial R API for extensions. Finally, the only solution I could find was to call the tempdir() R function from within my C++ extension. This works fine, but it bothers me that there not an equivalent entry point in R's API. Any pointers would be appreciated. If I'm deeply misunderstanding something please also shoot. Cheers, Rodrigo __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel __ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel