readLines function with R >= 3.5.0

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

readLines function with R >= 3.5.0

Ralf Stubner
Dear all,

I would like to draw you attention to this question on SO:
https://stackoverflow.com/questions/50372043/readlines-function-with-new-version-of-r

Based on the OP's code I used the script

#######################################
create_matrix <- function() {
  cat("Write the numbers of vertices: ")
  user_input <- readLines("stdin", n=1)
  user_input <- as.numeric(user_input)
  print(user_input)
  }
create_matrix()
#######################################

and called it with "R -f ..." from the command line.

With 'R version 3.4.4 (2018-03-15) -- "Someone to Lean On"' the script
prints the inputed number as expected. With both 'R version 3.5.0
(2018-04-23) -- "Joy in Playing"' and 'R Under development (unstable)
(2018-05-19 r74746) -- "Unsuffered Consequences"' the script does not
continue after inputing a number.

All these tests where done using docker images from the rocker project,
i.e. effectively using Debian Linux.

Is this an error in the script or in R?

Thanks
Ralf

--
Ralf Stubner
Senior Software Engineer / Trainer

daqana GmbH
Dortustraße 48
14467 Potsdam

T: +49 331 23 61 93 11
F: +49 331 23 61 93 90
M: +49 162 20 91 196
Mail: [hidden email]

Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

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

Re: readLines function with R >= 3.5.0

Martin Maechler
>>>>> Ralf Stubner
>>>>>     on Fri, 25 May 2018 19:18:58 +0200 writes:

    > Dear all, I would like to draw you attention to this
    > question on SO:
    > https://stackoverflow.com/questions/50372043/readlines-function-with-new-version-of-r


    > Based on the OP's code I used the script

    > #######################################
    > create_matrix <- function() {
    > cat("Write the numbers of vertices: ")
    > user_input <- readLines("stdin", n=1)
    > user_input <- as.numeric(user_input)
    > print(user_input)
    > }
    > create_matrix()
    > #######################################

    > and called it with "R -f <filename>" from the command line.

    > With 'R version 3.4.4 (2018-03-15) -- "Someone to Lean On"' the script
    > prints the inputed number as expected. With both 'R version 3.5.0
    > (2018-04-23) -- "Joy in Playing"' and 'R Under development (unstable)
    > (2018-05-19 r74746) -- "Unsuffered Consequences"' the script does not
    > continue after inputing a number.

I can confirm.
It "works" if you additionally (the [Enter], i.e., EOL) you also
"send" an EOF -- in Unix alikes via  <Ctrl>-D

The same happens if you use  'Rscript <filename>'

I'm not the expert here, but am close to sure that we (R core)
did not intend this change, when fixing other somewhat subtle
bugs in Rscript / 'R -f'

