Re: [Rd] How to debug an R package (with C code)

2014-01-17 Thread Sandip Nandi
Hi ,

The video may help you

http://vimeo.com/11937905

Thanks,
Sandip


On Thu, Jan 16, 2014 at 4:17 PM, Ma, Saisai - maysy020 <
saisai...@mymail.unisa.edu.au> wrote:

> Hi all,
>
> Sorry to bother you.
>
> I am a beginner in R programming. I have encountered some problems when I
> modified a R package with C code.
> I would like to modified the C code within the R package, and want to
> debug the C program. But I have no idea how to debug this kind of R package.
>
> In the package, a R file named aaa.R use the interface .call(bbb.c, ...)
> to call the C code named bbb.c.
> And what I did is that I resourced aaa.R and ran it in R Stdio again,
> after modifying bbb.c. But there is nothing happened.
> I am very sure that the all the path of R file and C file is correct. And
> I am using Windows operating system.
>
> Could anyone give me a few pointers on how to debug R packages (or
> extensions with C++/C code)? If possible, Could you please give me an
> simple example or some command lines?
>
>
> Regards,
> Saisai
>
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to debug an R package (with C code)

2014-01-17 Thread Gabriel Becker
Saisai,

I haven't watched the specific video Sandip posted, but from the title it
looks to be correct. Running R with/in gdb is the correct way to debug C
code in R packages.

One thing to watch out for: The default compiler flags for C/C++ code in R
packages call for substantial optimization (I believe it's '-O2'). This
mess with the execution order of some instructions, making step by step
debugging annoying. Also, it will optimize out values that may be important
to your debugging. To fix this you'll need to look into how Makevars files
work, and specifically how to set/override compiler flags in them.

There were some deeper issues with what you asked, though. Sourcing an R
file will have no effect on a package in the namespace at all unless you
are using something like ESS's developer mode which sources the file into
the package namespace. Furthermore, C code included in R packages is only
compiled at installation from source or binary build time.

Even if you are using a developer mode, sourcing an R file will have no
effect on a) the shared object library (DLL/so/dylib) available in the R
session, nor which shared objects are associated with the package. There
are ways to unload shared libraries within an R session, but personally I
tend to avoid them. The best way of being absolutely sure that the right
shared library is getting used is to reinstall the package, then close the
R session and restart.

I would encourage you to look into how R compiles and interacts with native
code in and out of packages before trying to tackle something like this
directly. It's all in the Writing R extensions manual here:
http://cran.r-project.org/doc/manuals/R-exts.html

Hope that helps,
~G


On Thu, Jan 16, 2014 at 8:04 PM, Sandip Nandi  wrote:

> Hi ,
>
> The video may help you
>
> http://vimeo.com/11937905
>
> Thanks,
> Sandip
>
>
> On Thu, Jan 16, 2014 at 4:17 PM, Ma, Saisai - maysy020 <
> saisai...@mymail.unisa.edu.au> wrote:
>
> > Hi all,
> >
> > Sorry to bother you.
> >
> > I am a beginner in R programming. I have encountered some problems when I
> > modified a R package with C code.
> > I would like to modified the C code within the R package, and want to
> > debug the C program. But I have no idea how to debug this kind of R
> package.
> >
> > In the package, a R file named aaa.R use the interface .call(bbb.c, ...)
> > to call the C code named bbb.c.
> > And what I did is that I resourced aaa.R and ran it in R Stdio again,
> > after modifying bbb.c. But there is nothing happened.
> > I am very sure that the all the path of R file and C file is correct. And
> > I am using Windows operating system.
> >
> > Could anyone give me a few pointers on how to debug R packages (or
> > extensions with C++/C code)? If possible, Could you please give me an
> > simple example or some command lines?
> >
> >
> > Regards,
> > Saisai
> >
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Gabriel Becker
Graduate Student
Statistics Department
University of California, Davis

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to debug an R package (with C code)

