Re: [Rd] [R] Use of .Fortran

2010-06-18 Thread Prof Brian Ripley

On Sat, 19 Jun 2010, David Scott wrote:

I have no experience with incorporating Fortran code and am probably doing 
something pretty stupid.


Surely you saw in the posting guide that R-help is not the place for 
questions about C, C++, Fortran code?  Diverting to R-devel.



I want to use the following Fortran subroutine (not written by me) in the


Well, it is not Fortran 77 but Fortran 95, and so needs to be given a 
.f95 extension to be sure to work.



file SSFcoef.f

 subroutine SSFcoef(nmax,nu,A,nrowA,ncolA)
 implicit double precision(a-h,o-z)
 implicit integer (i-n)
 integer l,i,nmax
 double precision nu,A(0:nmax,0:nmax)
 A(0,0) = 1D0
 do l=1,nmax
do i=1,l-1
A(l,i) = (-nu+i+l-1D0)*A(l-1,i)+A(l-1,i-1)
end do
A(l,0) = (-nu+l-1D0)*A(l-1,0)
A(l,l) = 1D0
 end do
 return
 end


I created a dll (this is windows) using R CMD SHLIB SSFcoef.f

Then my R code is:

### Load the compiled shared library in.
dyn.load(SSFcoef.dll)

### Write a function that calls the Fortran subroutine
SSFcoef - function(nmax, nu){
 .Fortran(SSFcoef,
  as.integer(nmax),
  as.integer(nu)
  )$A
}


That does not match.  nrowA and ncolA are unused, so you need
SSFcoef - function(nmax, nu){
  .Fortran(SSFcoef,
   as.integer(nmax),
   as.integer(nu),
   A = matrix(0, nmax+1, nmax+1),
   0L, 0L)$A
}




SSFcoef(10,2)

which when run gives


SSFcoef(10,2)

NULL

I am pretty sure the problem is that I am not dealing with the matrix A 
properly. I also tried this on linux and got a segfault.


Can anyone supply the appropriate modification to my call (and possibly to 
the subroutine) to make this work?


David Scott


--
_
David Scott Department of Statistics
The University of Auckland, PB 92019
Auckland 1142,NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email:  d.sc...@auckland.ac.nz,  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics

__
r-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

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


Re: [Rd] CHM help does not find help docs in package stats

2010-06-18 Thread Uwe Ligges



On 17.06.2010 09:19, Thaler,Thorn,LAUSANNE,Applied Mathematics wrote:

Indeed, the R version is 2.9.2. But the package was built on the very
same system. Besides, another package built with the same version of R
but on another machine does not show this nasty behavior. Upgrading,
however, could pose more problems as it solves, since even if I could
upgrade locally, it would be a mess to assure that all colleagues
upgrade as well.

Anyways, even if the chm format is not supported any more by current
versions of R, it should work with older ones, right? So, what can I do
additionally to get to the bottom of this problem?




During the last years, Mircosoft patched parts of the chm system by 
disallowing different features by default. It may see some result of 
these patches. Anyway, hard to say what is going on without seeing your 
binary package, your system setup, the libraries where you have 
installed you package and the otehrs, knowing if these are network paths 
or local storage, .


Uwe Ligges



Thanks + BR,

Thorn

-Original Message-
From: Uwe Ligges [mailto:lig...@statistik.tu-dortmund.de]
Sent: samedi 12 juin 2010 18:21
To: Thaler,Thorn,LAUSANNE,Applied Mathematics
Cc: r-devel@r-project.org
Subject: Re: [Rd] CHM help does not find help docs in package stats

Which version of R is this?
CHM help is R  2.10.0. All versions thereafter do not support compiled
html help anymore.

Please upgrade your version of R and try one of the currently supported
help formats.

Best wishes,
Uwe Ligges



On 11.06.2010 12:06, Thaler, Thorn, LAUSANNE, Applied Mathematics wrote:

Hi all,

currently I'm working on an R package bundling some frequently used
functions. When I load my package and type

?one_of_my_functions