Martin Maechler


    > All these tests where done using docker images from the rocker project,
    > i.e. effectively using Debian Linux.

    > Is this an error in the script or in R?

    > Thanks
    > Ralf

    > --
    > Ralf Stubner
    > Senior Software Engineer / Trainer

    > daqana GmbH
    > Dortustraße 48
    > 14467 Potsdam

    > T: +49 331 23 61 93 11
    > F: +49 331 23 61 93 90
    > M: +49 162 20 91 196
    > Mail: [hidden email]

    > Sitz: Potsdam
    > Register: AG Potsdam HRB 27966 P
    > Ust.-IdNr.: DE300072622
    > Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

    > ______________________________________________
    > [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: readLines function with R >= 3.5.0

Martin Maechler
>>>>> Martin Maechler
>>>>>     on Mon, 28 May 2018 10:28:01 +0200 writes:

>>>>> Ralf Stubner
>>>>>     on Fri, 25 May 2018 19:18:58 +0200 writes:

    >> Dear all, I would like to draw you attention to this
    >> question on SO:
    >> https://stackoverflow.com/questions/50372043/readlines-function-with-new-version-of-r


    >> Based on the OP's code I used the script

    >> #######################################
    >> create_matrix <- function() {
    >> cat("Write the numbers of vertices: ")
    >> user_input <- readLines("stdin", n=1)
    >> user_input <- as.numeric(user_input)
    >> print(user_input)
    >> }
    >> create_matrix()
    >> #######################################

    >> and called it with "R -f <filename>" from the command line.

    >> With 'R version 3.4.4 (2018-03-15) -- "Someone to Lean On"' the script
    >> prints the inputed number as expected. With both 'R version 3.5.0
    >> (2018-04-23) -- "Joy in Playing"' and 'R Under development (unstable)
    >> (2018-05-19 r74746) -- "Unsuffered Consequences"' the script does not
    >> continue after inputing a number.

    > I can confirm.
    > It "works" if you additionally (the [Enter], i.e., EOL) you also
    > "send" an EOF -- in Unix alikes via  <Ctrl>-D

    > The same happens if you use  'Rscript <filename>'

    > I'm not the expert here, but am close to sure that we (R core)
    > did not intend this change, when fixing other somewhat subtle
    > bugs in Rscript / 'R -f'

    > Martin Maechler

The same behavior in regular R , no need for a script etc.

> str(readLines("stdin", n=1))

then in addition to the input  you need to "give" an EOF (Ctrl D) in R >= 3.5.0

Interestingly, everything works fine if you use  stdin() instead
of "stdin" :

> rr <- readLines(stdin(), n=1)
foo
> rr
[1] "foo"
>
----------

So, for now use  stdin()  which is much clearer than the string
"stdin" anyway

Martin Maechler

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

Re: readLines function with R >= 3.5.0

Gábor Csárdi
stdin() is not the same as file("stdin"), see the note in ?stdin.

Gabor

On Mon, 28 May 2018, 10:41 Martin Maechler, <[hidden email]>
wrote:

> >>>>> Martin Maechler
> >>>>>     on Mon, 28 May 2018 10:28:01 +0200 writes:
>
> >>>>> Ralf Stubner
> >>>>>     on Fri, 25 May 2018 19:18:58 +0200 writes:
>
>     >> Dear all, I would like to draw you attention to this
>     >> question on SO:
>     >>
> https://stackoverflow.com/questions/50372043/readlines-function-with-new-version-of-r
>
>
>     >> Based on the OP's code I used the script
>
>     >> #######################################
>     >> create_matrix <- function() {
>     >> cat("Write the numbers of vertices: ")
>     >> user_input <- readLines("stdin", n=1)
>     >> user_input <- as.numeric(user_input)
>     >> print(user_input)
>     >> }
>     >> create_matrix()
>     >> #######################################
>
>     >> and called it with "R -f <filename>" from the command line.
>
>     >> With 'R version 3.4.4 (2018-03-15) -- "Someone to Lean On"' the
> script
>     >> prints the inputed number as expected. With both 'R version 3.5.0
>     >> (2018-04-23) -- "Joy in Playing"' and 'R Under development
> (unstable)
>     >> (2018-05-19 r74746) -- "Unsuffered Consequences"' the script does
> not
>     >> continue after inputing a number.
>
>     > I can confirm.
>     > It "works" if you additionally (the [Enter], i.e., EOL) you also
>     > "send" an EOF -- in Unix alikes via  <Ctrl>-D
>
>     > The same happens if you use  'Rscript <filename>'
>
>     > I'm not the expert here, but am close to sure that we (R core)
>     > did not intend this change, when fixing other somewhat subtle
>     > bugs in Rscript / 'R -f'
>
>     > Martin Maechler
>
> The same behavior in regular R , no need for a script etc.
>
> > str(readLines("stdin", n=1))
>
> then in addition to the input  you need to "give" an EOF (Ctrl D) in R >=
> 3.5.0
>
> Interestingly, everything works fine if you use  stdin() instead
> of "stdin" :
>
> > rr <- readLines(stdin(), n=1)
> foo
> > rr
> [1] "foo"
> >
> ----------
>
> So, for now use  stdin()  which is much clearer than the string
> "stdin" anyway
>
> Martin Maechler
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

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

Re: readLines function with R >= 3.5.0

Ralf Stubner
On 28.05.2018 11:07, Gábor Csárdi wrote:
> stdin() is not the same as file("stdin"), see the note in ?stdin.

In particular stdin() works in an interactive session but not when R -f
/ Rscript is used, since it does not wait for the user to input anything:

$ R -f readLines.R

R version 3.5.0 (2018-04-23) -- "Joy in Playing"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
[...]
> create_matrix <- function() {
+   cat("Write the numbers of vertices: ")
+   user_input <- readLines(stdin(), n=1)
+   user_input <- as.numeric(user_input)
+   print(user_input)
+   }
> create_matrix()
Write the numbers of vertices: numeric(0)
>

>>>>>>> Martin Maechler
>>>>>>>     on Mon, 28 May 2018 10:28:01 +0200 writes:
>>     > It "works" if you additionally (the [Enter], i.e., EOL) you also
>>     > "send" an EOF -- in Unix alikes via  <Ctrl>-D

From my point of view this is a reasonable workaround for now.

Thanks
Ralf

--
Ralf Stubner
Senior Software Engineer / Trainer

daqana GmbH
Dortustraße 48
14467 Potsdam

T: +49 331 23 61 93 11
F: +49 331 23 61 93 90
M: +49 162 20 91 196
Mail: [hidden email]

Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze


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

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Martin Maechler
>>>>> Ralf Stubner
>>>>>     on Mon, 28 May 2018 12:13:57 +0200 writes:

    > On 28.05.2018 11:07, Gábor Csárdi wrote:
    >> stdin() is not the same as file("stdin"), see the note in ?stdin.

Of course it is not the same, otherwise using one for the other
had no effect, but you are right:
it is *NOT* a solution to the problem

    > In particular stdin() works in an interactive session but not when R -f
    > / Rscript is used, since it does not wait for the user to input anything:

    > $ R -f readLines.R

    > R version 3.5.0 (2018-04-23) -- "Joy in Playing"
    > Copyright (C) 2018 The R Foundation for Statistical Computing
    > Platform: x86_64-pc-linux-gnu (64-bit)
    > [...]
    >> create_matrix <- function() {
    > +   cat("Write the numbers of vertices: ")
    > +   user_input <- readLines(stdin(), n=1)
    > +   user_input <- as.numeric(user_input)
    > +   print(user_input)
    > +   }
    >> create_matrix()
    > Write the numbers of vertices: numeric(0)
    >>

oh dear...  yes, that is true ..


    >>>>>>>> Martin Maechler
    >>>>>>>> on Mon, 28 May 2018 10:28:01 +0200 writes:
    >>> > It "works" if you additionally (the [Enter], i.e., EOL) you also
    >>> > "send" an EOF -- in Unix alikes via  <Ctrl>-D

    > From my point of view this is a reasonable workaround for now.

I'm glad to hear that.

Then, I now do think this needs to be dealt with as a bug
(but I'm not delving into fixing it!)

Martin


    > Thanks
    > Ralf

    > --
    > Ralf Stubner
    > Senior Software Engineer / Trainer

    > daqana GmbH
    > Dortustraße 48
    > 14467 Potsdam

    > T: +49 331 23 61 93 11
    > F: +49 331 23 61 93 90
    > M: +49 162 20 91 196
    > Mail: [hidden email]

    > Sitz: Potsdam
    > Register: AG Potsdam HRB 27966 P
    > Ust.-IdNr.: DE300072622
    > Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

    > x[DELETED ATTACHMENT signature.asc, application/pgp-signature]
    > ______________________________________________
    > [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: readLines function with R >= 3.5.0

Ralf Stubner
On 28.05.2018 16:38, Martin Maechler wrote:
> Then, I now do think this needs to be dealt with as a bug
> (but I'm not delving into fixing it!)

Ok. Can somebody with write privileges in bugzilla add the bug report? I
can also do this myself, if somebody with the required privileges can
create a user for me.

Greetings
Ralf

PS: I get an error message from https://bugs.r-project.org/bugzilla3/.

--
Ralf Stubner
Senior Software Engineer / Trainer

daqana GmbH
Dortustraße 48
14467 Potsdam

T: +49 331 23 61 93 11
F: +49 331 23 61 93 90
M: +49 162 20 91 196
Mail: [hidden email]

Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze


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

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: readLines function with R >= 3.5.0

Martin Maechler
>>>>> Ralf Stubner
>>>>>     on Tue, 29 May 2018 11:21:28 +0200 writes:

    > On 28.05.2018 16:38, Martin Maechler wrote:
    >> Then, I now do think this needs to be dealt with as a bug
    >> (but I'm not delving into fixing it!)

    > Ok. Can somebody with write privileges in bugzilla add the
    > bug report? I can also do this myself, if somebody with
    > the required privileges can create a user for me.

    > Greetings Ralf

<< PS: I get an error message from   https://bugs.r-project.org/bugzilla3/.

Yes, it is currently "down", i.e., in a wrong state.
I had alerted the owner of the server a few hours ago, but as
that is in California, it may need another few hours
before one of the  R Core members can add an account for you on
R bugzilla.

Best, Martin Maechler

    > --
    > Ralf Stubner Senior Software Engineer / Trainer

    > daqana GmbH Dortustraße 48 14467 Potsdam

    > T: +49 331 23 61 93 11 F: +49 331 23 61 93 90 M: +49 162
    > 20 91 196 Mail: [hidden email]

    > Sitz: Potsdam Register: AG Potsdam HRB 27966 P Ust.-IdNr.:
    > DE300072622 Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze

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

Re: readLines function with R >= 3.5.0

Simon Urbanek
The MySQL DB on the server died - not sure why. Restarted and it should be ok.


> On May 29, 2018, at 9:17 AM, Martin Maechler <[hidden email]> wrote:
>
>>>>>> Ralf Stubner
>>>>>>    on Tue, 29 May 2018 11:21:28 +0200 writes:
>
>> On 28.05.2018 16:38, Martin Maechler wrote:
>>> Then, I now do think this needs to be dealt with as a bug
>>> (but I'm not delving into fixing it!)
>
>> Ok. Can somebody with write privileges in bugzilla add the
>> bug report? I can also do this myself, if somebody with
>> the required privileges can create a user for me.
>
>> Greetings Ralf
>
> << PS: I get an error message from   https://urldefense.proofpoint.com/v2/url?u=https-3A__bugs.r-2Dproject.org_bugzilla3_&d=DwIDAw&c=LFYZ-o9_HUMeMTSQicvjIg&r=wCho7riGUuXpdLf26yPBz3JyaZUEU6lK6CD_m7-_CA8&m=_xu-PG0SXp13I6r_aA_W4Q0HuUKSUyN_7nwKZIVCqbs&s=L6IIRWOHJ6EkANE7wuzf8l4cEORIkFhwi-SE5i_iqII&e=.
>
> Yes, it is currently "down", i.e., in a wrong state.
> I had alerted the owner of the server a few hours ago, but as
> that is in California, it may need another few hours
> before one of the  R Core members can add an account for you on
> R bugzilla.
>
> Best, Martin Maechler
>
>> --
>> Ralf Stubner Senior Software Engineer / Trainer
>
>> daqana GmbH Dortustraße 48 14467 Potsdam
>
>> T: +49 331 23 61 93 11 F: +49 331 23 61 93 90 M: +49 162
>> 20 91 196 Mail: [hidden email]
>
>> Sitz: Potsdam Register: AG Potsdam HRB 27966 P Ust.-IdNr.:
>> DE300072622 Geschäftsführer: Prof. Dr. Dr. Karl-Kuno Kunze
>

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

use of buffers in sprintf and snprintf

jing hua zhao
In reply to this post by Ralf Stubner
Dear R-developers,

I am struggling with packaging with sprintf and snprintf() as the following WARNINGS from gcc 9.x,

  hap_c.c:380:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
  hap_c.c:392:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]

Essentially, I have

#define MAX_FILENAME_LEN 128
char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN];

...

 snprintf(tempname,sizeof(tempname),"%s.%d", of1name, j);

It looks I could get around with


#define MAX_FILENAME_LEN 128

#define MAX_FILENAME_LEN2 256

char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN2];

...
snprintf(tempname,2*sizeof(tempname)+1,"%s.%d", of1name, j)

It looks a bit waste of resources to me.


Any idea will be greatly appreciated,



Jing Hua

        [[alternative HTML version deleted]]


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

Re: use of buffers in sprintf and snprintf

jing hua zhao
Hi again,

I realised it is useful to replicate the warnings locally without relying on CRAN automatic check; instead of R(-devel)  CMD check --as-cran package_version.tar.gz one can use

R CMD check --configure-args=""

and in my case the WARNINGS were initially given with https://www.stats.ox.ac.uk/pub/bdr/gcc9/README.txt and those specification might as well used in --configure-args above.

Besst regards,


Jing Hua

________________________________
From: R-devel <[hidden email]> on behalf of jing hua zhao <[hidden email]>
Sent: 29 May 2019 15:49
To: [hidden email]
Subject: [Rd] use of buffers in sprintf and snprintf

Dear R-developers,

I am struggling with packaging with sprintf and snprintf() as the following WARNINGS from gcc 9.x,

  hap_c.c:380:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
  hap_c.c:392:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]

Essentially, I have

#define MAX_FILENAME_LEN 128
char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN];

...

 snprintf(tempname,sizeof(tempname),"%s.%d", of1name, j);

It looks I could get around with


#define MAX_FILENAME_LEN 128

#define MAX_FILENAME_LEN2 256

char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN2];