2014-01-17 Thread Peter Meilstrup
On Fri, Jan 17, 2014 at 12:43 AM, Gabriel Becker  wrote:
> There were some deeper issues with what you asked, though. Sourcing an R
> file will have no effect on a package in the namespace at all unless you
> are using something like ESS's developer mode which sources the file into
> the package namespace. Furthermore, C code included in R packages is only
> compiled at installation from source or binary build time.
>
> Even if you are using a developer mode, sourcing an R file will have no
> effect on a) the shared object library (DLL/so/dylib) available in the R
> session, nor which shared objects are associated with the package. There
> are ways to unload shared libraries within an R session, but personally I
> tend to avoid them. The best way of being absolutely sure that the right
> shared library is getting used is to reinstall the package, then close the
> R session and restart.

I value having a short turnaround time in debugging over such absolute
surety, so I would recommend using load_all from Hadley's devtools
package, which is usually able to recompile and reload the library
without restarting R. I think the OP indicated that they are using
RStudio, which should have a way to drive devtools (still, "source"
would be the wrong command.)

Peter

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] file.exists does not like path names ending in /

2014-01-17 Thread Uwe Ligges



On 17.01.2014 06:10, Gabor Grothendieck wrote:

If a path name ends in slash then file.exists says it does not exist.
I would have expected these to all return TRUE.


file.exists("/Program Files")

[1] TRUE

file.exists("/Program Files/")

[1] FALSE

file.exists(normalizePath("/Program Files/"))

[1] FALSE

R.version.string

[1] "R version 3.0.2 Patched (2013-11-25 r64299)"

I am using Windows 8.1 .




See ?file.exists:

"(However, directory names must not include a trailing backslash or 
slash on Windows.)"


Note also that  "/Program Files" is not safe anyway since it is relative 
to the current drive.


Best,
Uwe Ligges

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] file.exists does not like path names ending in /

2014-01-17 Thread Martin Maechler
> Gabor Grothendieck 
> on Fri, 17 Jan 2014 00:10:43 -0500 writes:

> If a path name ends in slash then file.exists says it does
> not exist.  I would have expected these to all return
> TRUE.

>> file.exists("/Program Files")
> [1] TRUE
>> file.exists("/Program Files/")
> [1] FALSE
>> file.exists(normalizePath("/Program Files/"))
> [1] FALSE
>> R.version.string
> [1] "R version 3.0.2 Patched (2013-11-25 r64299)"

I would also have expected all these to work,
but that is only because I do not use Windows;
indeed, for me  (Linux Fedora 19, but I'm pretty sure *any*
version):

  > dir.create("/tmp/foo bar")
  > normalizePath("/tmp/foo bar/")
  [1] "/tmp/foo bar"
  > file.exists("/tmp/foo bar/")
  [1] TRUE
  > file.exists(normalizePath("/tmp/foo bar/"))
  [1] TRUE
  > 


> I am using Windows 8.1 .

poor you  ;-)   yes, don't take it personally

Last but not least   ?file.exists   in its  'Details' section
mentions several times how Windows behaves differently from the 
civilized world (:-)
notably that it also says 

  (However,
 directory names must not include a trailing backslash or slash on
 Windows.)

So, all seems as documented, but unfortunately your (and most
probably not only yours !!) expectations are different.
I agree that this is quite unfortunate, and we all would be
happy if OSes were consistent here.

I wonder if R couldn't help you (and many others) better to
gloss over these OS differences in a helpful way.
This list may be a good place to discuss such proposals ...

Martin

-- 
Martin Maechler ETH Zurich -- and R Core Team

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] file.exists does not like path names ending in /

2014-01-17 Thread Gabor Grothendieck
On Fri, Jan 17, 2014 at 6:16 AM, Martin Maechler
 wrote:
