Re: [R] Data frames, passing by value, and performance (Matt Shotwell)

2010-05-24 Thread biostatmatt
R is pretty smart about duplicating only when necessary. That is,
arguments passed to a function are copy-on-write. Also, I think (someone
more knowledgeable please correct if I'm wrong) it may be better to use
the data frame, which is just a list internally, because if you only
modify one column, only that column is duplicated, not the entire data
frame. If you were to use a matrix, the entire matrix would require
duplication.

-Matt

On Mon, 2010-05-24 at 09:29 -0500, gschu...@scriptpro.com wrote:
> I understand that everything passed to an R function is passed "by
> value".  This would seem to include data frames, which my current
> application uses heavily, both for storing program inputs, and holding
> intermediate and final results.  In trying to get greater performance
> out of my R code, I am wondering if there is any clean way to access
> data frames without having them copied all the time.  Or is my only
> option to make them global, and write to them using <<-  ?
> 
> I have considered using matrices, but I like the self-documenting aspect
> of data frame column names.  Input/output to disk is not the issue here,
> as that does not take long in my case.  It's just the internal parameter
> passing that I'm concerned about.
> 
> (I've checked R-FAQ, R-lang and searched the R-help archives, but didn't
> see any specific mentions of this.)
> 
> Thanks.
> 
> Grant Schultz
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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


Re: [R] Wait for keystroke or timeout

2010-05-27 Thread biostatmatt


On Thu, 2010-05-27 at 19:08 -0400, Prof. John C Nash wrote:
> I would like to have a function that would wait either until a specified 
> timeout (in
> seconds preferably) or until a key is pressed. I've found such a function 
> quite useful in
> other programming environments in setting up dialogs with users or displaying 
> results,
> rather like a timed slideshow that can be speeded up by hitting a key.
> 
> Searching R-seek has led to wait() in the package 'audio', but when I try, 
> for example,
> 
> joe<-wait(readline("hit a key to continue"), timeout=6)
> 
> I am forced to wait the full timeout.
> 
> Probably someone has done this before and I'm just not using the right search 
> terms.
> Suggestions welcome.
> 
> Thanks in advance.
> 
> JN
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

John, 

What operating system do you use? I don't know of an existing solution
to this problem (although someone else might). However, I recently
participated in a discussion on the r-devel list (cc'd) here

,

about implementing a POSIX (Linux/Mac OSX) TTY (terminal) connection for
R to interface with a serial port. However, I am certain that a tty
connection for R could provide the functionality you are looking for, on
POSIX systems.

P.S. Maybe Simon (from the above discussion) has the right idea, that a
tty connection should exploit the full POSIX terminal API, then input
issues like this would have easy solutions... 

P.P.S The Win32 serial communications API

also provides a "wait or event" blocking mechanism.

-Matt

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


Re: [R] Wait for keystroke or timeout

2010-05-27 Thread biostatmatt
On Thu, 2010-05-27 at 19:08 -0400, Prof. John C Nash wrote:
> I would like to have a function that would wait either until a specified 
> timeout (in
> seconds preferably) or until a key is pressed. I've found such a function 
> quite useful in
> other programming environments in setting up dialogs with users or displaying 
> results,
> rather like a timed slideshow that can be speeded up by hitting a key.
> 
> Searching R-seek has led to wait() in the package 'audio', but when I try, 
> for example,
> 
> joe<-wait(readline("hit a key to continue"), timeout=6)
> 
> I am forced to wait the full timeout.
> 
> Probably someone has done this before and I'm just not using the right search 
> terms.
> Suggestions welcome.
> 
> Thanks in advance.
> 
> JN
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

John, 

What operating system do you use? I don't know of a general solution
to this problem (although someone else might). If you are using Linux
you can use try this

> system("read -t 1 -n 1")

where -n indicates the number of characters to read and -t specifies the
timeout in seconds. Hence, this command will wait until either 1 second
has elapsed, or 1 character has been read from the keyboard.

Also, I recently participated in a discussion on the r-devel list about
implementing a POSIX (Linux/Mac OSX) TTY (terminal) connection for R to
interface with a serial port. I am certain that a tty connection for R
could provide the functionality you are looking for, on POSIX systems.
Unfortunately this feature is currently only available as a source code
patch for R (not as an R package).

P.S. Maybe Simon (from the r-devel discussion) has the right idea, that
a tty connection should exploit the full POSIX terminal API, then input
issues like this would have easy solutions... 

-Matt

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


Re: [R] Calling fft from C

2010-05-30 Thread biostatmatt
The fft in R has the following C call chain

file  purpose
src/main/names.c  links R function "fft" to do_fft
src/main/fourier.cdo_fft calls fft_factor, fft_work
src/appl/fft.chome of fft_factor, fft_work

If you want to use the fft at a low level, you should use fft_factor and
fft_work. You can read about these functions in the fft.c file. In order
to use these functions in an R package, you must use the definitions in
the R/R_ext/Applic.h header. 

However, I believe the time limiting factor in an fft call is the work
performed by the fft_factor and fft_work functions. Hence, you are not
likely to save much time by calling them directly.

BTW, I think this sort of question is better suited for the R-devel
mailing list.

Matt Shotwell
Graduate Student
Division of Biostatistics and Bioinformatics 
Medical University of South Carolina

On Sun, 2010-05-30 at 21:35 +0200, Gunnar Hellmund wrote:
> Thanks for the reply, but ...
> 
> No, I am not interested in the convolution function defined in the
> R-extensions manual.
> I want to use a fast fourier transform version implemented in C
> returning values to another C function.
> So the question is: How do I call the fft supplied by R in C? (just
> like it is possible to use other R functions in C using #include
>  etc)
> 
> Best,
> Gunnar
> 
> On Sun, May 30, 2010 at 8:12 PM, Gabor Grothendieck
>  wrote:
> > The Writing R Extensions manual specifically uses convolve as an
> > example of calling C from R.
> >
> > On Sun, May 30, 2010 at 2:08 PM, Gunnar Hellmund  
> > wrote:
> >> Hi
> >>
> >> I have made a R function 'convolve2' for convolution of two real
> >> valued vectors based on Rs 'convolve' with option type="open" - see
> >> below.
> >> (exp.length and irf.length are variables set in another part of the 
> >> program)
> >>
> >> I wish to implement the function convolve2 in C and use it in a
> >> function used from R with .Call - e.g. I need to call fft in C.
> >> All I can find in the source code is do_fft in Internals.h - but how
> >> do I use do_fft? Or should I call another C routine (and how)?
> >>
> >> How do I solve the problem in the most appropriate way?
> >>
> >> convolve2=function2(x,y)
> >> {
> >> x<- c(rep.int(0,exp.length-1),x)
> >> n <- length(y<-c(y, rep.int(0, irf.length -1)))
> >> x <- fft(fft(x) * Conj(fft(y)), inverse=TRUE)
> >> return(Re(x)/n)
> >> }
> >>
> >> --
> >> Best wishes/bedste hilsner
> >>
> >> Gunnar Hellmund
> >>
> >> cand. scient., PhD
> >> http://staff.pubhealth.ku.dk/~guhe/
> >>
> >> ---
> >> “If everyone is thinking alike, then somebody isn't thinking.”
> >>
> >> __
> >> R-help@r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide 
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> 
> 
>

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