[R] R stops responding/communicating in for loop (lengthy description of issue)
Robert D. Bowers
n4fbz at tampabay.rr.com
Fri Apr 15 19:08:37 CEST 2016
Yeah, this is a bit lengthy, but it's a vexing problem.
First, I'm working on learning R, mainly by using it and coming more
from a programming aspect (I have the books and have gone through them,
but learn best by doing). I have multiple projects going where R is
almost necessary. I learned C a few years ago, but am very rusty with
it (and other languages back in the 70s). I also am a doctoral student
and have taken doctoral stats using SPSS, but using the GUI only and for
what I want to do, it won't be as useful. My proposed dissertation
research will probably require R for some rather esoteric statistical
manipulations which have proved difficult with SPSS (in a pilot study I
did). As you probably can tell, I'm no R guru... more on the lines of a
beginner.
My platform:
Dell Optiplex quad 3.4Ghz 16Gb ram 1Tb hard drive.
Linux Ubuntu 14.04LTS 64 bit
R (from the repositories) version 3.0.2
using RKWard for programming (and testing) version 0.6.1 (from repositories)
using socat to help establish communications - command is: socat
UDP-DATAGRAM:0.0.0.0:19004 PIPE:"usbsoft" (setting up a pipe between a
udb connection and R - which needs to access the pipe using FIFO() as I
learned). Version 1.7.2.3 from the repositories
I can run the target software (which has a UDP option) in R in the loops
reliably using system(), but each loop takes between 3/4 and 1 1/2
seconds. Using the UDP option, I can run between 20 and 50 loops per
second. I've tried using other forms (such as socket() and pipe() but
they didn't work.
The program runs and locks up at different points (unpredictable) - I
have to shut down and restart RKWard to "unlock". I CAN save and edit
the program in RKWard even when the R console is locked up. I've also
tried running the program directly from the R command line, without
success (but haven't tried with the most recent iterations). In every
case there are no error codes and seemingly no problems with socat or
the final software. I've been tweaking the code here and there, with
some improvement in the problem, but it's not completely gone.
I HAVE observed that sometimes the program runs through without
problems, often when I don't have any other major programs running in
the background (Firefox, Thunderbird, Google Earth, etc.).
My program/routine (and I'd like some feedback if there are easier ways
to do this):
First I start the program with the UDP interface
Then I start socat
___________________________________________________________________
test=c(1:50)
frequency=c(5+(1*1:500))
response = c(1:500)
number=c(1:500)
dBm=0
newline=('\n')
questionmark="?"
out=fifo("usbsoft",open="w")
volt=file("/dev/ttyUSB0",open="r+w")
for(j in test){
writeChar("set freq 6.0000",out)
Sys.sleep(1)
for(i in number){
print(frequency[i]) #used to determine where the loop stops
writeChar(questionmark,nchars=nchar(questionmark,type="chars"),volt,eos="\n")
dBm=readChar(volt,n=5,useBytes=FALSE)
response[i]=as.integer(dBm)
freq=paste("set freq ",frequency[i])
writeChar(freq,out,nchars=nchar(freq,type="chars"))
flush(out)
Sys.sleep(.05)
} #sometimes this loop doesn't finish on first try,
sometimes I'll get one successful loop, sometimes 3-4.
testno=data.frame(response)
testout=paste("/home/bob/TMP/test",j,".csv")
write.csv(testno,testout)
num=paste("test Number ",j)
print(num)
} #A few times this loop has successfully completed. Most of the
time it doesn't.
writeLines("\"quit\"",con=out,sep="\n") (Shuts down the UDP connected
software and clears the service.)
experiment=data.frame(frequency)
# experiment
write.csv(experiment, file="/home/bob/TMP/scanner-frequency.csv")
close(volt)
close(out)
___________________________________________________________________________
Basically, I'm using R to get data from some equipment modules I've come
up with and storing the result in a series of vectors (in csv form in a
folder). Then I'm building a spreadsheet (at the end) using all of the
different vectors so as to determine various factors about the
equipment. I'd really like to be able to run the loops and build a
single spreadsheet, but haven't figured that out yet.
_The statement flush(out) seems to help a little._ The Sys.sleep()
statements are to allow the UDP software link to "catch up" (too fast
and the equipment/software can't keep up and it appears that the buffer
starts filling up) - but not connected to the problem I'm asking help for.
It shouldn't be taking up THAT much memory. However, I've read of
similar problems with the for() loop in R when working with big data.
I've pretty well isolated it to some issue in R... maybe I don't have
something set right.
Thanks for any help!
Bob
[[alternative HTML version deleted]]
More information about the R-help
mailing list