>> Gabor Grothendieck 
>> on Fri, 17 Jan 2014 00:10:43 -0500 writes:
>
> > If a path name ends in slash then file.exists says it does
> > not exist.  I would have expected these to all return
> > TRUE.
>
> >> file.exists("/Program Files")
> > [1] TRUE
> >> file.exists("/Program Files/")
> > [1] FALSE
> >> file.exists(normalizePath("/Program Files/"))
> > [1] FALSE
> >> R.version.string
> > [1] "R version 3.0.2 Patched (2013-11-25 r64299)"
>
> I would also have expected all these to work,
> but that is only because I do not use Windows;
> indeed, for me  (Linux Fedora 19, but I'm pretty sure *any*
> version):
>
>   > dir.create("/tmp/foo bar")
>   > normalizePath("/tmp/foo bar/")
>   [1] "/tmp/foo bar"
>   > file.exists("/tmp/foo bar/")
>   [1] TRUE
>   > file.exists(normalizePath("/tmp/foo bar/"))
>   [1] TRUE
>   >
>
>
> > I am using Windows 8.1 .
>
> poor you  ;-)   yes, don't take it personally
>
> Last but not least   ?file.exists   in its  'Details' section
> mentions several times how Windows behaves differently from the
> civilized world (:-)
> notably that it also says
>
>   (However,
>  directory names must not include a trailing backslash or slash on
>  Windows.)
>
> So, all seems as documented, but unfortunately your (and most
> probably not only yours !!) expectations are different.
> I agree that this is quite unfortunate, and we all would be
> happy if OSes were consistent here.
>
> I wonder if R couldn't help you (and many others) better to
> gloss over these OS differences in a helpful way.
> This list may be a good place to discuss such proposals ...
>

I do find that sometimes I have to deal with paths that I did not
create that end in / or \.  At the moment I am using this to avoid the
problem:

File.exists <- function(x) {
   if (.Platform$OS == "windows" && grepl("[/\\]$", x)) {
   file.exists(dirname(x))
   } else file.exists(x)
}

but it would be nice if that could be done by file.exists itself.


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to debug an R package (with C code)

2014-01-17 Thread Gábor Csárdi
Small note: if you are using a recent version of OSX, then gdb is
essentially useless, but you can use llvm, which is nicer in many respects,
anyway.

Gabor


On Fri, Jan 17, 2014 at 3:43 AM, Gabriel Becker wrote:

> Saisai,
>
> I haven't watched the specific video Sandip posted, but from the title it
> looks to be correct. Running R with/in gdb is the correct way to debug C
> code in R packages.
>
> One thing to watch out for: The default compiler flags for C/C++ code in R
> packages call for substantial optimization (I believe it's '-O2'). This
> mess with the execution order of some instructions, making step by step
> debugging annoying. Also, it will optimize out values that may be important
> to your debugging. To fix this you'll need to look into how Makevars files
> work, and specifically how to set/override compiler flags in them.
>
> There were some deeper issues with what you asked, though. Sourcing an R
> file will have no effect on a package in the namespace at all unless you
> are using something like ESS's developer mode which sources the file into
> the package namespace. Furthermore, C code included in R packages is only
> compiled at installation from source or binary build time.
>
> Even if you are using a developer mode, sourcing an R file will have no
> effect on a) the shared object library (DLL/so/dylib) available in the R
> session, nor which shared objects are associated with the package. There
> are ways to unload shared libraries within an R session, but personally I
> tend to avoid them. The best way of being absolutely sure that the right
> shared library is getting used is to reinstall the package, then close the
> R session and restart.
>
> I would encourage you to look into how R compiles and interacts with native
> code in and out of packages before trying to tackle something like this
> directly. It's all in the Writing R extensions manual here:
> http://cran.r-project.org/doc/manuals/R-exts.html
>
> Hope that helps,
> ~G
>
>
> On Thu, Jan 16, 2014 at 8:04 PM, Sandip Nandi 
> wrote:
>
> > Hi ,
> >
> > The video may help you
> >
> > http://vimeo.com/11937905
> >
> > Thanks,
> > Sandip
> >
> >
> > On Thu, Jan 16, 2014 at 4:17 PM, Ma, Saisai - maysy020 <
> > saisai...@mymail.unisa.edu.au> wrote:
> >
> > > Hi all,
> > >
> > > Sorry to bother you.
> > >
> > > I am a beginner in R programming. I have encountered some problems
> when I
> > > modified a R package with C code.
> > > I would like to modified the C code within the R package, and want to
> > > debug the C program. But I have no idea how to debug this kind of R
> > package.
> > >
> > > In the package, a R file named aaa.R use the interface .call(bbb.c,
> ...)
> > > to call the C code named bbb.c.
> > > And what I did is that I resourced aaa.R and ran it in R Stdio again,
> > > after modifying bbb.c. But there is nothing happened.
> > > I am very sure that the all the path of R file and C file is correct.
> And
> > > I am using Windows operating system.
> > >
> > > Could anyone give me a few pointers on how to debug R packages (or
> > > extensions with C++/C code)? If possible, Could you please give me an
> > > simple example or some command lines?
> > >
> > >
> > > Regards,
> > > Saisai
> > >
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > __
> > > R-devel@r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-devel
> > >
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
>
>
> --
> Gabriel Becker
> Graduate Student
> Statistics Department
> University of California, Davis
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] How to debug an R package (with C code)