I get the particular help file. If I try to get help on another
function, which is part of package stats (prcomp say), I get This
program cannot display the webpage. A help on ?mean does, however,

work

as it opens a new window showing the help on mean.

There is another peculiarity, if it happens that I requested a help

file

for a function in the package stats before I used the help on one of

my

functions, everything works out fine.

Does anybody know how I can get rid of this annoying behavior? It has

to

have something to do with my particular package, since everything

works

as expected with another package of mine.

I override one of the functions of stats (biplot.default), which is,
however, not exported and resides within my namespace. Could that be

the

reason?

Any help highly appreciated. Thanks + BR,

Thorn

__
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] C Interface

2010-06-18 Thread michael meyer
Greetings,

I am trying to call simple C-code from R.
I am on Windows XP with RTools installed.

The C-function is

#include R.h
#include Rinternals.h
#include Rmath.h
#include Rdefines.h

// prevent name mangling
extern C {

SEXP __cdecl test(SEXP s){

  SEXP result;
  PROTECT(result = NEW_NUMERIC(1));
  double* ptr=NUMERIC_POINTER(result);
  double t = *REAL(s);
  double u = t-floor(t)-0.5;
  if(u0) *ptr=-1+4*u; else *ptr=-1-4*u;
  Rprintf(The value is %f, *ptr);
  UNPROTECT(1);
  return result;
}

};

It is compiled with

R CMD SHLIB OrthoFunctions.c

with flag

MAKEFLAGS=CC=g++


However when I call this code from R with

test - function(t){
  .Call(test,t)
}
dyn.load(./OrthoFunctions.dll)
test(0)
dyn.unload(./OrthoFunctions.dll)

then R crashes.

If I compile with the default flags (no extern C, no __cdecl) I get an
error message about an undefined reference to __gxx_personality_v0:

