[Rd] C code hanging and printing everything at the end
Hi, I am currently writing an extension for R and have the need to include some C code. If I call the code with a large amount of data then it can take several minutes to complete. The C code prints out after a certain iteration hence letting the user know it hasn't crashed. When running in R this generally does not happen and all is printed out at the end once the program has completed successfully. I am using Rprintf() to print out the required output. e.g. Something simple which illustrates my point for(int i=0; i<1; i++){ #Calculations if (i%1000==0){ Rprintf("Step %d\n",i) } } All I get during the program is the OS X spinning wheel in R. Is there any way to print out as the program is running? Thanks, Rob __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] C code hanging and printing everything at the end
On Jan 4, 2011, at 11:41 AM, Robert Lowe wrote: > Hi, > > I am currently writing an extension for R and have the need to include some C > code. If I call the code with a large amount of data then it can take several > minutes to complete. > > The C code prints out after a certain iteration hence letting the user know > it hasn't crashed. > > When running in R this generally does not happen and all is printed out at > the end once the program has completed successfully. > > I am using Rprintf() to print out the required output. > > e.g. Something simple which illustrates my point > > for(int i=0; i<1; i++){ > #Calculations > if (i%1000==0){ > Rprintf("Step %d\n",i) > } > } > > All I get during the program is the OS X spinning wheel in R. Is there any > way to print out as the program is running? > You want to add R_CheckUserInterrupt(); so that the system has a chance to run the event loop and thus display the result. But the implication is that you may be interrupted so make sure R controls any memory allocations you have made. Cheers, Simon __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] scoping/non-standard evaluation issue
Dear r-devel list members, On a couple of occasions I've encountered the issue illustrated by the following examples: - snip --- > mod.1 <- lm(Employed ~ GNP.deflator + GNP + Unemployed + + Armed.Forces + Population + Year, data=longley) > mod.2 <- update(mod.1, . ~ . - Year + Year) > all.equal(mod.1, mod.2) [1] TRUE > > f <- function(mod){ + subs <- 1:10 + update(mod, subset=subs) + } > f(mod.1) Call: lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + Population + Year, data = longley, subset = subs) Coefficients: (Intercept) GNP.deflator GNPUnemployed Armed.Forces 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 Population Year 1.164e+00-1.911e+00 > f(mod.2) Error in eval(expr, envir, enclos) : object 'subs' not found - snip --- I *almost* understand what's going -- that is, clearly mod.1 and mod.2, or the formulas therein, are associated with different environments, but I don't quite see why. Anyway, here are two "solutions" that work, but neither is in my view desirable: - snip --- > f1 <- function(mod){ + assign(".subs", 1:10, envir=.GlobalEnv) + on.exit(remove(".subs", envir=.GlobalEnv)) + update(mod, subset=.subs) + } > f1(mod.1) Call: lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + Population + Year, data = longley, subset = .subs) Coefficients: (Intercept) GNP.deflator GNPUnemployed Armed.Forces 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 Population Year 1.164e+00-1.911e+00 > f1(mod.2) Call: lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + Population + Year, data = longley, subset = .subs) Coefficients: (Intercept) GNP.deflator GNPUnemployed Armed.Forces 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 Population Year 1.164e+00-1.911e+00 > f2 <- function(mod){ + env <- new.env(parent=.GlobalEnv) + attach(NULL) + on.exit(detach()) + assign(".subs", 1:10, pos=2) + update(mod, subset=.subs) + } > f2(mod.1) Call: lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + Population + Year, data = longley, subset = .subs) Coefficients: (Intercept) GNP.deflator GNPUnemployed Armed.Forces 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 Population Year 1.164e+00-1.911e+00 > f2(mod.2) Call: lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + Population + Year, data = longley, subset = .subs) Coefficients: (Intercept) GNP.deflator GNPUnemployed Armed.Forces 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 Population Year 1.164e+00-1.911e+00 - snip --- The problem with f1() is that it will clobber a variable named .subs in the global environment; the problem with f2() is that .subs can be masked by a variable in the global environment. Is there a better approach? Thanks, John John Fox Senator William McMaster Professor of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada web: socserv.mcmaster.ca/jfox __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] scoping/non-standard evaluation issue
Dear all, A small correction: I had a stray line in my f2(), env <- new.env(parent=.GlobalEnv) left over from yet another attempt; it can simply be removed. Sorry for the confusion, John John Fox Senator William McMaster Professor of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada web: socserv.mcmaster.ca/jfox > -Original Message- > From: r-devel-boun...@r-project.org [mailto:r-devel-boun...@r-project.org] On > Behalf Of John Fox > Sent: January-04-11 4:36 PM > To: r-devel@r-project.org > Cc: 'Sanford Weisberg' > Subject: [Rd] scoping/non-standard evaluation issue > > Dear r-devel list members, > > On a couple of occasions I've encountered the issue illustrated by the > following examples: > > - snip --- > > > mod.1 <- lm(Employed ~ GNP.deflator + GNP + Unemployed + > + Armed.Forces + Population + Year, data=longley) > > > mod.2 <- update(mod.1, . ~ . - Year + Year) > > > all.equal(mod.1, mod.2) > [1] TRUE > > > > f <- function(mod){ > + subs <- 1:10 > + update(mod, subset=subs) > + } > > > f(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces >3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year >1.164e+00-1.911e+00 > > > f(mod.2) > Error in eval(expr, envir, enclos) : object 'subs' not found > > - snip --- > > I *almost* understand what's going -- that is, clearly mod.1 and mod.2, or > the formulas therein, are associated with different environments, but I > don't quite see why. > > Anyway, here are two "solutions" that work, but neither is in my view > desirable: > > - snip --- > > > f1 <- function(mod){ > + assign(".subs", 1:10, envir=.GlobalEnv) > + on.exit(remove(".subs", envir=.GlobalEnv)) > + update(mod, subset=.subs) > + } > > > f1(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces >3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year >1.164e+00-1.911e+00 > > > f1(mod.2) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces >3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year >1.164e+00-1.911e+00 > > > f2 <- function(mod){ > + env <- new.env(parent=.GlobalEnv) > + attach(NULL) > + on.exit(detach()) > + assign(".subs", 1:10, pos=2) > + update(mod, subset=.subs) > + } > > > f2(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces >3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year >1.164e+00-1.911e+00 > > > f2(mod.2) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces >3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year >1.164e+00-1.911e+00 > > - snip --- > > The problem with f1() is that it will clobber a variable named .subs in the > global environment; the problem with f2() is that .subs can be masked by a > variable in the global environment. > > Is there a better approach? > > Thanks, > John > > > John Fox > Senator William McMaster > Professor of Social Statistics > Department of Sociology > McMaster University > Hamilton, Ontario, Canada > web: socserv.mcmaster.ca/jfox > > __ > 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] R CMD check bug or misfeature
This is a bug/misfeature report for R CMD check. The function tools:::.check_packages_used_in_tests Gives an apparently unintended error when checking the tests in the contributed package rcdd_1.1-3.tar.gz as found on CRAN. See the script below for details. The actual error reported is totally mysterious. * checking for unstated dependencies in tests ... NOTE Error in as.character(function (description = "", open = "", blocking = TRUE, : cannot coerce type 'closure' to vector of type 'character' Calls: ... tryCatch -> tryCatchList -> tryCatchOne -> Execution halted The error is apparently due to several of the test scripts using scan() which reads stuff from the script itself until an empty line is reached. This works fine (as one can see below, all the tests pass), but tools:::.check_packages_used_in_tests wants to parse each of the test scripts, and they don't parse. First, is this a bug? "Writing R Extensions" just says the contents of the tests directory should be [a-zA-Z]*.R files. It does not say they have to parse. Even if this is deemed a feature not a bug, it seems that "Writing R Extensions" should warn about this issue. Also tools:::.check_packages_used_in_tests should give a sane report about the issue, not a totally mysterious crash. Given the FIXME's and other comments in this function, it clearly needs more work. This issue should go on the todo list. I have rewritten the tests in rcdd so that they do not give this error, and will upload a new version when I get another bug resolved, but I think this issue could bite someone else. Hence should be fixed, somehow. -- here is a script showing the error --- -- the system is openSuSE Linux 11.3 --- Script started on Tue 04 Jan 2011 04:08:53 PM CST oak$ wget http://cran.r-project.org/src/contrib/rcdd_1.1-3.tar.gz --2011-01-04 16:09:51-- http://cran.r-project.org/src/contrib/rcdd_1.1-3.tar.gz Resolving cran.r-project.org... 137.208.57.37 Connecting to cran.r-project.org|137.208.57.37|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 598019 (584K) [application/x-gzip] Saving to: `rcdd_1.1-3.tar.gz' 0% [ ] 0 --.-K/s 2% [ ] 12,726 53.8K/s 7% [==>] 47,478 92.4K/s 23% [> ] 138,702 175K/s 52% [===> ] 312,462 308K/s 100%[==>] 598,019 494K/s in 1.2s 2011-01-04 16:09:53 (494 KB/s) - `rcdd_1.1-3.tar.gz' saved [598019/598019] oak$ tar zxf rcdd_1.1-3.tar.gz oak$ R CMD check rcdd * using log directory ‘/HOME/faculty/charlie/tmp/Bugs/rcmdcheck/rcdd.Rcheck’ * using R version 2.12.1 (2010-12-16) * using platform: x86_64-unknown-linux-gnu (64-bit) * using session charset: UTF-8 * checking for file ‘rcdd/DESCRIPTION’ ... OK * this is package ‘rcdd’ version ‘1.1-3’ * checking package name space information ... OK * checking package dependencies ... OK * checking if this is a source package ... OK * checking for executable files ... OK * checking whether package ‘rcdd’ can be installed ... OK * checking package directory ... OK * checking for portable file names ... OK * checking for sufficient/correct file permissions ... OK * checking DESCRIPTION meta-information ... OK * checking top-level files ... OK * checking index information ... OK * checking package subdirectories ... OK * checking R files for non-ASCII characters ... OK * checking R files for syntax errors ... OK * checking whether the package can be loaded ... OK * checking whether the package can be loaded with stated dependencies ... OK * checking whether the package can be unloaded cleanly ... OK * checking whether the name space can be loaded with stated dependencies ... OK * checking whether the name space can be unloaded cleanly ... OK * checking for unstated dependencies in R code ... OK * checking S3 generic/method consistency ... OK * checking replacement functions ... OK * checking foreign function calls ... OK * checking R code for possible problems ... OK * checking Rd files ... OK * checking Rd metadata ... OK * checking Rd cross-references ... OK * checking for missing documentation entries ... OK * checking for code/documentation mismatches ... OK * checking Rd \usage sections ... OK * checking Rd contents ... OK * checking for unstated dependencies in examples ... OK * checking line endings in C/C++/Fortran sources/headers ... OK * checking line endings in Makefiles ... OK * checking for portable compilation flags in Makevars ... OK * checking for portable use of $BLAS_LIBS ... OK * checking examples ... OK * checking for unstated dependencies in tests ... NOTE Error in as.character(function (description = "", open = "", blocking = TRUE, : cannot coerce type 'c
Re: [Rd] scoping/non-standard evaluation issue
On Jan 4, 2011, at 22:35 , John Fox wrote: > Dear r-devel list members, > > On a couple of occasions I've encountered the issue illustrated by the > following examples: > > - snip --- > >> mod.1 <- lm(Employed ~ GNP.deflator + GNP + Unemployed + > + Armed.Forces + Population + Year, data=longley) > >> mod.2 <- update(mod.1, . ~ . - Year + Year) > >> all.equal(mod.1, mod.2) > [1] TRUE >> >> f <- function(mod){ > + subs <- 1:10 > + update(mod, subset=subs) > + } > >> f(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + >Population + Year, data = longley, subset = subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year > 1.164e+00-1.911e+00 > >> f(mod.2) > Error in eval(expr, envir, enclos) : object 'subs' not found > > - snip --- > > I *almost* understand what's going -- that is, clearly mod.1 and mod.2, or > the formulas therein, are associated with different environments, but I > don't quite see why. > > Anyway, here are two "solutions" that work, but neither is in my view > desirable: > > - snip --- > >> f1 <- function(mod){ > + assign(".subs", 1:10, envir=.GlobalEnv) > + on.exit(remove(".subs", envir=.GlobalEnv)) > + update(mod, subset=.subs) > + } > >> f1(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + >Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year > 1.164e+00-1.911e+00 > >> f1(mod.2) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + >Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year > 1.164e+00-1.911e+00 > >> f2 <- function(mod){ > + env <- new.env(parent=.GlobalEnv) > + attach(NULL) > + on.exit(detach()) > + assign(".subs", 1:10, pos=2) > + update(mod, subset=.subs) > + } > >> f2(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + >Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year > 1.164e+00-1.911e+00 > >> f2(mod.2) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + >Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNPUnemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 > Population Year > 1.164e+00-1.911e+00 > > - snip --- > > The problem with f1() is that it will clobber a variable named .subs in the > global environment; the problem with f2() is that .subs can be masked by a > variable in the global environment. > > Is there a better approach? I think the best way would be to modify the environment of the formula. Something like the below, except that it doesn't actually work... f3 <- function(mod) { f <- formula(mod) environment(f) <- e <- new.env(parent=environment(f)) mod <- update(mod, formula=f) evalq(.subs <- 1:10, e) update(mod, subset=.subs) } The catch is that it is not quite so easy to update the formula of a model. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] scoping/non-standard evaluation issue
On Tue, Jan 4, 2011 at 4:35 PM, John Fox wrote: > Dear r-devel list members, > > On a couple of occasions I've encountered the issue illustrated by the > following examples: > > - snip --- > >> mod.1 <- lm(Employed ~ GNP.deflator + GNP + Unemployed + > + Armed.Forces + Population + Year, data=longley) > >> mod.2 <- update(mod.1, . ~ . - Year + Year) > >> all.equal(mod.1, mod.2) > [1] TRUE >> >> f <- function(mod){ > + subs <- 1:10 > + update(mod, subset=subs) > + } > >> f(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = subs) > > Coefficients: > (Intercept) GNP.deflator GNP Unemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02 -3.971e-03 -8.595e-03 > Population Year > 1.164e+00 -1.911e+00 > >> f(mod.2) > Error in eval(expr, envir, enclos) : object 'subs' not found > > - snip --- > > I *almost* understand what's going -- that is, clearly mod.1 and mod.2, or > the formulas therein, are associated with different environments, but I > don't quite see why. > > Anyway, here are two "solutions" that work, but neither is in my view > desirable: > > - snip --- > >> f1 <- function(mod){ > + assign(".subs", 1:10, envir=.GlobalEnv) > + on.exit(remove(".subs", envir=.GlobalEnv)) > + update(mod, subset=.subs) > + } > >> f1(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNP Unemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02 -3.971e-03 -8.595e-03 > Population Year > 1.164e+00 -1.911e+00 > >> f1(mod.2) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNP Unemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02 -3.971e-03 -8.595e-03 > Population Year > 1.164e+00 -1.911e+00 > >> f2 <- function(mod){ > + env <- new.env(parent=.GlobalEnv) > + attach(NULL) > + on.exit(detach()) > + assign(".subs", 1:10, pos=2) > + update(mod, subset=.subs) > + } > >> f2(mod.1) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNP Unemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02 -3.971e-03 -8.595e-03 > Population Year > 1.164e+00 -1.911e+00 > >> f2(mod.2) > > Call: > lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + > Population + Year, data = longley, subset = .subs) > > Coefficients: > (Intercept) GNP.deflator GNP Unemployed Armed.Forces > 3.641e+03 8.394e-03 6.909e-02 -3.971e-03 -8.595e-03 > Population Year > 1.164e+00 -1.911e+00 > > - snip --- > > The problem with f1() is that it will clobber a variable named .subs in the > global environment; the problem with f2() is that .subs can be masked by a > variable in the global environment. > > Is there a better approach? > I think there is something wrong with R here since the formula in the call component of mod.1 has a "call" class whereas the corresponding call component of mod.2 has "formula" class: > class(mod.1$call[[2]]) [1] "call" > class(mod.2$call[[2]]) [1] "formula" If we reset call[[2]] to have "call" class then it works: > mod.2a <- mod.2 > mod.2a$call[[2]] <- as.call(as.list(mod.2a$call[[2]])) > f(mod.2a) Call: lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + Population + Year, data = longley, subset = subs) Coefficients: (Intercept) GNP.deflator GNPUnemployed Armed.Forces Population Year 3.641e+03 8.394e-03 6.909e-02-3.971e-03-8.595e-03 1.164e+00-1.911e+00 -- 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