...
snprintf(tempname,2*sizeof(tempname)+1,"%s.%d", of1name, j)

It looks a bit waste of resources to me.


Any idea will be greatly appreciated,



Jing Hua

        [[alternative HTML version deleted]]


        [[alternative HTML version deleted]]

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

Re: use of buffers in sprintf and snprintf

Simon Urbanek
In reply to this post by jing hua zhao
No, that will make it even worse since you'll be declaring a lot more memory that you actually have.

The real problem is that you're ignoring the truncation, so you probably want to use something like

if (snprintf(tempname, sizeof(tempname), "%s.%d", of1name, j) >= sizeof(tempname)) Rf_error("file name is too long");

BTW: most OSes systems have a path limits that are no lower than 256 so you should allow at least as much.

Cheers,
Simon




> On May 29, 2019, at 11:49 AM, jing hua zhao <[hidden email]> wrote:
>
> Dear R-developers,
>
> I am struggling with packaging with sprintf and snprintf() as the following WARNINGS from gcc 9.x,
>
>  hap_c.c:380:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
>  hap_c.c:392:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
>
> Essentially, I have
>
> #define MAX_FILENAME_LEN 128
> char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN];
>
> ...
>
> snprintf(tempname,sizeof(tempname),"%s.%d", of1name, j);
>
> It looks I could get around with
>
>
> #define MAX_FILENAME_LEN 128
>
> #define MAX_FILENAME_LEN2 256
>
> char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN2];
>
> ...
> snprintf(tempname,2*sizeof(tempname)+1,"%s.%d", of1name, j)
>
> It looks a bit waste of resources to me.
>
>
> Any idea will be greatly appreciated,
>
>
>
> Jing Hua
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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: use of buffers in sprintf and snprintf