2014-01-17 Thread Hadley Wickham
>> Even if you are using a developer mode, sourcing an R file will have no
>> effect on a) the shared object library (DLL/so/dylib) available in the R
>> session, nor which shared objects are associated with the package. There
>> are ways to unload shared libraries within an R session, but personally I
>> tend to avoid them. The best way of being absolutely sure that the right
>> shared library is getting used is to reinstall the package, then close the
>> R session and restart.
>
> I value having a short turnaround time in debugging over such absolute
> surety, so I would recommend using load_all from Hadley's devtools
> package, which is usually able to recompile and reload the library
> without restarting R. I think the OP indicated that they are using
> RStudio, which should have a way to drive devtools (still, "source"
> would be the wrong command.)

In Rstudio, devtools::load_all() is bound to Cmd + shift + L. Rstudio
also provides Build and reload, which builds the package, restarts R
and reloads it.  This is a little slower than load_all(), but also a
little more accurate so can come in handy from time-to-time.

Hadley

-- 
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] file.exists does not like path names ending in /

2014-01-17 Thread Kirill Müller

On 01/17/2014 02:56 PM, Gabor Grothendieck wrote:

At the moment I am using this to avoid the
problem:

File.exists <- function(x) {
if (.Platform$OS == "windows" && grepl("[/\\]$", x)) {
file.exists(dirname(x))
} else file.exists(x)
}

but it would be nice if that could be done by file.exists itself.
I think that ignoring a terminal slash/backslash on Windows would do no 
harm: It would improve consistency between platforms, and perhaps nobody 
really relies on the current behavior. Would shorten the documentation, too.



-Kirill

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] file.exists does not like path names ending in /

2014-01-17 Thread William Dunlap
> I think that ignoring a terminal slash/backslash on Windows would do no
> harm:

Windows makes a distinction between "C:" and "C:/": the former is
not a file (or directory) and the latter is.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: r-devel-boun...@r-project.org [mailto:r-devel-boun...@r-project.org] On 
> Behalf
> Of Kirill Müller
> Sent: Friday, January 17, 2014 9:26 AM
> To: r-devel@r-project.org
> Subject: Re: [Rd] file.exists does not like path names ending in /
> 
> On 01/17/2014 02:56 PM, Gabor Grothendieck wrote:
> > At the moment I am using this to avoid the
> > problem:
> >
> > File.exists <- function(x) {
> > if (.Platform$OS == "windows" && grepl("[/\\]$", x)) {
> > file.exists(dirname(x))
> > } else file.exists(x)
> > }
> >
> > but it would be nice if that could be done by file.exists itself.
> I think that ignoring a terminal slash/backslash on Windows would do no
> harm: It would improve consistency between platforms, and perhaps nobody
> really relies on the current behavior. Would shorten the documentation, too.
> 
> 
> -Kirill
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] rexp, different results on 32/64 bit

2014-01-17 Thread Gábor Csárdi
Hi all,

