Re: [R] hit return key before next gaph appears
On Thu, Oct 19, 2006 at 02:28:01PM -0400, Chuck Cleland wrote: > Leeds, Mark (IED) wrote: > > i am looping and creating plots which are coming to the screen. i am in > > linux and remember ( in a previopus life ) i used to use a command so > > that the next graph n + 1 didn't appear on the screen until > > i hit the return key after graph n appeared. i thought i remeber using > > the command unix but when i type unix at the r prompt, it gices me > > system. it's probably a deprecated > > command then but i still don't remember what i did to make the next > > graph not appear until i hit the return key. or maybe it was any key for > > that matter. thanks a lot. > > par(ask=TRUE) > plot(rnorm(10)) As a somewhat more "advanced" variant, it's also possible to manually engineer the waiting for the return key. I sometimes do this in loops, as in hitReturn <- function(msg) { invisible(readline(sprintf("%s -- hit return", msg))); } plotAllColumns <- function(dframe, waitFunc = hitReturn) { for (n in colnames(dframe)) { plot(dframe[[n]]); waitFunc(n); } } # demo: dframe <- data.frame(x = runif(10), y = rnorm(10), z = rexp(10)); plotAllColumns(dframe); I find this useful to keep track of what the current plot actually is displaying. Furthermore, it's possible to run a simplistic kind of animation, as in plotAllColumns(dframe, function(msg) { print(msg); Sys.sleep(3); }); Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch 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.
Re: [R] OT UNIX grep question
On Thu, Aug 10, 2006 at 08:51:36AM -0300, Rolf Turner wrote: > [EMAIL PROTECTED] wrote: > > > You have to learn about regular expressions. Then you'll come up with > > something like : > > > > grep "^dog$" /usr/share/dict/words > > *You* have to learn about shell syntax. The foregoing doesn't > work; it gives an ``Illegal variable name.'' error. To protect > against the shell interpretation of the dollar sign you have > to use *single* quotes. > > grep '^dog$' /usr/share/dict/words > > *does* work. (Try it!) you're perfectly right about single quotes being the correct thing to use here, but not all shells are insisting on linguistic correctness the way [t]csh does. bash leaves constructs that it cannot expand as they are, so the variant with double quotes does work as expected with bash (although through a mechanism that might be unexpected by most). Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch 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.
Re: [R] VERY TINY question: missing function to clear the console?
On Thu, Jul 13, 2006 at 12:47:09PM +0200, Hans-Joerg Bibiko wrote: > > Hi, > > for presentation purposes I would like to clear to whole console > window (like in a UNIX terminal: 'clear'). > > Is there such a function? > > If not, I could image that is not too hard to write such a function. At the risk of this being a stupid answer: An easy way would be system("clear"); This should work under unixes that provide the "clear" command, don't know about windows. If there is no "clear screen" function in R (I haven't seriously looked for one), I think one of the reasons for that might be that writing that in a multi-platform portable way is actually a somewhat more laborious thing to do than it seems. Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] PowerPoint - eps not suitable
On Fri, Jun 23, 2006 at 01:43:54PM -0500, Marc Schwartz (via MN) wrote: > On Fri, 2006-06-23 at 14:02 -0400, Michael H. Prager wrote: > > Previous posters have argued for EPS files as a desirable transfer > > format for quality reasons. This is of course true when the output is > > through a Postscript device. > > > > However, the original poster is making presentations with PowerPoint. > > Those essentially are projected from the screen -- and screens of > > Windows PCs are NOT Postscript devices. The version of PowerPoint I > > have will display a bitmapped, low-resolution preview when EPS is > > imported, and that is what will be projected. It is passable, but much > > better can be done! > > > > In this application, I have had best results using cut and paste or the > > Windows metafile format, both of which (as others have said) give > > scalable vector graphics. When quirks of Windows metafile arise (as > > they can do, especially when fonts differ between PCs), I have had good > > results with PNG for line art and JPG for other art. > > > > Mike > > Just so that it is covered (though this has been noted in other > threads), even in this situation, one can still use EPS files embedded > in PowerPoint (or Impress) presentations. Just to cover yet another possible route, I've in the past used ghostscript to produce high resolution (or, more generally, whatever resolution was required) raster images from PostScript by something like gs -r150x150 -sDEVICE=bmp256 -sOutputFile=x.bmp -dNOPAUSE myfile.ps -c quit Apologies if this has been mentioned in this thread already. Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] "Pipelining" programs in R
On Thu, May 18, 2006 at 11:50:24PM -0400, Dan Rabosky wrote: > > Hello... > > I would like to use R for 'pipelining' data among several programs. I'm > wondering how I can use R to call another program, feed that program a set > of parameters, and retrieve the output. > > E.g., I have an executable that, when opened, prompts the user to enter a > set of parameters. The program then executes & prompts the user for the > name of an output file. I need to run this program on a large batch of > parameters, such that it would clearly be desirable to automate the > process. Is there a straightforward way to do this? I can't find any > online documentation addressing this topic, but perhaps I've not been > looking in the right place. > > In pseudocode, supposing I have a large array of parameters in R: > > For each set of parameters > -Open Program. > -Enter Parameters. > -Cause program to execute (typically done by simply entering " \n > " after manually entering parameters). > -Enter name of output file. > -Close program. If your program gets all the input it requires from the command line, you might use the pipe function, as in f <- pipe("ls -l"); l <- readLines(f); However, your executable seems to expect its input via its standard input and you want to read the output it writes to its standard output. To my knowledge, this is not possible with R. I've written some stuff to add such functionality to R, this is available from http://www2.cmp.uea.ac.uk/~jtk/software/ The filterpipe patch is against an older version of R, though. Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Can't there be a cd command?
On Wed, May 10, 2006 at 12:56:23PM -0400, Duncan Murdoch wrote: > On 5/10/2006 12:15 PM, Jan T. Kim wrote: > > On Wed, May 10, 2006 at 11:26:55AM -0400, Duncan Murdoch wrote: > >> On 5/10/2006 11:10 AM, Gabor Grothendieck wrote: > >> > On 5/10/06, Duncan Murdoch <[EMAIL PROTECTED]> wrote: > > > >> >> What is it that you find objectionable about having a default for the > >> >> file argument in read.table? I think Martin has said that he doesn't > >> >> want non-UI functions to be involved with UI functions, but I don't see > >> >> that: if your code works now, it will be completely unaffected by > >> >> setting a default for the argument. (Sorry if I summarized the argument > >> >> incorrectly, Martin, I didn't look it up.) > >> > > >> > That would be my objection too. UI should not be tied to the non-UI > >> > core. > >> > Its basically a loose coupling argument. > >> > >> I don't accept that argument, because in R everything* is interactive. > >> There isn't a non-UI core. The function arguments are part of the user > >> interface. > > > > It seems to me that there might be a misunderstanding here; as the term > > "user" is used to refer to a person interacting with the computer on > > the one hand, and to refer to a programmer using R on the other hand. > > One of the design goals of S and R is to blur the distinction between > users and programmers. It is a continuum. R is designed to gently urge > non-programmers to become programmers, because the designers think > that's the way statistical computing should be done. That's an idea I like very much too -- much better than the currently popular idea of "protecting" users from the "unfriendliness" of programming, anyway... > > Everything being "part of the user interface", in the sense of > > every user-visible function being part of the API, does not and should > > not imply that everything should be interactive. > > No, I didn't suggest that. What I was suggesting is that it should be > *convenient* to use read.table interactively, not that it should be > required. (It's already possible, but not convenient, especially for a > beginner who doesn't know the secret incantation.) Well, not knowing a secret is always inconvenient... ;-) > > In my experience, interactivity is a rather double-edged thing: On the > > one hand, it facilitates learning and exploration, but on the other > > hand, its improper use is frequently detrimental to reproducibility of > > scientific computation. > > I definitely agree with that. It should be convenient to use R > non-interactively as well. Anyone who wants reproducibility should be > writing packages and scripts or vignettes that run non-interactively. Ok, I fully agree with this -- seems that I've interpreted the statement that "in R everything is interactive" a bit too narrowly. > That's why I am emphasizing that this change will have no effect on > existing code. I wouldn't suggest it if it did. That's an important point too, obviously. I'm not entirely convinced about the convenience aspect, as I find file choosers of all sorts disruptive to workflow... but that's perhaps a matter of personal taste. Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Can't there be a cd command?
On Wed, May 10, 2006 at 11:26:55AM -0400, Duncan Murdoch wrote: > On 5/10/2006 11:10 AM, Gabor Grothendieck wrote: > > On 5/10/06, Duncan Murdoch <[EMAIL PROTECTED]> wrote: > >> What is it that you find objectionable about having a default for the > >> file argument in read.table? I think Martin has said that he doesn't > >> want non-UI functions to be involved with UI functions, but I don't see > >> that: if your code works now, it will be completely unaffected by > >> setting a default for the argument. (Sorry if I summarized the argument > >> incorrectly, Martin, I didn't look it up.) > > > > That would be my objection too. UI should not be tied to the non-UI core. > > Its basically a loose coupling argument. > > I don't accept that argument, because in R everything* is interactive. > There isn't a non-UI core. The function arguments are part of the user > interface. It seems to me that there might be a misunderstanding here; as the term "user" is used to refer to a person interacting with the computer on the one hand, and to refer to a programmer using R on the other hand. Everything being "part of the user interface", in the sense of every user-visible function being part of the API, does not and should not imply that everything should be interactive. In my experience, interactivity is a rather double-edged thing: On the one hand, it facilitates learning and exploration, but on the other hand, its improper use is frequently detrimental to reproducibility of scientific computation. Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] scoping issues?
On Thu, Dec 08, 2005 at 06:47:05AM -0500, tom wright wrote: > Can anyone please help me understand whats happening here? > Thanks > Tom > > getAmpRatio<-function(v_amps){ > #calculates the amplitude ratios between the 3 largest amps and the > rest > bigamp<-0 > map<-rep(TRUE,length(v_amps)) > > for(iLoc in 1:3){ > bigamp<-bigamp+max(v_amps) > map[which.max(v_amps)]<-FALSE > v_amps<-v_amps[map] > map<-rep(TRUE,length(v_amps)) > } > browser() > return(bigamp/mean(v_amps)) > } > > amps<-c(1,2,3,3,3,2,1) > getAmpRatio(amps) > > Browse[1]> v_amps > [1] 1 1 2 2 1 > Browse[1]> c(amps[1],amps[2],amps[3],amps[7],amps[8]) > [1] 1 1 2 2 1 > Browse[1]> mean(v_amps) > [1] 1.4 This computes the mean of the set {1, 1, 2, 2, 1}, which is 7 / 5 = 1.4 > Browse[1]> mean(amps[1],amps[2],amps[3],amps[7],amps[8]) > [1] 1 This computes the mean of amps[1], i.e. of the set {1}, which is 1 / 1 = 1. The remaining parameters are matched to the special variable length formal parameter of the mean function, which, upon eventual dispatch to mean.default are basically ignored, as far as I see. Perhaps, you meant mean(c(amps[1], amps[2], amps[3], amps[7], amps[8])) which effectively calls mean with one argument of length 5, as opposed to 5 arguments of length 1, as your call does. Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R is GNU S, not C.... [was "how to get or store ....."]
On Tue, Dec 06, 2005 at 04:21:01PM +, Patrick Burns wrote: > I don't put in extraneous ';' because I maybe get a > blister on my little finger. > > I suspect that those who find the semi-colons ugly in > R do not find them ugly in C. I think the reason there > would be a visceral reaction in R but not in C is that > there is a danger when using them in R that they really > mean something. > > We get questions on R-help often enough about why > code like: > > if(x > 0) y <- 4 > else y <- 4.5e23 > > doesn't work. > > If people habitually used semi-colons, those sorts of > questions would probably multiply. I don't understand. It would seem to me that in if (x > 0) y <- 4; else y <- 4.5e23; it's pretty obvious that the "if" statement is terminated by the semicolon at the end of the first line and that therefore, the "else" on the next line is erroneous because it is not associated with any "if". At least, the version above fails consistently, i.e. regardless of context. On the other hand, I've studied the R Language Definition for quite some time before fully understanding why if (x > 0) y <- 4 else y <- 4.5e23 works inside of a function (or other enclosing block) while it does not work interactively (or at the top level of a script). Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R is GNU S, not C.... [was "how to get or store ....."]
On Tue, Dec 06, 2005 at 03:16:17PM +0100, [EMAIL PROTECTED] wrote: > Martin Maechler a ?crit : > > > please, please, these trailing ";" are *so* ugly. > > This is GNU S, not C (or matlab) ! > > > > but I'll be happy already if you could > > drop these ugly empty statements at the end of your lines... > > May I disagree ? > I find missing ";" at end of lines *so* ugly. > Ugly/not ugly depends on our observer's eyes. > From my programmer point of view, I prefer to mark > clearly the end of the lines. > In many languages, it's safer to do it this way, > and I thank the R developers to permit it. I agree with this view -- I prefer an explicit statement terminator to a whitespace which terminates if termination is possible, too. > (in my opinion, it should even be mandatory). > (By the way, marking the end of lines with a unique symbol > makes also the job easier for the following treatment.) > And yes, I'm also a C programmer ;-) > > > {and I have another chain of argments why "<-" is so more > > expressive than "=" > > Why "<-" seems better than "=" is also quite mysterious for me. > There was a discussion about this point recently I think. > I believe in 99% of cases it's more for historical reason > (and perhaps also for some "snob" reasons). > > I am not at all a 20 years experienced R programmer, but I have > written several hundreds of R lines those 6 last months, > and until today didn't get any problem using "=" instead of "<-". As far as plain, stand-alone assignment statements are concerned, "=" and "<-" are equivalent. Given the diversity of coding styles that are permitted by R, consistently using one style is, in practice, perhaps more relevant than finding out what the "best" style is. There is a draft R Coding Convention available at http://www.maths.lth.se/help/R/RCC/ which may be useful for finding a style that is good because it is widely used and therefore familiar to a large number of readers. Best regards, Jan -- +- Jan T. Kim ---+ | email: [EMAIL PROTECTED] | | WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Long lines with Sweave
On Wed, Sep 14, 2005 at 02:49:56PM +0200, Henrik Andersson wrote: > Jan T. Kim wrote: > > On Wed, Sep 14, 2005 at 10:14:59AM +0200, Henrik Andersson wrote: > > > >>I have used Sweave a lot the latest year, but never really used any long > >>function calls. > >> > >> > >>If I have code which look like this > >> > >>- > >>gof <- benthic.flux(ID="Gulf of Finland", > >> meas.conc=conc, > >> bw.conc=bw.conc, > >> time=times, > >> substance=expression(DIC~(mmol~m^{-3})) > >> ) > >>- > >> > >>I get the output by Sweave in my pdf file, like this: > >> > >>--- > >> > gof <- benthic.flux(ID = "Gulf of Finland", meas.conc = conc, > >>+ bw.conc = bw.conc, time = times, substance = expression(DIC ~ > >>+ (mmol ~ m^{ > >>+ -3 > >>+ }))) > >> > >> > >>I can understand that it will not look exactly as entered but why is the > >>'-3' on a line of it's own? > >> > >>Can anyone suggest a idea to how I can make this more readable. > > > > > > It seems you've been thinking LaTeX rather than R ;-) : > > The exponent "-3" in the expression should be enclosed by parentheses > > rather than by curly braces. > > > > The code formatting done by the print method inserts the newline after > > "{" and before "}". > > > > Best regards, Jan > > If you look at demo(plotmath), I get the impression that m^(-3) does not > give me the desired behavior. > > I want to have -3 in superscript without visible parentheses. > > Tricky! Ok, I see. It seems to me that you could omit the curly braces in the example, I don't see any differences between the title in the plots produced by plot(1:10, main = expression(DIC~(mmol~m^-3))) and plot(1:10, main = expression(DIC~(mmol~m^{-3}))) For more complex exponents, you could try plain() to prevent them from being wrongly grouped by operator precedence, as in plot(1:10, main = expression(DIC~(mmol~m^plain(-3 + t Not exactly ideal for readability, however... Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Long lines with Sweave
On Wed, Sep 14, 2005 at 10:14:59AM +0200, Henrik Andersson wrote: > I have used Sweave a lot the latest year, but never really used any long > function calls. > > > If I have code which look like this > > - > gof <- benthic.flux(ID="Gulf of Finland", > meas.conc=conc, > bw.conc=bw.conc, > time=times, > substance=expression(DIC~(mmol~m^{-3})) > ) > - > > I get the output by Sweave in my pdf file, like this: > > --- > > gof <- benthic.flux(ID = "Gulf of Finland", meas.conc = conc, > + bw.conc = bw.conc, time = times, substance = expression(DIC ~ > + (mmol ~ m^{ > + -3 > + }))) > > > I can understand that it will not look exactly as entered but why is the > '-3' on a line of it's own? > > Can anyone suggest a idea to how I can make this more readable. It seems you've been thinking LaTeX rather than R ;-) : The exponent "-3" in the expression should be enclosed by parentheses rather than by curly braces. The code formatting done by the print method inserts the newline after "{" and before "}". Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Semicolons (was: clip to keep coordinate system?)
On Tue, Aug 09, 2005 at 07:49:36AM -0700, Thomas Lumley wrote: > On Tue, 9 Aug 2005 [EMAIL PROTECTED] wrote: > > > > > dear R wizards: > > > > plot( 1, 1, ylim=(2,10), xlim=(2,10), type="n"); > > rect( -1, -1, 12, 12, col=gray(0.99) ); > > > > unfortunately wipes out the border axes around the plot. how do I keep > > this? > > I think you meant >plot( 1, 1, ylim=c(2,10), xlim=c(2,10), type="n") >rect( -1, -1, 12, 12, col=gray(0.99) ) > Your code has two syntax errors and two spurious semicolons. What's wrong with the semicolons? Technically, they're not necessary, but they definitely improve readability without doing any harm. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] debian vcd package
On Thu, Jul 21, 2005 at 03:55:29PM +0100, Peter Ho wrote: > [Apologies if you have already read this message sent from another > email address] > > Hi R-Help, > > I have been using R in Linux (Debian) for the past month. The usual way > I install packages is through apt. Recently, a new packages "vcd" became > available on CRAN. I tried installing it today and found that Debian > does not seem to support this package. I also found that many other > packages were unavailable. > Does anyone have any recommended sites where a full list is available? > If none exist, what would be the best way to move ahead in installing > say the vcd package. > > I am still a novice in using Debian and so please forgive me if some of > my questions may seem trivial for experienced users. Unfortunately, the term "package" means different things in the context of R and of Debian. A Debian package is what you install using tools like apt etc. The traditional way of installing an R package on Linux is to * have R installed from source, or install the r-base-dev Debian package * download the package archive (e.g. http://www.stats.bris.ac.uk/R/src/contrib/vcd_0.9-0.tar.gz ) * run the R CMD INSTALL command on it, e.g. R CMD INSTALL vcd_0.9-0.tar.gz This requires having a number of development Debian packages installed, such as gcc, g77 etc (installing r-base-dev will automatically resolve such dependencies). Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Random seed problem in MCMC coupling of chains
On Wed, Jun 08, 2005 at 12:55:07PM -0400, Gabor Grothendieck wrote: > That could be addressed like this (where changing the offset > changes the experiment). > > offset <- 123 > > niter <- 3 > nchain <- 2 > for (i in 1:niter) { # iterations > for (j in 1:nchain) { # chains >set.seed(i+offset) >a <- runif(1) >cat("iter:", i, "chain:", j, "runif:", a, "\n") > } > } > > On 6/8/05, Paul Gilbert <[EMAIL PROTECTED]> wrote: > > Beware that your easy trick will give you the same result every time you > > run it. You need a better scheme if you actually intend to get a new > > experiment each time you run it. That's not a bad thing per se; in fact, it's a good thing to be able to exactly reproduce the results you obtained with your software. Personally, I dislike the "convenience" feature of random number generators of generating a seed, frequently based on the system time, if none has been set explicitly; I always set a seed and frequently make the seed a commandline option or part of the control parameter file or the like. >From this perspective, Gabor's solution seems perfect to me. > > Paul > > > > Gorjanc Gregor wrote: > > > > > Thanks to Duncan, Dimitris as well as James for answers. I'll provide > > > here also example from James, which seems to be the easiest of them > > > all and was not posted to the list: > > > > > > niter <- 3 > > > nchain <- 2 > > > for (i in 1:niter) { # iterations > > > for (j in 1:nchain) { # chains > > > set.seed(i) > > > a <- runif(1) > > > cat("iter:", i, "chain:", j, "runif:", a, "\n") > > > } > > > } > > > > > > Note that seed is set with iteration counter. This is really neat and > > > simple. I am just wondering if this is OK from "RNG point of view". Can > > > someone comment on that? The only concern I could think about is the case of a bad random number generator, in which the first couple of values are not entirely uncorrelated to the seed. But I'd be very surprised if that was a problem with R's RNGs -- I guess it's memories of lousy implementations C library rand() functions that make me write this remark. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Increasing Console "Paste Buffer"
On Tue, May 31, 2005 at 11:47:05PM +0100, Gavin Simpson wrote: > Manuel Morales wrote: > >Hello list. > > > >I'm using R from the gnome-terminal in Fedora. My preference is to write > >programs in VIM, and then source the file from R, or copy and paste the > >lines into the console. I'm wondering if there is a way to increase the > >"paste buffer" as an alternative to "sourcing" large analyses. As was > >mentioned in a recent thread on Linux GUI's, I find that if I paste in a > >large amount of text, the lines end up getting cut off at some point. I > >wonder if this is an R restriction, because it seems like I am able to > >paste substantially more text in other console-based programs. Is there > >any way to increase the amount of text that I can paste into an R > >session? > > > >Thanks! > > > >Manuel > > > > Manuel, > > Maybe I misunderstand what you mean by "lines end up getting cut off at > some point" so correct me if I got it wrong, but I assume you mean that > after a certain number of lines entered you can no longer scroll back up > and view the earlier lines? I think that this is not an issue of the scroll buffer, but of buffers internal to the terminal program or the shell, which are designed to hold keyboard input and which can be overwhelmed by the rate of input when large text selections are pasted in, as this appears as though thousands of keys had been typed almost instantaneously from their view, so to speak. The point at which the buffer overruns is quite unpredictable and irreproducible, but generally, the slower a program is to interpret its input, the faster the overrun occurs. Editors like vim are likely processing their input much faster than R, and they may therefore be much less prone to this effect. I've seen this phenomenon with rxvt and the fancy terminals that come with Gnome and KDE. The only terminal program with which I've never seen that is xterm -- but that doesn't mean that xterm is entirely proof against such loss of input either. Pasting in larger amounts of code frequently results in a screen which is rather difficult to interpret. More than once, I've been called to help people who didn't get the desired result from pasting code they presumed correct int some terminal, only to find that they were overlooking that error message triggered by line 7 out of 53 lines because that was hidden in the swamp resulting from all the subsequent lines of input and any output triggered by these. One fundamental problem with pasting lines is that the pasted matter will continue to be entered into the interpreter regardless of any errors caused along the way. For these reasons, I generally strongly recommend against pasting into terminals. In R, use the source() instead... ;-) Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R annoyances
On Fri, May 20, 2005 at 08:14:24AM -0400, Liaw, Andy wrote: > > From: Robin Hankin > > > > On May 20, 2005, at 11:00 am, Jan T. Kim wrote: > > > > > On Thu, May 19, 2005 at 03:10:53PM -0400, John Fox wrote: > > > > > >> Since you can use variables named c, q, or t in any event, I don't > > >> see why > > >> the existence of functions with these names is much of an > > impediment. > > > > > > True, particularly since I'm not too likely to use these > > variables for > > > (local) > > > functions, and variables of other types don't prevent > > functions from > > > working. > > > (I thought this was a problem... I must be spoilt by > > recently having > > > to read > > > too much Matlab code, where parentheses are used to both enclose > > > subscripts and > > > parameter lists, thus rendering subscript expressions and function > > > calls > > > syntactically indistinguishable.) > > > > > > Heh, I'm a recovering Matlab user too. This is sooo true! > > > > In Matlab: > > > > f(10)# function f() evaluated at 10 > > f(10)# 10th element of vector f. confusing!! > > > > R uses round brackets in two unrelated ways: > > > > 4*(1+2) --- using "(" and ")" to signify grouping > > f(8) function f() evaluated at 8. > > > > where there is no reason to use the same parenthesis symbol for both > > tasks. > > The same is done in Fortran/C/C++/Java/Python and God knows how many > others... And this is different from the subscripting / function call ambiguity, as these languages (to the extent I know them) are designed such that parentheses for precedence control are syntactically distinguishable from those used for function parameter lists: If the opening parenthesis is preceded by an identifier, that identifier is a function name and the parenthesis opens a parameter list. (Python is a somewhat messy case, though, because it uses parentheses for tuples too.) Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R annoyances
On Thu, May 19, 2005 at 03:10:53PM -0400, John Fox wrote: > Since you can use variables named c, q, or t in any event, I don't see why > the existence of functions with these names is much of an impediment. True, particularly since I'm not too likely to use these variables for (local) functions, and variables of other types don't prevent functions from working. (I thought this was a problem... I must be spoilt by recently having to read too much Matlab code, where parentheses are used to both enclose subscripts and parameter lists, thus rendering subscript expressions and function calls syntactically indistinguishable.) > The problem that I see with T and F is that allowing them to be redefined > sets a trap for people. If R wants to discourage use of T and F for TRUE and > FALSE, then why provide standard global variables by these names? On the > other hand, if providing T and F is considered desirable (e.g., for S-PLUS > compatibility), then why not make them reserved names? Perhaps, it's a legacy code thing -- if there's both code relying on T and F being aliases of TRUE and FALSE, and code using T or F as variable names, then the situation cannot be resolved in either way without breaking some code. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] parsing speed
On Tue, May 17, 2005 at 09:50:20AM +0200, Martin Maechler wrote: > >>>>> "BertG" == Berton Gunter <[EMAIL PROTECTED]> > >>>>> on Mon, 16 May 2005 15:20:01 -0700 writes: > > BertG> (just my additional $.02) ... and as a general rule > BertG> (subject to numerous exceptions, caveats, etc.) > > BertG> 1) it is programming and debugging time that most > BertG> impacts "overall" program execution time; 2) this is > BertG> most strongly impacted by code readability and size > BertG> (the smaller the better); 3) both of which are > BertG> enhanced by modular construction and reuseability, > BertG> which argues for avoiding inline code and using > BertG> separate functions. > > BertG> These days, i would argue that most of the time it is > BertG> program clarity and correctness (they are related) > BertG> that is the important issue, not execution speed. > > BertG> ... again, subject to exceptions and caveats, etc. > > Yes indeed; very good points very well put! > > Just to say it again: > > We strongly recommend not to "inline" your code, but rather > program modularly, i.e. call small `utility' functions. Generally, I fully agree -- modular coding is good, not only in R. However, with regard to execution time, modularisation that involves passing of large amounts of data (100 x 1000 data frames etc.) can cause problems. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] inserting R code in a latex document
On Wed, May 11, 2005 at 10:09:07AM +0200, Uwe Ligges wrote: > Thibaut Jombart wrote: > > >Hello, > > > >I'm trying to insert R source code (functions) in an appendix of a latex > >document. I guess the easiest way to do so is to use the package Sweaved > >(file : Sweaved.sty) provided with the latest R version. Latex succeeds > >in loading the package, but my problem comes from the use of this very > >package. I tried to use the 'Schunk' environment, but '#' characters > >generate error (my R functions are annotated, and I want to keep those > >annotations). Under the 'Sinput' environment, no error is generated but > >the result is simply not different from what would be obtained under > >'verbatim' environment : R code lines are cut as they don't fit in the > >page width. > > > >I tried to find answers in the latest Sweaved User Manual, > >unsuccessfully. I'm a recent latex user and I doubt I can quickly find a > >solution by myself. > > > I think we are talking about "Sweave" in package "utils"? > If so, you will find that you have to re-read the manual, because you > don't need to specify 'Schunk'/'Sinput' yourself: Sweave does it for > you. Just write the code in code chunks such as My understanding was that Thibaut tries to use the Sweave style (\usepackage{Sweave}) in a standard LaTeX document, so my comment here is focused on LaTeX rather than R. In this case, the Schunk environment won't be of any use because as provided by Sweave.sty, it doesn't do anything: \newenvironment{Schunk}{}{} Therefore, LaTeX will interpret the hash as a special character inside Schunk environments; the hash has to be wrapped into a verbatim-like environment to be legal as a standard character. Running Sweave on an rnw file results in the code chunks, such as > <<>>= > code > # comment > @ to be wrapped in \begin{Schunk} \begin{Sinput} code # coment \end{Sinput} \end{Schunk} As the Schunk environment does nothing, I have used it as a handle for adding effects, e.g. if your lines are just a bit too long, you may be able to fix that by adding \renewenvironment{Schunk}{\footnotesize}{} to the preamble of your LaTeX file. (This can also be useful for rnw files too.) If your code lines are way too long and won't even fit with \tiny, I'm afraid that there is no automatic way to resolve this. With very few exceptions, automatic reformatting of code does not result in readable results. You'll have to manually arrange your code such that line length does not exceed a reasonable maximum (which may generally improve readability and maintainability of your code). Finally, even as a LaTeX beginner, don't be afraid to look into style files and the like. Sweave.sty is really a quite short and understandable one. A great thing about LaTeX is that you can always check the code, and while such lessons may not always be what you're looking for e.g. when under time pressure to get something finished, I've found that (as very often), I've found that reading LaTeX code has enabled me to use LaTeX much more effectively over time. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Simple methods question
On Thu, Apr 21, 2005 at 04:35:07PM +0100, Jan T. Kim wrote: > On Thu, Apr 21, 2005 at 03:00:07PM +0200, Uwe Ligges wrote: > > Sean Davis wrote: > > > > >I would like to create a function with methods with the same behavior > > >for both numeric and logical arguments (using S4 methods) and different > > >for matrix. > > > > > >I would typically do: > > > > > >setGeneric('foo',function(x) standardGeneric('foo')) > > >setGeneric('foo','numeric',function(x) {...stuff 1...}) [...] > > >setGeneric('foo','logical',function(x) {...stuff 1...}) > > >setGeneric('foo','matrix',function(x) {stuff 2...}) > > > > > >If "stuff1" is identical for numeric and logical, can the two > > >setGenerics be "combined" somehow? > > > > Maybe using (implicit) inheritance? > > If the main point is to avoid duplication of the ...stuff 1... code, > I'd suggest assigning that function to an identifier and using that in > the setMethod calls, as in > > stuff1 <- function(x) > { > ...stuff 1... > } > > > stuff2 <- function(x) > { > ...stuff 2... > } > > setMethod('foo', 'numeric', stuff1); > setMethod('foo', 'logical', stuff1); > setMethod('foo', 'matrix', stuff1); ^^ Just to avoid any possible confusion: This should have been "stuff2", of course. Sorry for this fumble. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Simple methods question
On Thu, Apr 21, 2005 at 03:00:07PM +0200, Uwe Ligges wrote: > Sean Davis wrote: > > >I would like to create a function with methods with the same behavior > >for both numeric and logical arguments (using S4 methods) and different > >for matrix. > > > >I would typically do: > > > >setGeneric('foo',function(x) standardGeneric('foo')) > >setGeneric('foo','numeric',function(x) {...stuff 1...}) > > Do you mean setMethod()? Seems to be the case to me -- if not, my subsequent comment may be pointless... > >setGeneric('foo','logical',function(x) {...stuff 1...}) > >setGeneric('foo','matrix',function(x) {stuff 2...}) > > > >If "stuff1" is identical for numeric and logical, can the two > >setGenerics be "combined" somehow? > > Maybe using (implicit) inheritance? If the main point is to avoid duplication of the ...stuff 1... code, I'd suggest assigning that function to an identifier and using that in the setMethod calls, as in stuff1 <- function(x) { ...stuff 1... } stuff2 <- function(x) { ...stuff 2... } setMethod('foo', 'numeric', stuff1); setMethod('foo', 'logical', stuff1); setMethod('foo', 'matrix', stuff1); Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] line numbers and file names in error messages
On Fri, Apr 15, 2005 at 12:37:46PM -0400, Gabor Grothendieck wrote: > On 4/15/05, Jan T. Kim <[EMAIL PROTECTED]> wrote: > > On Fri, Apr 15, 2005 at 11:27:13AM -0400, Gabor Grothendieck wrote: > > > On 4/15/05, Vivek Rao <[EMAIL PROTECTED]> wrote: > > > > Many of my R scripts call other R scripts using the > > > > source function. If there is a syntax error in one of > > > > the scripts, I get an error message such as > > > > > > > > Error in parse(file, n, text, prompt) : syntax error > > > > on line 1 > > > > > > > > but the name of the file where the error occurs is not > > > > given. Other error messages such as > > > > > > > > Error in print(xxx) : Object "xxx" not found > > > > > > > > show neither the file name or the line number. Is > > > > there a way to get this information in error messages? > > > > I have found it helpful in other programming > > > > languages. > > > > > > Perhaps at the end of each script you could add a print statement > > > to tell you it had successfully finished. > > > > No, this won't help. The trouble is that the first type of error is > > detected during parsing while the second type of error occurs during > > execution. For the parser, the line > > > >print(xxx); > > > > is perfectly fine, the error is that the thing to be printed does > > not exist. At the time of execution, the information about which > > line in what file contained the code that caused the problem. > > Actually it does help. The last line of the file will only be reached > if there are no errors that prevent it from reaching there regardless > of their type. Thus if the print executed you know that that sourced file > finished allowing you to determine which ones worked. Sorry if I sounded a bit harsh here. What I meant is that the printing approach may be misleading. Consider: # file1.r foo <- function() { print(xxx); } print("file1: success"); # file2.r source("file1.r"); bar <- function() { foo(); } bar(); xxx <- "hello, world"; print(xxx); print("file2: success"); > source("file2.r"); [1] "file1: success" Error in print(xxx) : Object "xxx" not found Now, the unsuspecting may easily be misled to believe that print(xxx) in file2.r is at fault, whereas the traceback reveals that the foo function is the culprit: > traceback() 6: print(xxx) 5: foo() 4: bar() 3: eval.with.vis(expr, envir, enclos) 2: eval.with.vis(ei, envir) 1: source("file2.r") Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] line numbers and file names in error messages
On Fri, Apr 15, 2005 at 11:27:13AM -0400, Gabor Grothendieck wrote: > On 4/15/05, Vivek Rao <[EMAIL PROTECTED]> wrote: > > Many of my R scripts call other R scripts using the > > source function. If there is a syntax error in one of > > the scripts, I get an error message such as > > > > Error in parse(file, n, text, prompt) : syntax error > > on line 1 > > > > but the name of the file where the error occurs is not > > given. Other error messages such as > > > > Error in print(xxx) : Object "xxx" not found > > > > show neither the file name or the line number. Is > > there a way to get this information in error messages? > > I have found it helpful in other programming > > languages. > > Perhaps at the end of each script you could add a print statement > to tell you it had successfully finished. No, this won't help. The trouble is that the first type of error is detected during parsing while the second type of error occurs during execution. For the parser, the line print(xxx); is perfectly fine, the error is that the thing to be printed does not exist. At the time of execution, the information about which line in what file contained the code that caused the problem. The traceback() function can be useful for investigating execution time errors. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Printing integers in R "as is"
On Thu, Apr 14, 2005 at 02:32:33PM +0300, Firas Swidan wrote: > I am using the following command to print to a file (I omitted the file > details): > > cat( paste( paste(orientation, start, end, names,"\n"), paste(start, end, > "exon\n"), sep="")) > > where "orientation" and "names" are character vectors and "start" and > "end" are integer vectors. For printing formatted output of this kind, you're generally much better off using sprintf, as in cat(sprintf("%2s %8d %8d %s\n", orientation, as.integer(start), as.integer(end), names)); or, if length(names) > 1, you might consider sprintf("%2s %8d %8d %s\n", orientation, as.integer(start), as.integer(end), paste(names, collapse = ", ")); etc. This assumes that start and end are numeric vectors of length 1, which seems sensible to me based on the context I can conclude from the variable names, and I think that sprintf in R-devel, and R 2.1.0 in the near future will cycle over longer vectors too. > The problem is that R coerce the integer vectors to characters. In > general, that works fine, but when one of the integer is 10 (or has > more 0's) then R prints it as 1e+05. This behavior causes a lot of > trouble for the program reading R's output. > This problem occur with paste, cat, > and print (i.e. paste(10)="1e+05" and so on). Are you certain that start and end are integer vectors? If in doubt, check typeof(start) -- the fact that the values are integer does not necessarily mean that the type is integer. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R as programming language: references?
On Tue, Apr 12, 2005 at 02:01:04PM +0200, A.J. Rossini wrote: > On Apr 12, 2005 11:54 AM, Duncan Murdoch <[EMAIL PROTECTED]> wrote: > > > > - Original Message - From: "Federico Calboli" > > > <[EMAIL PROTECTED]> > > > To: "r-help" > > > Sent: Tuesday, April 12, 2005 5:14 PM > > > Subject: [R] R as programming language: references? > > > > > > > > >> Hi All, > > >> > > >> I am looking for references on R as a programming language (apart form > > >> the standard R-lang.pdf and the other manuals), reference that would > > >> cover _in_depth_ things like loops, code optimisation, debugging tools > > >> etc... and is as up-to-date as possible. > > >> > > >> Can anyone suggest any book or other reference apart from the "green > > >> book" and the V&R "S-programming"? > > > > I think you've already got the best references. > > There is always the source. In a sense, it IS the most in-depth and > up-to-date description of the intricacies of using the language, > though it isn't as easy to read as V&R's S Programming. > > In-depth and up-to-date are tradeoffs rather than being complementary. I don't know what Federico Calboli has in mind, but as for myself, upon starting with R, I've been looking for an R language reference in the style of the Python reference (http://docs.python.org/ref/ref.html). The specification of the grammar and the associated semantics of a language gives me the kind of in-depth conceptual understanding that I like to have, and I find this more difficult to accrue for R than for other languages. For example, I'm still not certain whether I'm able to correctly predict how many copies of an object are created during the execution of some code, and consequently, I'm not really confident that my code is reasonably optimal. I'd appreciate pointers to any (more or less hidden) gems I may have overlooked, of course. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] hex format
On Thu, Apr 07, 2005 at 11:58:48AM -0500, Earl F. Glynn wrote: > "Duncan Murdoch" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > > Seems to me the conversion from hex to decimal should be system > independent > > > (and makes working with colors much more convenient). Why isn't this > system > > > independent now? > > > > Presumably because nobody thought it was important enough to make it so. > > R isn't a low level system programming language, so why should it > > treat hex specially? > > 1) While generally I'd agree with your statement, manipulating colors is one > place the ability to convert to/from hex would be quite nice. > > > rgb(1,0,0.5) > [1] "#FF0080" > > rgb returns a hex string and then R makes manipulating this string somewhat > difficult. I'd like to second this opinion. It just occasionally happens that data are available in some variant of hex format, and I've had the impression that getting such data into R is a bit less convenient than it could be. > One might want to use such color values to convert to a > different color space, perform some sort of manipulation in that other color > space, and then convert back to rgb. > > 2) I would think that one of R's mathematical abilities would be to provide > a way to convert from any base to base 10, and from base 10 to any base. I > haven't found this general math tool yet in R. Working with base-16 (or > even base 2 sometimes) could be done with such a general math tool. In fact, the ANSI C function strtol already provides conversion to any base between 2 and 36, so R's mathematical capabilities don't even need to be invoked here. An R function strtol(x, base), x being a character variable and base an integer between 2 and 36, would probably add a bit of convenience. I've never programmed that, though -- seems that I'm one of those to whom this hasn't been important enough. If it is done some day, I'd favour the strtol function over having as.numeric interpret the (rather C-ish) 0x prefix. I wasn't aware that this currently works on some platforms (and I'm glad it doesn't interpret the 0 prefix for octal, as C does, making 007 legal and 008 not. ;-) ) Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] how to print error message in batch mode
On Thu, Apr 07, 2005 at 01:58:55PM +0200, Elio Mineo wrote: > This solution is fine, too. > The Achim's solution is what I have asked with this slight modification: > > $ R -q --no-save < prova > prova.out 2>> prova.out The canonical and perhaps "more correct" way to redirect stderr into stdout is 2>&1, as in R -q --no-save < prova > prova.out 2>&1 Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] off-topic question: Latex and R in industries
On Wed, Apr 06, 2005 at 11:56:59AM -0700, Thomas Lumley wrote: > On Wed, 6 Apr 2005, roger bos wrote: > >Insightful will come in and give a company presentation. Who wants to > >volunteer to come into my company and demo R for my manager? I only > >learned about R a year ago when a friend of mine told me about it. > >The real question is, how to get more exposuRe? > > > > The other real question is "Why?". I can see the motivation of people who > want to use R and need to convince their management that it is safe, but > inflicting R on people who haven't heard of it and are perfectly happy > that way seems unnecessary. What would be the benefit? Of course, it follows from the assumption of perfect happiness without R that there's no point in forcibly selling R to them. I suspect that the salesforces behind commercial software are not exclusively driven by such reason, however... Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Introduce a new function in a package?
On Wed, Apr 06, 2005 at 09:57:00AM -0400, Roger D. Peng wrote: > I think the usual way is to create an R package for yourself and load > it when you need it for whatever project. > > -roger Alternatively, one can also write the function in question into one's ~/.Rprofile; then, it's automatically available in all R sessions. To avoid confusion, make sure that you choose a unique name, i.e. one that isn't used by any package, if possible. This method should be used only for functions intended to provide some convenience in interactive sessions, code in scripts should not rely on functions being provided by ~/.Rprofile. For scripting, an R package is definitely preferred. Best regards, Jan > Luis Ridao Cruz wrote: > >R-help, > > > >Sometimes I define functions I wish to have in any R session. > >The obvious thing to do is copy-paste the code > >The thing is that sometimes I don't know where I have the function > >code. > > > >My question is if somehow I could define a function and "introduce" it > >(let's say 'base' package ) so that > >could be used anytime I run a different R project. > > > >Thank you in advance > > > >__ > >R-help@stat.math.ethz.ch mailing list > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide! > >http://www.R-project.org/posting-guide.html > > > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Precision
On Wed, Apr 06, 2005 at 02:00:58PM +0200, Josef Eschgfaeller wrote: > How precise is R numerically? For example I > wrote the following function for calculating > the volume of the ball inscribed in the > unit cube in m dimensions. In order to see what > happens in 40 dimensions, I created an output > of 24 digits. But how many are precise? R uses IEEE-754 double precision floating point arithmetic (see capabilities()), which has a 52 bit mantissa, roughly corresponding to 15 decimal digits. This is how much information is available, how much of it is precise in the sense that it accurately reflects the quantity you're computing depends on the computation you're doing. > Thanks > Josef Eschgf?ller > Ferrara > --- > Vol = function (m) > {if (m<=1) 1 > else Vol(m-2)*pi/(m+m)} > > > for (m in 1:40) > {x=sprintf('%2d %.24f',m,Vol(m)) > print(x)} > --- For getting an impression of "what happens", this looks ok to me. If you're concerned that precision wanes because you get less and less non-zero digits: that's not the case -- use the %e or %g format to see that the number of mantissa digits does not decrease. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] about get stdout from other program
On Fri, Apr 01, 2005 at 01:29:01AM +, Michael S wrote: > if I want to use stdout from other language as my R program input ,which is > the best way for design the API,using Pipe function or produce a temporary > file,using scan function to read the file ? For the scan function, it makes no difference whether you read from a pipe or a regular file, both are represented by connections in R. Temporary files should be avoided where possible, as they introduce a source of data corruption that may remain unnoticed for quite some time. If your external program can be run by just one command line, use a pipe, as in p = pipe("ls"); scan(p, what = character(0)); Full filtering (i.e. if you need to write input into its stdin in addition to reading output from its stdout) is currently not supported by R. I've attempted to provide that, see http://www2.cmp.uea.ac.uk/~jtk/software/ Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] r under linux: creating high quality bmp's for win users
On Tue, Mar 22, 2005 at 11:59:25AM +0100, Christoph Lehmann wrote: > I produce graphics with R under linux, but my collaborators often use > windows and cannot import eps pics e.g. in msword > > what is the standard way to get e.g. bmp's with the same quality as eps. > going the way: creating eps, convert eps2bmp using 'convert' doesn't > yield good enough bmp's > > thanks for a short hint The too short version: You can't get bmps of the same quality as encapsulated postscript because bmp is a raster format and postscript is a language that implements vector graphics. The somewhat more useful (hopefully) version: You can always use gs to produce a fixed resolution raster snapshot of your EPS file, e.g. gs -r600x600 -sDEVICE=bmp16m -sOutputFile=x.bmp -dNOPAUSE x.eps -c quit This allows you to control the resolution (-r option) and should allow you to produce any "quality" that may practically suffice, although this is a kludge. Best regards, Jan P.S.: I believe that the convert program just acts as a wrapper to gs anyway, with a resolution that is chosen to be useful for screen graphics rather than for printing. P.P.S.: I'll never understand why Word & Co. don't support encapsulated postscript. With all that OLE and whatever, it can't be impossible to do as LaTeX / xdvi does...? -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Sweave/margin
On Fri, Mar 18, 2005 at 10:49:32PM +0100, Katharina Hoff wrote: > I am currently using Sweave for writing my bachelor thesis - and I have a > problem: > > I am using a LaTeX style (report) with quite big margin spaces. The Sweave > generated LaTeX code "floats" into the margin - and it looks ugly. The text > is blocked and fine... then there comes some flattering code running over > the margin... and blocked text again. > > Considering the LaTeX output, I guess that Sweave puts the source code > somehow in LaTeX-boxes and I suppose there is a place where I could change > the width of the source code boxes (At a certain point, there is a break, > closely before the text would drift out of the page. Then the code continues > in new lines below.) > > Does anyone know where I could change or insert the box width? > > Or probably I am totally wrong and someone knows another solution... > > Hoping for help - and excuse if anyone asked this stupid question before, I > did not find it in the archive, I assume you talk about R code lines extending into the right margin of pages. These are due to the formatting of such stuff using Verbatim environments. To gain control over code formatting, you can copy the pertinent lines from Sweave.sty into your document's preamble: \usepackage{fancyvrb} % \usepackage{Sweave} \DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl} \DefineVerbatimEnvironment{Soutput}{Verbatim}{} \DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl} \newenvironment{Schunk}{}{} The comment "% \usepackage{Sweave}" is needed, it suppresses the insertion of that usepackage line by Sweave. My Sweave manual just mentions that you are to define Sinput and Soutput, but in fact, both are wrapped by the currently (R 2.0.1) unused Schunk environment, providing you with a convenient handle for altering the font size, as e.g. in: \newenvironment{Schunk}{\tiny}{} Alternatively, you can make use of the fontsize parameter provided by the Verbatim package of LaTeX. Finally, you can always try to tweak your R code to consist of, and to produce shorter lines. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] removing message: [Previously saved workspace restored]
On Tue, Mar 08, 2005 at 08:51:44PM +, Federico Calboli wrote: > I saved by mistake the environment I was working in after typing q(), > and now I get the annoying message: > > [Previously saved workspace restored] > > I have already deleted all the objects in the environment, saving it as > an empty environment, so it's just a matter of nitpicking I suppose. The > message does not appear if I start R from any other place in the > directory tree. > > I am reading ?Startup and related docs, but I am utterly failing to > understand how to remove [Previously saved workspace restored] when I > call R from the offending dir... > > I am using R on Debian Sarge x86 There is a file called .Rdata, containing the saved workspace, in the "offending directory". Following Unix convention, the file is not normally displayed by ls and other programs because its name begins with a dot. See "Data permanency and removing objects" in the R-intro. If you don't want the saved stuff anymore, simply delete that file. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] simple if...else causes syntax error
On Mon, Mar 07, 2005 at 10:16:50AM -0500, roger bos wrote: > I am trying to do the simplest thing in the world. The following works: > > aaa <- ifelse(aaa==5, 6, 7) > > But if I want to change the if...else syntax instead, it gives errors > and assigns 7 to aaa. Here is the problem code: > > aaa <- 5 > if ( aaa==5 ) { >aaa <- 6 > } > else { >aaa <- 7 > } This is due to R's (somewhat peculiar) semantics of newline, which R interprets as a terminator if an expression can terminate at the position of the newline, or else as a plain whitespace, see section on "Separators" in the R Language Definition. In the construction if ( aaa==5 ) { aaa <- 6 } R decides that the final newline can be a terminator, namely of an if- expression without an else branch. So, the if-expression is consumed by the parser and "forgotten" for the purpose of associating the else branch with it. The else branch thus appears to be astray and is reported as a syntax error. All this does not happen if the entire construct is enclosed in braces. Alternatively, the "else" can be placed on one line with the brace closing the if branch. Out of personal interest: Does anyone here know why the R parser was designed this way? Personally, I have been coding in R for years in the belief that newline is whitespace, and never even noticed any problems because all my ifs with elses were within functions and thus enclosed in curly braces. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] return from nested function?
On Tue, Mar 01, 2005 at 11:21:44PM -0800, Seth Falcon wrote: > On Feb 25, 2005, at 12:34 PM, [EMAIL PROTECTED] wrote: > > >Is is possible from within a function to cause its caller to return()? > > This snippet may be of interest: > > > > f = function(x) { > + print("f") > + g(return()) > + print("end of f") > + } > > > g=function(x) {print("g") > + x > + print("end of g") > + } > > > f(1) > [1] "f" > [1] "g" > NULL I may be dumb today, but doesn't that beg the question of how does g cause f not to return? Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] basic question about changing limits on generated plots
On Wed, Feb 23, 2005 at 09:14:50PM -0500, rif wrote: > This does not do what the matlab code I posted does (the matlab code > also works in the free program octave, if you want to try). The > matlab code moves already plotted data within the window (replots it). > When I first type plot(1:10,1:10), I see a graph with axis limits [1 > 10 1 10]. When I type hold on (to keep my original data), and execute > plot(2:12,5:15), the plot I see is equivalent to the plot I'd have > gotten if I'd originally specified axis limits [1 12 5 15]. By > contrast, in the R code you sent, it's as if I'm superimposing two > unrelated plots. > > Essentially, the underlying "task" is that I want to compare multiple > functions, but I do not know good limits for the complete set of > functions when I start. Being able to adjust the graph to show all > the data I've plotted so far would be extremely useful for exploratory > analysis. This is the mode I and colleagues generally use matlab and > octave in. > > Does this question get asked all the time? It seems to be something > that would come up a lot for people who switch from Matlab/Octave to > R, but I searched the archives and didn't really see anything. FWIW, I use constructs such as plotfuncs <- function(x, func, ...) { y <- as.list(1:length(func)); for (i in 1:length(func)) { y[[i]] <- sapply(x, func[[i]]); } xlim <- c(min(x), max(x)); ylim <- c(min(sapply(y, min)), max(sapply(y, max))); plot.new(); plot.window(xlim, ylim, ...); for (i in 1:length(func)) { lines(x, y[[i]]); } axis(1, ...); axis(2, ...); box(...); } plotfuncs(1:100 / 100, list(sqrt, log, exp)) which allows you to add further functions incrementally, as in plotfuncs(1:100 / 100, list(sqrt, log, exp, function(x) {3 / (x + 1)})) Perhaps, that's what you have in mind, and probably, that's what (some) others do... Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Getting *types* of arguments?
On Thu, Feb 17, 2005 at 11:44:59AM +0100, Georg Hoermann wrote: > short question: is there a possibility to get a list of > arguments of a function *with* variable/parameter types? > > formals() gives me the names of the parameters, but says > nothing about the parameter type it expects (I know I can always use the > help function). > > I would like somthing like > > $x: vector or data.frame... I think you're looking for something that does not exist. There is no notion of a parameter type in R in the way you have it in C or Java. Whether or not a type is appropriate for a parameter cannot be decided before the function call is actually carried out. If all operations performed on the parameter turn out to be valid during function execution, the parameter type can be considered valid (at least in a syntactic view). Multiple types may well be syntactically valid in this sense, and worse, it may depend on the content of a parameter rather than on its type whether it is valid or not. For example, for foo <- function(x) { if (x[1] != "bar") x / 2 else x } you'd have to expect something like $x: a numeric vector, data.frame etc. or a character with x[1] == "bar" Clearly, no (finite) amount of syntactic analysis of a function's code can produce such a result, such information has to be provided in the docs. If you look for more strict typing, the methods package may be of interest, methods of S4 classes allow that. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Passing colnames to graphics title
On Wed, Feb 16, 2005 at 02:46:51PM +, Laura Quinn wrote: > Obviously I have been trying to use the colnames() function! > > However, when I try to subscript ie: > > for(i in 1:20){ > main=paste("Site:",colnames(i),sep="") ^^^ it looks to me that this should be something like colnames(foo)[i] where foo is the matrix or data.frame you use. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] graphics - current filename
On Mon, Feb 14, 2005 at 07:23:23AM -0800, Thomas Lumley wrote: > On Mon, 14 Feb 2005, Uwe Ligges wrote: > > >Paul Sorenson wrote: > > > >>I would like to query R for the current (or last used) filename for a > >>graphics device. > >> > >>Eg after png(filename="plot%02d.png") I would like something like the > >>output of dev.cur() but with the %02d expanded to the current name. > > > >You cannot, it is handled internally and the name is not returned. > >So you have to workaround yourself either by specifying filenames yourself > >and looping over the png() calls, or counting yourself ... > > Or just look on the disk with list.files(). > > sort(list.files(pattern="plot[0-9][0-9]\\.png"), > decreasing=TRUE)[1] As a note of caution: Don't use this technique if you intend to run your program more than once (unless you arrange for your program to remove any preexisting plot[0-9][0-9].png files before entering into the loop in question, which may also prove undesired some day down the line...). Otherwise, if you e.g. repeat the same thing that produces files plot01.png to plot99.png, the first run will work as intended, but the second run will deal with plot99.png in each cycle of the loop... The idea of querying the device for the current file name is basically correct, because the device would be the authorive source of that information, but as that isn't available, I'd second the recommendation to specifying the file names yourself. Then, you are the authoritative source. Kind regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Using a number as a name to access a list
On Thu, Feb 10, 2005 at 01:35:56PM -, michael watson (IAH-C) wrote: > I have a variable that contains the following numerical text "04010". > This is the name to access a list: > > > as.list(KEGGPATHID2NAME)$"04010" > [1] "MAPK signaling pathway" > > Marvellous! Except I want to do that when "04010" is assigned to a > variable called path and I can't figure out how to do it! > > > path <- "04010" > > > > # the original and best > > as.list(KEGGPATHID2NAME)$"04010" > [1] "MAPK signaling pathway" > > > > # clearly this doesn't, and shouldn't, work > > as.list(KEGGPATHID2NAME)$path > NULL $ allows only a literal character string or a symbol as the index, according to the R language definition, but [[ indexing does the same as $ and can be used for computed indexing. So as.list(KEGGPATHID2NAME)[[path]] should do what you want. Greetinx, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Process to both write to and read from (pipe/fork)?
Dear all, I would like to start a process from an R program in such a way that I can both feed input into the process and read the process's output. It seems that in R, I can have a pipe for writing into another process's input or a pipe for reading from another process's output, but not both. Doing both necessitates forking, such that the child can start the external process and feed that with some input, and the parent can read the output from the external process. Additionally, this requires obtaining a plain pipe, i.e. one with an input handle (for writing to) and an output handle (for reading from) prior to forking, so the child can connect the external process's stdout to the input handle and the parent can read that from the output handle. My problem is that I cannot find a way in R to set up such a pipe. For forking, I've found the fork package, but I can't seem to get a pipe. I know about the pipe function in the base package, but it seems to me that that is an interface to popen(2), rather than to pipe(2) (see attached C source). At least, I can't seem to get anything else but a popen equivalent from it. I've looked for a pipe package, for pipe in the fork package, and googled around, to no avail. For illustrating what I'd like to do, I attach a C program that replicates the pipe example from the base docs, with the difference that the input data to be processed with sed are present in the program rather than in a file. I start to feel silly and stupid for being unable to figure out such a basic thing. Thanks in advance for any help -- RTFMs very welcome. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* #include #include #include int main(int argc, char *argv[]) { int fd[2]; /* get a pipe, fd[0] is "input end", fd[1] is "output end" */ pipe(fd); /* fork a child process to feed something into the pipe */ if (fork()) { /* this is the parent process which reads from the pipe */ FILE *f = fdopen(fd[0], "r"); char buf[100]; close(fd[1]); while (!feof(f)) { if (fgets(buf, 100, f)) printf("got line: %s", buf); } fclose(f); } else { /* this is the child process which feeds something into the pipe */ FILE *f = popen("sed -e 's/,$//'", "w"); close(fd[0]); /* connect stdout to pipe's output end, so parent ends up reading the outut of sed */ dup2(fd[1], fileno(stdout)); fprintf(f, "450, 390, 467, 654, 30, 542, 334, 432, 421,\n"); fprintf(f, "357, 497, 493, 550, 549, 467, 575, 578, 342,\n"); fprintf(f, "446, 547, 534, 495, 979, 479\n"); pclose(f); } return (0); } __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Function to modify existing data.frame--Improving R Language
ing the function declaration or the docs. As an idea, making possible modification of parameter variables obvious in R could perhaps look something like mydata <- some.program(make.promise(mydata, allow.modification = TRUE)) > It probably wouldn't be that hard to produce something that looked like > a data frame but was passed by reference, by wrapping it in a > environment. This reminds me of Python, where everything is a dictionary... Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How to replace slashes with back slashes
On Wed, Jan 19, 2005 at 02:50:41PM +0100, Joerg Klausen wrote: > I am running R2.0.0 under Windows 2000. I am compiling a number of file paths > into a simple text file that will be read by some other software we use. > Unfortunately, it can only handle file paths with back slashes (MS Windows > convention), and from R, I get file paths with forward slashes. The following > didn't work. > > > gsub('/', '\\', 'c:/dir1/dir2/file.ext') > [1] "c:dir1dir2file.ext" > > gsub('/', '', 'c:/dir1/dir2/file.ext') > [1] "c:\\dir1\\dir2\\file.ext" > > I have tried to find an answer on R-help, but didn't ;-( Sometimes, you don't find an(other) answer because you already have one... ;-) In this case, gsub('/', '', 'c:/dir1/dir2/file.ext') actually does what you want. Notice that print doesn't just dump the string, it renders it in a representation suitable for the righthand side in an R assignment. You could convince yourself with cat(gsub('/', '', 'c:/dir1/dir2/file.ext')) which actually does dump the string. Greetinx, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Function to modify existing data.frame
On Tue, Jan 18, 2005 at 09:35:26AM -0800, Thomas Lumley wrote: > On Tue, 18 Jan 2005, Peter Muhlberger wrote: > > >I'm used to statistical languages, such as Stata, in which it's trivial to > >pass a list of variables to a function & have that function modify those > >variables in the existing dataset rather than create copies of the > >variables > >or having to replace the entire dataset to change a few variables. In R, I > >suppose I could paste together the right instructions in a function and > >then > >execute it, but is there any more straightforward way of doing this I'm > >missing? > There are two ways to get the effect you are looking for. I don't > recommend either, though. This leaves me wondering which way you actually would recommend? After all, one cannot entirely recommend against the concept of a function (sensu lato, perhaps) which modifies a few members of a (possibly very large) object in place, without copying the entire object in the process. > 1) Store your variables in an environment, rather than a data frame. > Environments are passed by reference. This is the approach used in the OOP package -- so, while implementing the data set as an object in this way is probably substantially "more" than what Peter asked for, wouldn't this be an interesting option to at least look into? > 2) The right combinations of eval() and substitute() and lazy evaluation > make it possible to write macros in R. There's an R Newsletter article > about this. Ok, this refers to the "defmacro" technique, if I dereference this call by reference correctly ;-) Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Function to modify existing data.frame
On Tue, Jan 18, 2005 at 09:31:33AM -0600, Marc Schwartz wrote: > On Tue, 2005-01-18 at 10:18 -0500, Peter Muhlberger wrote: > > I'm used to statistical languages, such as Stata, in which it's trivial to > > pass a list of variables to a function & have that function modify those > > variables in the existing dataset rather than create copies of the variables > > or having to replace the entire dataset to change a few variables. In R, I > > suppose I could paste together the right instructions in a function and then > > execute it, but is there any more straightforward way of doing this I'm > > missing? > What type of modifications? > > Take a look at: > > ?replace > ?transform I know neither the original poster nor Stata, but as I understand the question, this answer may not be exactly what Peter has been looking for. As the docs on replace state: x is unchanged: remember to assign the result. So, something like x <- replace(x, 4:6, 3:5 * 3) does not eliminate creation of a copy of x. A function that modifies its argument would require a call by reference mechanism, which isn't provided by R. Some time ago, I looked quite extensively for a call by reference facility in R, but found that there is none. In the archives of this mailing list and elsewhere, various alternatives to call by reference approaches are suggested. As far as I can see, the issues of performance hits incurred by unnecessary copying of large objects, and of implementing true state changes in object oriented programming cannot fully be resolved, however. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] thanks
On Tue, Jan 11, 2005 at 04:24:11PM +0100, Lefebure Tristan wrote: > example from a shell: > > echo -e "pdf(file=\"test.pdf\")\nplot(1:10,11:20)\ndev.off(dev.cur())\n">cmd.R > R -s > (write a file of command for R, and than feed R with it) This may be on the verge of becoming offtopic, but let me remark that the technique proposed here should be used for illustrative purposes only. For real life, use pipes: echo 'print(mean(rnorm(10)));' | R --vanilla This is equivalent to echo ''print(mean(rnorm(10)));' > cmd.R R --vanilla < cmd.R *as long as only one shell is executing this sequence at any given time*. The reason I mention this here is that I've seen it happen a few times that this "temporary command file" approach has made it from examples into shell scripts of which then, later on, multiple instances were run at a time, resulting in very rare, very irreproducible, and most inexplicable erroneous results. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] thanks
On Tue, Jan 11, 2005 at 03:59:58PM +0100, Cserh?ti M?ty?s wrote: > I would like to run an R script from the Linux prompt. Is there any way > possible to do this? The reason is, the calculation that I'm doing takes a > few hours, and I would like to automatize it. > > Or does it mean that I have to run source within the R prompt? > > Or is there a way to do the automatization within the R prompt? The standard way (well, my usual way, anyway) is to just use I/O redirection: linux> R --vanilla < stuff.r is, for the most part (see below), equivalent to linux> R > source("stuff.r"); The --vanilla option is necessary to suppress any interactive questions concerning workspace saving (i.e. the "Save workspace image? [y/n/c]" thing); differences between the automated and the interactive form may be due to your script depending on some saved environment, or some stuff in your init files. I'd like to encourage you to automate your calculations, as this enhances not only convenience but also reproducibility of your results. Best regards, Jan -- +- Jan T. Kim ---+ |*NEW*email: [EMAIL PROTECTED] | |*NEW*WWW: http://www.cmp.uea.ac.uk/people/jtk | *-=< hierarchical systems are for files, not for humans >=-* __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html