Henrik Bengtsson-5
On Thu, May 30, 2019 at 7:21 PM Simon Urbanek
<[hidden email]> wrote:
>
> No, that will make it even worse since you'll be declaring a lot more memory that you actually have.
>
> The real problem is that you're ignoring the truncation, so you probably want to use something like
>
> if (snprintf(tempname, sizeof(tempname), "%s.%d", of1name, j) >= sizeof(tempname)) Rf_error("file name is too long");
>
> BTW: most OSes systems have a path limits that are no lower than 256 so you should allow at least as much.

On MS Windows, there's actually a limit of 255 characters, cf.
http://www.aroma-project.org/howtos/UseLongFilenamesOnWindows/
(disclaimer: I'm the author).  Note particularly the comment at the
end:

"Unfortunately it is not a solution to try to use relative instead of
absolute pathnames. The limitation is deep down in the file system
itself and it is the absolute pathname that counts."

Admittedly, it's been several years when I last looked into it, but at
the time when I wrote that I spent lots of time investigating it.

/Henrik

>
> Cheers,
> Simon
>
>
>
>
> > On May 29, 2019, at 11:49 AM, jing hua zhao <[hidden email]> wrote:
> >
> > Dear R-developers,
> >
> > I am struggling with packaging with sprintf and snprintf() as the following WARNINGS from gcc 9.x,
> >
> >  hap_c.c:380:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
> >  hap_c.c:392:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
> >
> > Essentially, I have
> >
> > #define MAX_FILENAME_LEN 128
> > char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN];
> >
> > ...
> >
> > snprintf(tempname,sizeof(tempname),"%s.%d", of1name, j);
> >
> > It looks I could get around with
> >
> >
> > #define MAX_FILENAME_LEN 128
> >
> > #define MAX_FILENAME_LEN2 256
> >
> > char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN2];
> >
> > ...
> > snprintf(tempname,2*sizeof(tempname)+1,"%s.%d", of1name, j)
> >
> > It looks a bit waste of resources to me.
> >
> >
> > Any idea will be greatly appreciated,
> >
> >
> >
> > Jing Hua
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [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: use of buffers in sprintf and snprintf

Gábor Csárdi
On Fri, May 31, 2019 at 3:39 AM Henrik Bengtsson
<[hidden email]> wrote:
[...]

> On MS Windows, there's actually a limit of 255 characters, cf.
> http://www.aroma-project.org/howtos/UseLongFilenamesOnWindows/
> (disclaimer: I'm the author).  Note particularly the comment at the
> end:
>
> "Unfortunately it is not a solution to try to use relative instead of
> absolute pathnames. The limitation is deep down in the file system
> itself and it is the absolute pathname that counts."
>
> Admittedly, it's been several years when I last looked into it, but at
> the time when I wrote that I spent lots of time investigating it.

AFAICT NTFS supports path names up to 32,767 characters. The Unicode
Win API functions also support these, or you can just prefix the paths
with \\?\ to support them. I don't know if there is anything in R that
would prevent using the \\?\ prefix but just doing a file() on them
seems to work fine.

Gabor


> /Henrik
>
> >
> > Cheers,
> > Simon
> >
> >
> >
> >
> > > On May 29, 2019, at 11:49 AM, jing hua zhao <[hidden email]> wrote:
> > >
> > > Dear R-developers,
> > >
> > > I am struggling with packaging with sprintf and snprintf() as the following WARNINGS from gcc 9.x,
> > >
> > >  hap_c.c:380:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
> > >  hap_c.c:392:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
> > >
> > > Essentially, I have
> > >
> > > #define MAX_FILENAME_LEN 128
> > > char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN];
> > >
> > > ...
> > >
> > > snprintf(tempname,sizeof(tempname),"%s.%d", of1name, j);
> > >
> > > It looks I could get around with
> > >
> > >
> > > #define MAX_FILENAME_LEN 128
> > >
> > > #define MAX_FILENAME_LEN2 256
> > >
> > > char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN2];
> > >
> > > ...
> > > snprintf(tempname,2*sizeof(tempname)+1,"%s.%d", of1name, j)
> > >
> > > It looks a bit waste of resources to me.
> > >
> > >
> > > Any idea will be greatly appreciated,
> > >
> > >
> > >
> > > Jing Hua
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [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
|

Calculation of e^{z^2/2} for a normal deviate z

jing hua zhao
In reply to this post by jing hua zhao
Dear R-developers,

I am keen to calculate exp(z*z/2) with z=qnorm(p/2) and p is very small. I wonder if anyone has experience with this?

Thanks very much in advance,


Jing Hua

        [[alternative HTML version deleted]]

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

Re: Calculation of e^{z^2/2} for a normal deviate z

Rui Barradas
Hello,

Well, try it:

p <- .Machine$double.eps^seq(0.5, 1, by = 0.05)
z <- qnorm(p/2)

pnorm(z)
# [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
# [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
#[11] 1.110223e-16
p/2
# [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
# [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
#[11] 1.110223e-16

exp(z*z/2)
# [1] 9.184907e+06 5.301421e+07 3.073154e+08 1.787931e+09 1.043417e+10
# [6] 6.105491e+10 3.580873e+11 2.104460e+12 1.239008e+13 7.306423e+13
#[11] 4.314798e+14


p is the smallest possible such that 1 + p != 1 and I couldn't find
anything to worry about.


R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 19.04

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
  [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
  [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
  [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
[7] base

other attached packages:

[many packages loaded]


Hope this helps,

Rui Barradas

Às 15:24 de 21/06/19, jing hua zhao escreveu:

> Dear R-developers,
>
> I am keen to calculate exp(z*z/2) with z=qnorm(p/2) and p is very small. I wonder if anyone has experience with this?
>
> Thanks very much in advance,
>
>
> Jing Hua
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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: Calculation of e^{z^2/2} for a normal deviate z

jing hua zhao
Dear Rui,

Thanks for your quick reply -- this allows me to see the bottom of this. I was hoping we could have a handle of those p in genmoics such as 1e-300 or smaller.

Best wishes,


Jing Hua

________________________________
From: Rui Barradas <[hidden email]>
Sent: 21 June 2019 15:03
To: jing hua zhao; [hidden email]
Subject: Re: [Rd] Calculation of e^{z^2/2} for a normal deviate z

Hello,

Well, try it:

p <- .Machine$double.eps^seq(0.5, 1, by = 0.05)
z <- qnorm(p/2)

pnorm(z)
# [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
# [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
#[11] 1.110223e-16
p/2
# [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
# [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
#[11] 1.110223e-16

exp(z*z/2)
# [1] 9.184907e+06 5.301421e+07 3.073154e+08 1.787931e+09 1.043417e+10
# [6] 6.105491e+10 3.580873e+11 2.104460e+12 1.239008e+13 7.306423e+13
#[11] 4.314798e+14


p is the smallest possible such that 1 + p != 1 and I couldn't find
anything to worry about.


R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 19.04

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
  [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
  [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
  [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
[7] base

other attached packages:

[many packages loaded]


Hope this helps,

Rui Barradas

�s 15:24 de 21/06/19, jing hua zhao escreveu:

> Dear R-developers,
>
> I am keen to calculate exp(z*z/2) with z=qnorm(p/2) and p is very small. I wonder if anyone has experience with this?
>
> Thanks very much in advance,
>
>
> Jing Hua
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
        [[alternative HTML version deleted]]


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

Re: Calculation of e^{z^2/2} for a normal deviate z

Rui Barradas
Hello,

Sorry, my mistake, I grossly misunderstood the question.

qnorm(1e-300)
#[1] -37.0471

Anyway, you cannot go much smaller.

p <- 10^seq(-300, -400, by = -10)
z <- qnorm(p/2)
exp(z*z/2)


Hope this helps,

Rui Barradas

Às 16:11 de 21/06/19, jing hua zhao escreveu:

> Dear Rui,
>
> Thanks for your quick reply -- this allows me to see the bottom of this.
> I was hoping we could have a handle of those p in genmoics such as
> 1e-300 or smaller.
>
> Best wishes,
>
>
> Jing Hua
>
> ------------------------------------------------------------------------
> *From:* Rui Barradas <[hidden email]>
> *Sent:* 21 June 2019 15:03
> *To:* jing hua zhao; [hidden email]
> *Subject:* Re: [Rd] Calculation of e^{z^2/2} for a normal deviate z
> Hello,
>
> Well, try it:
>
> p <- .Machine$double.eps^seq(0.5, 1, by = 0.05)
> z <- qnorm(p/2)
>
> pnorm(z)
> # [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
> # [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
> #[11] 1.110223e-16
> p/2
> # [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
> # [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
> #[11] 1.110223e-16
>
> exp(z*z/2)
> # [1] 9.184907e+06 5.301421e+07 3.073154e+08 1.787931e+09 1.043417e+10
> # [6] 6.105491e+10 3.580873e+11 2.104460e+12 1.239008e+13 7.306423e+13
> #[11] 4.314798e+14
>
>
> p is the smallest possible such that 1 + p != 1 and I couldn't find
> anything to worry about.
>
>
> R version 3.6.0 (2019-04-26)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 19.04
>
> Matrix products: default
> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0
>
> locale:
>    [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
>    [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
>    [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
>    [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
>    [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods
> [7] base
>
> other attached packages:
>
> [many packages loaded]
>
>
> Hope this helps,
>
> Rui Barradas
>
> Às 15:24 de 21/06/19, jing hua zhao escreveu:
>> Dear R-developers,
>>
>> I am keen to calculate exp(z*z/2) with z=qnorm(p/2) and p is very small. I wonder if anyone has experience with this?
>>
>> Thanks very much in advance,
>>
>>
>> Jing Hua
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [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: Calculation of e^{z^2/2} for a normal deviate z

Christophe Dutang1
In reply to this post by jing hua zhao
You should take a look at https://CRAN.R-project.org/package=Rmpfr

Regards, Christophe

------------------------------------------------------------
Christophe Dutang
CEREMADE, Univ. Paris Dauphine, PSL Univ., France
Web : http://dutangc.free.fr
------------------------------------------------------------

> Le 21 juin 2019 à 17:11, jing hua zhao <[hidden email]> a écrit :
>
> Dear Rui,
>
> Thanks for your quick reply -- this allows me to see the bottom of this. I was hoping we could have a handle of those p in genmoics such as 1e-300 or smaller.
>
> Best wishes,
>
>
> Jing Hua
>
> ________________________________
> From: Rui Barradas <[hidden email]>
> Sent: 21 June 2019 15:03
> To: jing hua zhao; [hidden email]
> Subject: Re: [Rd] Calculation of e^{z^2/2} for a normal deviate z
>
> Hello,
>
> Well, try it:
>
> p <- .Machine$double.eps^seq(0.5, 1, by = 0.05)
> z <- qnorm(p/2)
>
> pnorm(z)
> # [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
> # [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
> #[11] 1.110223e-16
> p/2
> # [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
> # [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
> #[11] 1.110223e-16
>
> exp(z*z/2)
> # [1] 9.184907e+06 5.301421e+07 3.073154e+08 1.787931e+09 1.043417e+10
> # [6] 6.105491e+10 3.580873e+11 2.104460e+12 1.239008e+13 7.306423e+13
> #[11] 4.314798e+14
>
>
> p is the smallest possible such that 1 + p != 1 and I couldn't find
> anything to worry about.
>
>
> R version 3.6.0 (2019-04-26)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 19.04
>
> Matrix products: default
> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0
>
> locale:
>  [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
>  [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
>  [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
>  [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods
> [7] base
>
> other attached packages:
>
> [many packages loaded]
>
>
> Hope this helps,
>
> Rui Barradas
>
> �s 15:24 de 21/06/19, jing hua zhao escreveu:
>> Dear R-developers,
>>
>> I am keen to calculate exp(z*z/2) with z=qnorm(p/2) and p is very small. I wonder if anyone has experience with this?
>>
>> Thanks very much in advance,
>>
>>
>> Jing Hua
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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: Calculation of e^{z^2/2} for a normal deviate z

Peter Dalgaard-2
In reply to this post by jing hua zhao
You may want to look into using the log option to qnorm

e.g., in round figures:

> log(1e-300)
[1] -690.7755
> qnorm(-691, log=TRUE)
[1] -37.05315
> exp(37^2/2)
[1] 1.881797e+297
> exp(-37^2/2)
[1] 5.314068e-298

Notice that floating point representation cuts out at 1e+/-308 or so. If you want to go outside that range, you may need explicit manipulation of the log values. qnorm() itself seems quite happy with much smaller values:

> qnorm(-5000, log=TRUE)
[1] -99.94475

-pd

> On 21 Jun 2019, at 17:11 , jing hua zhao <[hidden email]> wrote:
>
> Dear Rui,
>
> Thanks for your quick reply -- this allows me to see the bottom of this. I was hoping we could have a handle of those p in genmoics such as 1e-300 or smaller.
>
> Best wishes,
>
>
> Jing Hua
>
> ________________________________
> From: Rui Barradas <[hidden email]>
> Sent: 21 June 2019 15:03
> To: jing hua zhao; [hidden email]
> Subject: Re: [Rd] Calculation of e^{z^2/2} for a normal deviate z
>
> Hello,
>
> Well, try it:
>
> p <- .Machine$double.eps^seq(0.5, 1, by = 0.05)
> z <- qnorm(p/2)
>
> pnorm(z)
> # [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
> # [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
> #[11] 1.110223e-16
> p/2
> # [1] 7.450581e-09 1.228888e-09 2.026908e-10 3.343152e-11 5.514145e-12
> # [6] 9.094947e-13 1.500107e-13 2.474254e-14 4.080996e-15 6.731134e-16
> #[11] 1.110223e-16
>
> exp(z*z/2)
> # [1] 9.184907e+06 5.301421e+07 3.073154e+08 1.787931e+09 1.043417e+10
> # [6] 6.105491e+10 3.580873e+11 2.104460e+12 1.239008e+13 7.306423e+13
> #[11] 4.314798e+14
>
>
> p is the smallest possible such that 1 + p != 1 and I couldn't find
> anything to worry about.
>
>
> R version 3.6.0 (2019-04-26)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 19.04
>
> Matrix products: default
> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0
>
> locale:
>  [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
>  [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
>  [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
>  [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
> [11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods
> [7] base
>
> other attached packages:
>
> [many packages loaded]
>
>
> Hope this helps,
>
> Rui Barradas
>
> �s 15:24 de 21/06/19, jing hua zhao escreveu:
>> Dear R-developers,
>>
>> I am keen to calculate exp(z*z/2) with z=qnorm(p/2) and p is very small. I wonder if anyone has experience with this?
>>
>> Thanks very much in advance,
>>
>>
>> Jing Hua
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

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