Hi,
Using R 2.8.0 on Windows,
I have a mathematical model written in C++, the model writes to file (.txt) a set of numbers I want to plot in R.
The model iterates over about 10,000 runs, each time overwriting the old with the new set of results to the output file.
I want to be able to continuously plot the output from the model in R each time a new run generates data.
At the moment I tried cheating a little by creating a continuous loop which constantly scans another output file from the model which contains the current run number, when the run number changes, R then goes on to plot the new C++ output.
The 'Quick' code I tried which works to a certain extent, although very bad idea I am sure,
The idea is I strt R first and then run the C++ Model
#Make sure the C++ run number output file is zeroed
write.table(0, file = "C://rdata//Outfile5.txt",quote = F, row.names = F,col.names = F)
#Set up legend and colors
leg.txt<-c("Male Younger", "Male Older", "Female Younger", "Female Older")
leg.cols<-c("cyan","blue","pink","magenta")
#The number of runs for the C++ model
runs=10000
#Loop corresponding to the number of runs in the C++ model
for(i in 1:runs){
# set / reset the plot variable to 0 (no plotting)
plot<-0
#read the current run number from the C++ model
nogo<-read.table("c:\\rdata\\Outfile5.txt",header=F)
#This loop tries to implement a continuous scan of the C++ output file containing the current run number
for(i in 1:1000){
# Reads the current run number from the C++ file
go<-read.table("c:\\rdata\\Outfile5.txt",header=F)
#Tried to make a continuous loop by reseting i from the for loop to 1 again (which doesnt work)
i<-1
#Pause the whole thing for a while
Sys.sleep(0.2)
# If the C++ model run number has been updated
if(go==nogo+1)
{
# Set the I from the for loop to max so we get an exit
i<-1000
# set plot variable to 1 (Plot)
plot<-1
}
}
#If the plot variable is one indicating plot the data
if(plot==1){
#read the C++ model output for this run
data<-read.table("c:\\rdata\\Outfile3.txt")
#Plot the data
plot(data[1:41,2],data[1:41,3],type="l",ylim=c(0,0.3),col="cyan",xlab="Years",ylab="Proportion HIV Infected",lwd=2)
legend(x=0,y=0.08,leg.txt,fill = leg.cols)
lines(data[1:41,2],data[1:41,4],col="blue",lwd=2)
lines(data[1:41,2],data[1:41,5],col="pink",lwd=2)
lines(data[1:41,2],data[1:41,6],col="magenta",lwd=2)
}
}
Hope I am making sense in my question (If not in my code).
Any help welcome.
Many thanks
Andrew Paul Cox
Mathematical Modeller
London School of Hygiene and Tropical Medicine
WC1E 7HT
Plotting C++ output in R
2 messages · Andrew.Cox at lshtm.ac.uk, Warren Young
Andrew.Cox at lshtm.ac.uk wrote:
I want to be able to continuously plot the output from the model in R each time a new run generates data.
From the C++ program, run the R script that plots the data. Something
like this:
system("Rscript myplotter.R");
That assumes Rscript is in the PATH, and that it works on Windows. I'll
assume you can figure out the details.
You might also consider making the C++ program write the R script, so
bits can vary. For instance, each iteration of the model could write
out to a different data file, then call R from a new thread, so the R
analysis stage doesn't interfere with the generation of the next
iteration's results. Remove the now-unwanted data file when system()
returns, and end the thread.