[Rd] R CMD check tells me 'no visible binding for global variable ', what does it mean?
When I run R CMD check on a package I have recently started work on I get the following: * checking R code for possible problems ... NOTE addlinear: no visible binding for global variable 'x' I appreciate that this is only a NOTE and so I assume is R's equivalent of 'This is perfectly legal but I wonder whether it is really what you intended' but I would like to understand it. In the relevant function addlinear the following function is defined locally: orfun <- function(x, oddsratio) {1/(1+1/(oddsratio * (x/(1-x} and then used later in curve curve(orfun(x, exp(estimate)), from = 0.001, to = 0.999, add = TRUE) These are the only occurrences of 'x'. Is it just telling me that I have never assigned a value to x? Or is it more sinister than that? As far as I can tell the function does what I intended. Michael Dewey http://www.aghmed.fsnet.co.uk __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] R CMD check tells me 'no visible binding for global variable ', what does it mean?
On 12/04/2010 10:51 AM, Michael Dewey wrote: When I run R CMD check on a package I have recently started work on I get the following: * checking R code for possible problems ... NOTE addlinear: no visible binding for global variable 'x' I appreciate that this is only a NOTE and so I assume is R's equivalent of 'This is perfectly legal but I wonder whether it is really what you intended' but I would like to understand it. In the relevant function addlinear the following function is defined locally: orfun <- function(x, oddsratio) {1/(1+1/(oddsratio * (x/(1-x} and then used later in curve curve(orfun(x, exp(estimate)), from = 0.001, to = 0.999, add = TRUE) These are the only occurrences of 'x'. Is it just telling me that I have never assigned a value to x? Or is it more sinister than that? As far as I can tell the function does what I intended. The curve() function evaluates the first argument in a strange way, and this confuses the code checking. (The variable name "x" is special to curve().) I think you can avoid the warning by rewriting that call to curve() as curve(function(x) orfun(x, exp(estimate)), from = 0.001, to = 0.999, add = TRUE) Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] R CMD check tells me 'no visible binding for global variable ', what does it mean?
On Mon, Apr 12, 2010 at 5:08 PM, Duncan Murdoch wrote: > On 12/04/2010 10:51 AM, Michael Dewey wrote: >> >> When I run R CMD check on a package I have recently started work on I get >> the following: >> >> * checking R code for possible problems ... NOTE >> addlinear: no visible binding for global variable 'x' >> >> I appreciate that this is only a NOTE and so I assume is R's equivalent of >> 'This is perfectly legal but I wonder whether it is really what you >> intended' but I would like to understand it. >> >> In the relevant function addlinear the following function is defined >> locally: >> >> orfun <- function(x, oddsratio) {1/(1+1/(oddsratio * (x/(1-x} >> >> and then used later in curve >> >> curve(orfun(x, exp(estimate)), from = 0.001, to = 0.999, add = TRUE) >> >> These are the only occurrences of 'x'. >> >> Is it just telling me that I have never assigned a value to x? Or is it >> more sinister than that? As far as I can tell the function does what I >> intended. > > The curve() function evaluates the first argument in a strange way, and this > confuses the code checking. (The variable name "x" is special to curve().) > > I think you can avoid the warning by rewriting that call to curve() as > > curve(function(x) orfun(x, exp(estimate)), from = 0.001, to = 0.999, add = > TRUE) ...or x <- NULL; rm(x); # Dummy to trick R CMD check curve(orfun(x, exp(estimate)), from = 0.001, to = 0.999, add = TRUE) /Henrik > > Duncan Murdoch > > __ > 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] R CMD check tells me 'no visible binding for globalvariable ', what does it mean?
> -Original Message- > From: r-devel-boun...@r-project.org > [mailto:r-devel-boun...@r-project.org] On Behalf Of Henrik Bengtsson > Sent: Monday, April 12, 2010 8:24 AM > To: Duncan Murdoch > Cc: r-devel; Michael Dewey > Subject: Re: [Rd] R CMD check tells me 'no visible binding > for globalvariable ', what does it mean? > > On Mon, Apr 12, 2010 at 5:08 PM, Duncan Murdoch > wrote: > > On 12/04/2010 10:51 AM, Michael Dewey wrote: > >> > >> When I run R CMD check on a package I have recently > started work on I get > >> the following: > >> > >> * checking R code for possible problems ... NOTE > >> addlinear: no visible binding for global variable 'x' > >> > >> I appreciate that this is only a NOTE and so I assume is > R's equivalent of > >> 'This is perfectly legal but I wonder whether it is really what you > >> intended' but I would like to understand it. > >> > >> In the relevant function addlinear the following function > is defined > >> locally: > >> > >> orfun <- function(x, oddsratio) {1/(1+1/(oddsratio * > (x/(1-x} > >> > >> and then used later in curve > >> > >> curve(orfun(x, exp(estimate)), from = 0.001, to = > 0.999, add = TRUE) > >> > >> These are the only occurrences of 'x'. > >> > >> Is it just telling me that I have never assigned a value > to x? Or is it > >> more sinister than that? As far as I can tell the function > does what I > >> intended. > > > > The curve() function evaluates the first argument in a > strange way, and this > > confuses the code checking. (The variable name "x" is > special to curve().) > > > > I think you can avoid the warning by rewriting that call to > curve() as > > > > curve(function(x) orfun(x, exp(estimate)), from = 0.001, to > = 0.999, add = > > TRUE) > > ...or > > x <- NULL; rm(x); # Dummy to trick R CMD check > curve(orfun(x, exp(estimate)), from = 0.001, to = 0.999, add = TRUE) Or we could come up with a scheme to telling the usage checking functions in codetools that some some or all arguments of certain functions are evaluated in odd ways so it should not check them. E.g., irregularUsage(curve, expr) irregularUsage(lm, subset, formula) # subset and formula arguments of lm irregularUsage(expression, ...) # ... arguments to expression Perhaps one could add such indications to the NAMESPACE file or to a new file in a package. The former is kludgy but the latter requires changes to the packaging system. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > > /Henrik > > > > > Duncan Murdoch > > > > __ > > 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 > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Getting started with .C
Jeff Brown wrote: > > Hi, > > I'm trying to learn to use .C, which lets one invoke compiled C code from > within R. To do that, one has to first get the C code into R as a shared > object, which (I think) means first compiling it (with COMPILE or SHLIB) > and then loading it (with dyn.load()). > I would suggest taking it a step further and building an R package to hold your compiled code. The pros are: * It keeps the R wrapper scripts and other things you will end up creating packaged together with your code. * It handles compilation automagically during installation. * It handles loading the dylib for you. The only con I can think of is: * It takes ~2 extra minutes of your time to set up. But compared to other languages I have used this is a ridiculously small price to pay for the portability and organization offered by packages. I wrote a post that goes through step-by-step how to do this for the .Call() interface, including example code. You can find it at: http://n4.nabble.com/Writing-own-simulation-function-in-C-td1580190.html#a1580423 In "Writing R Extensions", p. 79, they give the following example of a C program for convolution of two vectors. (The details aren't important; it's just a function that does something to some stuff.) void convolve (double *a, int *na, double *b, int *nb, double *ab) { int i, j, nab = *na + *nb - 1; for(i = 0; i < nab; i++) ab[i] = 0.0; for(i = 0; i < *na; i++) for(j = 0; j < *nb; j++) ab[i + j] += a[i] * b[j] } Jeff Brown wrote: > > The document suggests calling it from R like this (again the details > aren't important): > > conv <- function(a, b) > .C("convolve", > as.double(a), > as.integer(length(a)), > as.double(b), > as.integer(length(b)), > ab = double(length(a) + length(b) - 1))$ab > > I wrote a file, "convolve.c", with nothing but the above C code in it. I > can't figure out how to compile it. I don't understand the syntax (no > parentheses?) and I always get the same information-free error message: > >> list.files() > [1] "AER""convolve.c" "sendmailR" >> R CMD SHLIB "compile.c" > Error: syntax error >> COMPILE "compile.c" > Error: syntax error >> R CMD SHLIB "compile" > Error: syntax error >> COMPILE "compile" > Error: syntax error >> R CMD SHLIB compile.c > Error: syntax error >> COMPILE compile.c > Error: syntax error >> R CMD SHLIB compile > Error: syntax error >> COMPILE compile > Error: syntax error > > I'm using an Intel MacBook Pro running Leopard. At a console, typing "gcc > --version" yields 4.2.1. I know I'm supposed to be using version 4.2; I > thought 4.2.1 would qualify, but please let me know if I'm wrong about > that. > > For guidance I've been relying on "Writing R Extensions", "R Installatino > and Administration", the "R for Mac OS X Developer's Page", and the > built-in help. Please let me know if there are other important resources > I've missed. > > Many thanks, > Jeff > All R CMD commands must be executed at the command line- i.e. in a Windows CMD shell or Unix/Linux bash shell. They are not meant for use inside the R interpreter. Hope this helps! -Charlie - Charlie Sharpsteen Undergraduate-- Environmental Resources Engineering Humboldt State University -- View this message in context: http://n4.nabble.com/Getting-started-with-C-tp1837912p1837936.html Sent from the R devel mailing list archive at Nabble.com. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Getting started with .C
On 12 April 2010 at 18:11, Sharpie wrote: | Jeff Brown wrote: | > I'm trying to learn to use .C, which lets one invoke compiled C code from | > within R. To do that, one has to first get the C code into R as a shared | > object, which (I think) means first compiling it (with COMPILE or SHLIB) | > and then loading it (with dyn.load()). | > | | I would suggest taking it a step further and building an R package to hold | your compiled code. The pros are: | | * It keeps the R wrapper scripts and other things you will end up | creating packaged together with your code. | | * It handles compilation automagically during installation. | | * It handles loading the dylib for you. All good reasone, but see below for an even easier solution to get going. | The only con I can think of is: | | * It takes ~2 extra minutes of your time to set up. But compared to | other languages I have used this is a ridiculously small price to pay for | the portability and organization offered by packages. | | I wrote a post that goes through step-by-step how to do this for the .Call() | interface, including example code. You can find it at: | | | http://n4.nabble.com/Writing-own-simulation-function-in-C-td1580190.html#a1580423 | | | | In "Writing R Extensions", p. 79, they give the following example of a C | program for convolution of two vectors. (The details aren't important; it's | just a function that does something to some stuff.) | | void convolve (double *a, int *na, double *b, int *nb, double *ab) { | int i, j, nab = *na + *nb - 1; | for(i = 0; i < nab; i++) | ab[i] = 0.0; | for(i = 0; i < *na; i++) | for(j = 0; j < *nb; j++) | ab[i + j] += a[i] * b[j] | } And all this is even easier if you use the excellent inline package. No Makefiles, no linking, no loading, it all "just works" on all three major platforms: - define the code you want in a variable, here 'code' this does not include the function header - define the function signature - call the 'cfunction' from package inline to compile, link and load the generated function - use it! Here is a live example: R> library(inline) # load inline R> code <- "int i, j, nab = *na + *nb - 1; +for(i = 0; i < nab; i++) + ab[i] = 0.0; +for(i = 0; i < *na; i++) { + for(j = 0; j < *nb; j++) +ab[i + j] += a[i] * b[j]; + }" R> fun <- cfunction(signature(a="numeric", na="numeric", b="numeric", nb="numeric", ab="numeric"), + code, language="C", convention=".C") R> str(fun) # check what the new fun object is Formal class 'CFunc' [package "inline"] with 2 slots ..@ .Data:function (a, na, b, nb, ab) ..@ code : chr "#include \n\n\nvoid file46e87ccd ( double * a, double * na, double * b, double * nb, double * ab ) {\nint i, j, nab = *na "| __truncated__ R> fun( 1:10, 10, 4:12, 9, numeric(18))$ab [1] 4 13 28 50 80 119 168 228 300 372 400 413 410 390 352 295 218 120 R> Voila, and we extended R at the command prompt. I'd still recommed .Call over .C, whether you use C++ (which I also recommend :) or C. I have a number of examples for this in the 'Introduction to High-Performance Computing with R' tutorials I have given the last few years, see the slides at http://dirk.eddelbuettel.com/presentations.html as well as the recent UCLA talks on more inline examples with Rcpp (if you want C++). Cheers, Dirk -- Registration is open for the 2nd International conference R / Finance 2010 See http://www.RinFinance.com for details, and see you in Chicago in April! __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Lapack, determinant, multivariate normal density, solution to linear system, C language
r-devel list, I have recently written an R package that solves a linear least squares problem, and computes the multivariate normal density function. The bulk of the code is written in C, with interfacing code to the BLAS and Lapack libraries. The motivation here is speed. I ran into a problem computing the determinant of a symmetric matrix in packed storage. Apparently, there are no explicit routines for this as part of Lapack. While there IS an explicit routine for this in Linpack, I did not want to use the older library. Also, right before I needed the determinant of the matrix A, I had used the Lapack routine dspsv to solve the linear system Ax=b, which does much of the work of computing a determinant also. In fact, the solution I came up with involves the output of this routine (which might be obvious to Lapack designers, but not me) My modest Googleing turned up very little unique material (as is typical with BLAS/Lapack/Linpack queries). Hence, I am writing the r-devel list partly to document the solution I've come up with, but mainly to elicit additional wisdom from seasoned R programmers. My solution to the problem is illustrated in the appended discussion and C code. Thanks for your input. -Matt Shotwell -- The Lapack routine dspsv solves the linear system of equations Ax=b, where A is a symmetric matrix in packed storage format. The dspsv function performs the factorization A=UDU', where U is a unitriangular matrix and D is a block diagonal matrix where the blocks are of dimension 1x1 or 2x2. In addition to the solution for x, the dspsv function also returns the matrices U and D. The matrix D may then be used to compute the determinant of A. Recall from linear algebra that det(A) = det(UDU') = det(U)det(D)det(U'). Since U is unitriangular, det(U) = 1. The determinant of D is the product of the determinants of the diagonal blocks. If a diagonal block is of dimension 1x1, then the determinant of the block is simply the value of the single element in the block. If the diagonal block is of dimension 2x2 then the determinant of the block may be computed according to the well-known formula b11*b22-b12*b21, where bij is the value in the i'th row and j'th column of the block. int i, q, info, *ipiv, one = 1; double *b, *A, *D, det; /* ** A and D are upper triangular matrices in packed storage ** A[] = a00, a01, a11, a02, a12, a22, a03, a13, a23, a33, ... ** use the following macro to address the element in the ** i'th row and j'th column for i <= j */ #define UMAT(i, j) (i + j * ( j + 1 ) / 2) /* ** additional code should be here ** - set q ** - allocate ipiv... ** - allocate and fill A and b... */ /* ** solve Ax=b using A=UDU' factorization where ** A represents a qxq matrix, b a 1xq vector. ** dspsv outputs the elements of the matrix D ** is upper triangular packed storage ** in the memory addressed by A. That is, A is ** replaced by D when dspsv returns. */ F77_CALL(dspsv)("U", &q, &one, A, ipiv, b, &q, &info); if( info > 0 ) { /*issue warning, system is singular*/ } if( info < 0 ) { /*issue error, invalid argument*/ } /* ** compute the determinant det = det(A) ** if ipiv[i] > 0, then D(i,i) is a 1x1 block diagonal ** if ipiv[i] = ipiv[i-1] < 0, then D(i-1,i-1), ** D(i-1,i), and D(i,i) form the upper triangle ** of a 2x2 block diagonal */ D = A; det = 1.0; for( i = 0; i < q; i++ ) { if( ipiv[ i ] > 0 ) { det *= D[ UMAT(i,i) ]; } else if( i > 0 && ipiv[ i ] < 0 && ipiv[ i-1 ] == ipiv[ i ] ) { det *= D[ UMAT(i,i) ] * D[ UMAT(i-1,i-1) ] -\ D[ UMAT(i-1,i) ] * D[ UMAT(i-1,i) ]; } } __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel