To run R in Unix using ssh from your PC: In the computer-lab click on the Putty window and login in Unity login.stat.ncsu.edu Then click on an ICON named XWin32 (A blue X icon) Go to the directory of interest, using the command cd in Unix % add R % R Computing on the R language and calling FORTRAN and UNIX -------------------------------------------------------- Topics: * computing on the R language and calling FORTRAN and UNIX * 3 approaches to call Fortran in R # assign function assign("zork", 1:5) # equivalent to temp<- get( "zork") # equivalent to temp zork<- 1:5 temp <- zork # The advantage of assign and get is that they take a # character string as the data set name EXAMPLE OF COMPUTING IN R: _________________________ # here is an example of creating a bunch of data sets # and looping over them using their names # first make them up dnames<- paste( "data", 1:10,sep=".") dnames # this a character vector with names that we want to use for the # 10 datasets # # Note that something like dnames[1] <- 1:5 # will NOT assign 1:5 to the data set data.1 ! What will happen? # # creating 10 variables. set.seed(123) # we will all get the same random numbers for(k in 1:10) { N<- rpois(n=1, lambda=100) # a value simulated from a poisson distribution cat( dnames[k], N, fill=T) # things to watch temp<-sort( runif( N)) # simulate N values from a Uniform and order the values assign( dnames[k], temp) # the assignment } data.1 #data.1 is a variable with sorted uniform values. PLOT THE DATASETS: # Now we want to loop through these to make some plots plot.data<- function(){ par(mfrow=c(5,2)) for ( k in 1:10) { temp<- get( dnames[k]) #get the dataset N<- length( temp) # find its length plot( temp, 1:N, xlab="time", ylab=" events", type="l") title( dnames[k]) } } plot.data() # Lets remove all these datasets remove( dnames) -------------------------------------1. Using Fortran within R ----------------------## How to call UNIX form within R: ---------------------------------system( "ls") # will list the UNIX files from your current directory system( "ls") system("who", TRUE) # Example in Unix to get netscape in R (you can SAS too). system('"C:/Program Files/Internet Explorer/iexplore.exe" "http://www.google.com"',wait=FALSE) Single quotes on outside allow file locations to contain spaces. # A very useful trick is to use the system command, R IO to run a # FORTRAN program. This is not the elegant way to call FORTRAN # from R but is very easy and quick. ###DOWNLOAD FROM THE WEB THE FILES: lab8.f, lab8.in, msort.f # msort.f has the subroutines that do the looping over columns and # the sorting # # # # % this is a stand alone program to read in a matrix and sort it compile it call the program lab8.x IN UNIX: f77 lab8.f msort.f -o lab8.x % cat lab8.in # some sample input # run it % lab8.x < lab8.in # redirect the output to a file % lab8.x < lab8.in > look cat look # # Now get back in to S # # In SPLUS now try > system( "lab8.x < lab8.in") 2. Another way to run fortran in R: -------------------------------# so here is a function to sort columns of a matrix in R # by running this program msort<- function(x){ m<- nrow(x) n<- ncol( x) write( c(m,n), "temp.in") # write out the number of rows and columns # to a UNIX file write( x, "temp.in", append=T) # append the values of x # run the program in the UNIX shell system( "lab8.x < temp.in > temp.out") # after it has run read in the results scan( "temp.out")-> hold matrix( hold, ncol=n, nrow=m) # return values as a matrix } zork <- matrix( 12:1, ncol=3) msort(zork) 3.The best approach --------------# # # # # this is not the best solution because of all the extra reading and writing of the matrix to files. A more efficient way ( but more difficult way) is to load just the subroutines ( in the file msort.o) and then call these directly from R # recall that the calling sequence for msort is # # subroutine msort( m,n,x) # real*8 x(m,1) # integer m,n # ... etc. msort2<- function( mat) { m<- as.integer( nrow(mat)) n<- as.integer( ncol(mat)) # make sure these are integers so that the FORTRAN is not confused # out<- .Fortran("msort",m=m,n=n, x=as.double(mat)) # .Fortran returns a list, but we only want the x component # reformed as a matrix matrix( out$x,ncol=n) } # now first dynamically load msort.o # in Unix (we compile msort.f) % f77 msort.f -c # # Now in R use the function (after you open access to the Fortran subroutine) dyn.load("msort.o") zork <- matrix( 12:1, ncol=3) msort2(zork) # Note that for either case msort or msort2 R is using a # FORTRAN sub to do the heavy lifting but this is transparent # at the command line level