C:\...R CMD SHLIB OrthoFunctions.c
C:/Programme/R/R-2.10.1/etc/Makeconf:151: warning: overriding commands for
target `.c.o'
C:/Programme/R/R-2.10.1/etc/Makeconf:142: warning: ignoring old commands for
target `.c.o'
C:/Programme/R/R-2.10.1/etc/Makeconf:159: warning: overriding commands for
target `.c.d'
C:/Programme/R/R-2.10.1/etc/Makeconf:144: warning: ignoring old commands for
target `.c.d'
C:/Programme/R/R-2.10.1/etc/Makeconf:169: warning: overriding commands for
target `.m.o'
C:/Programme/R/R-2.10.1/etc/Makeconf:162: warning: ignoring old commands for
target `.m.o'
g++ -IC:/Programme/R/R-2.10.1/include-O2 -Wall  -c
OrthoFunctions.c -o OrthoFunctions.o
gcc -shared -s -o OrthoFunctions.dll tmp.def OrthoFunctions.o
-LC:/Programme/R/R-2.10.1/bin -lR
OrthoFunctions.o:OrthoFunctions.c:(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status



I have a vague idea of the issue of calling conventions and was hoping that
the __cdecl
specifier would force the appropriate convention.
I also have Cygwin installed as part of the Python(x,y) distribution but I
am assuming that
R CMD SHLIB source.c
calls the right compiler.

What could the problem be?

Many thanks,


Michael

[[alternative HTML version deleted]]

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


[Rd] nchar( NA )

2010-06-18 Thread Romain Francois

Hello,

Is this expected ?

 nchar( c( , NA ) )
[1] 0 2

Should not the second one be NA ?

Romain

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/98Uf7u : Rcpp 0.8.1
|- http://bit.ly/c6YnCi : graph gallery collage
`- http://bit.ly/bZ7ltC : inline 0.3.5

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


Re: [Rd] nchar( NA )

2010-06-18 Thread Sarah Goslee
Hi Romain,

Did you read the help for nchar?

Value:

 For ‘nchar’, an integer vector giving the sizes of each element,
 currently always ‘2’ for missing values (for ‘NA’).

It may be unexpected behavior, but it's *well-documented* unexpected behavior.

Sarah

On Fri, Jun 18, 2010 at 4:52 PM, Romain Francois
rom...@r-enthusiasts.com wrote:
 Hello,

 Is this expected ?

 nchar( c( , NA ) )
 [1] 0 2

 Should not the second one be NA ?

 Romain



-- 
Sarah Goslee
http://www.functionaldiversity.org

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


Re: [Rd] nchar( NA )

2010-06-18 Thread Henrique Dallazuanna
The help page says:

For ‘nchar’, an integer vector giving the sizes of each
 element, currently always ‘2’ for missing values (for
 ‘NA’).




On Fri, Jun 18, 2010 at 5:52 PM, Romain Francois
rom...@r-enthusiasts.comwrote:

 Hello,

 Is this expected ?

  nchar( c( , NA ) )
 [1] 0 2

 Should not the second one be NA ?

 Romain

 --
 Romain Francois
 Professional R Enthusiast
 +33(0) 6 28 91 30 30
 http://romainfrancois.blog.free.fr
 |- http://bit.ly/98Uf7u : Rcpp 0.8.1
 |- http://bit.ly/c6YnCi : graph gallery collage
 `- http://bit.ly/bZ7ltC : inline 0.3.5

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




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

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


Re: [Rd] nchar( NA )

2010-06-18 Thread Romain Francois

Le 18/06/10 22:58, Sarah Goslee a écrit :

Hi Romain,

Did you read the help for nchar?

Value:

  For ‘nchar’, an integer vector giving the sizes of each element,
  currently always ‘2’ for missing values (for ‘NA’).

It may be unexpected behavior, but it's *well-documented* unexpected behavior.


Oops. My scan of the help page was too quick. I did not see it.

Sorry for the noise.


Sarah

On Fri, Jun 18, 2010 at 4:52 PM, Romain Francois
rom...@r-enthusiasts.com  wrote:

Hello,

Is this expected ?


nchar( c( , NA ) )

[1] 0 2

Should not the second one be NA ?

Romain


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/98Uf7u : Rcpp 0.8.1
|- http://bit.ly/c6YnCi : graph gallery collage
`- http://bit.ly/bZ7ltC : inline 0.3.5

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


Re: [Rd] C Interface

2010-06-18 Thread Simon Urbanek

On Jun 18, 2010, at 10:23 AM, michael meyer mjhme...@googlemail.com wrote:

 Greetings,
 
 I am trying to call simple C-code from R.
 I am on Windows XP with RTools installed.
 
 The C-function is
 
 #include R.h
 #include Rinternals.h
 #include Rmath.h
 #include Rdefines.h
 
 // prevent name mangling
 extern C {
 
 SEXP __cdecl test(SEXP s){
 
  SEXP result;
  PROTECT(result = NEW_NUMERIC(1));
  double* ptr=NUMERIC_POINTER(result);
  double t = *REAL(s);
  double u = t-floor(t)-0.5;
  if(u0) *ptr=-1+4*u; else *ptr=-1-4*u;
  Rprintf(The value is %f, *ptr);
  UNPROTECT(1);
  return result;
 }
 
 };
 
 It is compiled with
 
 R CMD SHLIB OrthoFunctions.c
 
 with flag
 
 MAKEFLAGS=CC=g++
 

That is entirely wrong - g++ is not a C compiler.

Cheers,
Simon


 
 However when I call this code from R with
 
 test - function(t){
  .Call(test,t)
 }
 dyn.load(./OrthoFunctions.dll)
 test(0)
 dyn.unload(./OrthoFunctions.dll)
 
 then R crashes.
 
 If I compile with the default flags (no extern C, no __cdecl) I get an
 error message about an undefined reference to __gxx_personality_v0:
 
 C:\...R CMD SHLIB OrthoFunctions.c
 C:/Programme/R/R-2.10.1/etc/Makeconf:151: warning: overriding commands for
 target `.c.o'
 C:/Programme/R/R-2.10.1/etc/Makeconf:142: warning: ignoring old commands for
 target `.c.o'
 C:/Programme/R/R-2.10.1/etc/Makeconf:159: warning: overriding commands for
 target `.c.d'
 C:/Programme/R/R-2.10.1/etc/Makeconf:144: warning: ignoring old commands for
 target `.c.d'
 C:/Programme/R/R-2.10.1/etc/Makeconf:169: warning: overriding commands for
 target `.m.o'
 C:/Programme/R/R-2.10.1/etc/Makeconf:162: warning: ignoring old commands for
 target `.m.o'
 g++ -IC:/Programme/R/R-2.10.1/include-O2 -Wall  -c
 OrthoFunctions.c -o OrthoFunctions.o
 gcc -shared -s -o OrthoFunctions.dll tmp.def OrthoFunctions.o
 -LC:/Programme/R/R-2.10.1/bin -lR
 OrthoFunctions.o:OrthoFunctions.c:(.eh_frame+0x11): undefined reference to
 `__gxx_personality_v0'
 collect2: ld returned 1 exit status
 
 
 
 I have a vague idea of the issue of calling conventions and was hoping that
 the __cdecl
 specifier would force the appropriate convention.
 I also have Cygwin installed as part of the Python(x,y) distribution but I
 am assuming that
 R CMD SHLIB source.c
 calls the right compiler.
 
 What could the problem be?
 
 Many thanks,
 
 
 Michael
 
[[alternative HTML version deleted]]
 
 __
 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


Re: [Rd] nchar( NA )

2010-06-18 Thread Hadley Wickham
 Value:

     For ‘nchar’, an integer vector giving the sizes of each element,
     currently always ‘2’ for missing values (for ‘NA’).

 It may be unexpected behavior, but it's *well-documented* unexpected behavior.

Oh, that must make it ok then.

For a more sensible take:

 library(stringr)
 str_length(c(, NA))
[1]  0 NA


Hadley


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

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


Re: [Rd] C Interface

2010-06-18 Thread Dirk Eddelbuettel

Michael,

You are getting confused in compiler minutia when you could be concentrating
on your code.  The inline package can help here.  

Consider the snippet below which loads inline, defines your function body
sans headers etc and then uses the magic of inline to compile, link and load
your function:

---
library(inline)

code - '
  SEXP result;
  PROTECT(result = NEW_NUMERIC(1));
  double* ptr=NUMERIC_POINTER(result);
  double t = *REAL(s);
  double u = t-floor(t)-0.5;
  if(u0) *ptr=-1+4*u; else *ptr=-1-4*u;
  Rprintf(The value is %f\\n, *ptr);
  UNPROTECT(1);
  return result;
'

fun - cfunction(signature(s=numeric), code)

fun(0.0001)
---

Pasted in my R session I get:

 library(inline)
 
 code - '
+   SEXP result;
+   PROTECT(result = NEW_NUMERIC(1));
+   double* ptr=NUMERIC_POINTER(result);
+   double t = *REAL(s);
+   double u = t-floor(t)-0.5;
+   if(u0) *ptr=-1+4*u; else *ptr=-1-4*u;
+   Rprintf(The value is %f\\n, *ptr);
+   UNPROTECT(1);
+   return result;
+ '
 
 fun - cfunction(signature(s=numeric), code)
 
 fun(0.0001)
The value is 0.999600
[1] 0.9996
 

(I added a newline which, given that the code is character string, needed to
be escaped.)

If you like inline, you may like it even more in conjunction with Rcpp as the
program becomes shorter and simpler thanks to as() and wrap():

 code - '
+   double t = asdouble(s);
+   double u = t-floor(t)-0.5;
+   if (u0) t=-1+4*u; else t=-1-4*u;
+   std::cout  The value is   t  std::endl;
+   return wrap(t);
+ '
 fun - cxxfunction(signature(s=numeric), code, plugin=Rcpp)
 
 fun(0.0001)
The value is 0.9996
[1] 0.9996
 

Rcpp has a few vignettes that help getting started.  The rcpp-devel list can
help with questions.

-- 
  Regards, Dirk

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