I just run into this today. Apparently rexp() sometimes gives different
slightly results for the same seed on 32 bit and 64 bit machines. runif()
is the same for both, so the problem seems to be in rexp().

64 bit Linux is the same as 64 bit OSX, and R-devel gives the same results
as R-3.0.2.

Best,
Gabor

# -
> options(digits=22) ; set.seed(9) ; rexp(1, 5)
[1] 0.2806184054728815824298
> options(digits=22) ; set.seed(9) ; runif(1)
[1] 0.2216013972647488117218
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: i686-pc-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=en_US.utf8   LC_NUMERIC=C
 [3] LC_TIME=en_US.utf8LC_COLLATE=en_US.utf8
 [5] LC_MONETARY=en_US.utf8LC_MESSAGES=en_US.utf8
 [7] LC_PAPER=en_US.utf8   LC_NAME=C
 [9] LC_ADDRESS=C  LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C

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

# -
> options(digits=22) ; set.seed(9) ; rexp(1, 5)
[1] 0.2806184054728815269186
> options(digits=22) ; set.seed(9) ; runif(1)
[1] 0.2216013972647488117218
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] file.exists does not like path names ending in /

2014-01-17 Thread Kirill Müller

On 01/17/2014 07:35 PM, William Dunlap wrote:

I think that ignoring a terminal slash/backslash on Windows would do no
>harm:

Windows makes a distinction between "C:" and "C:/": the former is
not a file (or directory) and the latter is.
But, according to the documentation, neither would be currently detected 
by file.exists, while the latter is a directory, as you said, and should 
be detected as such.



-Kirill

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] file.exists does not like path names ending in /

2014-01-17 Thread Henrik Bengtsson
1. This is one of the reasons for isFile() and isDirectory() in
R.utils.  I add "workarounds" as new ones are discovered.

2. This works (and also tests for existence of a *directory*):

> file_test("-d", "C:/Program Files")
[1] TRUE
> file_test("-d", "C:/Program Files/")
[1] TRUE

3a. "C:" and "C:/" (at the root) are rather special border cases and
they should mean difference things (just as they do at the Windows
command prompt), e.g.

> setwd("C:/Program Files/")
> getwd()
[1] "C:/Program Files"
> setwd("T:/tmp/")
> getwd()
[1] "T:/tmp"
> setwd("C:")
> getwd()
[1] "C:/Program Files"
> setwd("T:")
> getwd()
[1] "T:/tmp"

(A feature rarely used, but correct.)

3b. Contrary to OP's example, file.exists() works correctly on both
these cases (at least back to R 2.15.3), e.g.

> file.exists("C:")
[1] FALSE
> file.exists("C:/")
[1] TRUE

3c. Related: An inconsistency in file.info("C:/") with workaround
file.info("C:/.") was fixed as of R 3.0.2, cf.
https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=15302

/Henrik


On Fri, Jan 17, 2014 at 11:01 AM, Kirill Müller
 wrote:
> On 01/17/2014 07:35 PM, William Dunlap wrote:
>>>
>>> I think that ignoring a terminal slash/backslash on Windows would do no
>>> >harm:
>>
>> Windows makes a distinction between "C:" and "C:/": the former is
>> not a file (or directory) and the latter is.
>
> But, according to the documentation, neither would be currently detected by
> file.exists, while the latter is a directory, as you said, and should be
> detected as such.
>
>
>
> -Kirill
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] wrong MD5 checksums in R 3.0.2

2014-01-17 Thread Tal Galili
Dear R-devel members,

An R user recently
reportedthat
when running MD5
checksums on a recent R installation (R 3.0.2), he gets the following error
that the files
‘bin/i386/Rblas.dll’, ‘bin/i386/Rlapack.dll’, ‘bin/x64/Rblas.dll’,
‘bin/x64/Rlapack.dll’, ‘etc/repositories’ have the wrong MD5 checksums.

Could you please let me know if this makes sense?

Thanks,

Tal



Contact
Details:---
Contact me: tal.gal...@gmail.com |
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
--

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel