Miscellaneous

How do I find the true URL of an FAQ?

The key is to find the accordion-### for the true URL.
/node/accordion-###

1. Find your way to a FAQ page that includes the QA of interest.
Here is an example using Chrome:
I am interested being a Major in Statistics. How can I apply?
Navigate to
/faqs?page=3
2. When on that page, right-clicking over the FAQ item, select "Inspect".
The tool window will highlight the accordion-### line.
(The 'x' on the top right will close this tool window.)
3. The question sits inside a div with an id of the form accordion-###.
<div id="accordion-10142"
Copy or make note of the four digit code (10142 in the example).
4. Form a URL using this code as follows:
/node/accordion-###. For the example, it's
/node/10142. This URL can be shared with the student.

FAQ Category

How to get Mouse to focus or FocusFollowsMouse on a MacIntosh?

This can be made to work only for X11 applications. The following seems to work. In your .bashrc file, add

  defaults write com.apple.x11 wm_ffm -bool true
  defaults write com.apple.Terminal FocusFollowsMouse -string YES

Logout and then login again. Open two xterm's and focus should follow mouse for the xterm's.

FAQ Category

Focus follows mouse

Open A Terminal (Black Icon).  You don't need admin right to run the command.
Type this:
defaults write com.apple.Terminal FocusFollowsMouse -string YES
Quit and restart Terminal program.

FAQ Category

How to read SAS data files int Splus?

Method 1. 

If you are using SAS version 6.12 in Unix... 

Step 1 
Output SAS data to the desired directory as usual. The file extension using SAS version 6.12 should be .ssd01. Suppose a SAS data file named myfile.ssd01 is located under /home/user/data/

Step 2 
In Splus 3.4 or above, use the function sas.get(). For example, if you want to name the data set as "mydata" in Splus, type the following at the Splus prompt: 

      mylibrary <- "/home/user/data/" 
      mydata <- sas.get(mylibrary,"myfile") 

For more detail, do help(sas.get)

OR 

In Splus 5 or above, use the function importData(). For example, if you want to name the data set as "mydata" in Splus, type the following at the Splus prompt: 

      mydata <- importData("/home/user/data/myfile.ssd01",type="SAS1") 

For more detail, do help(importData)

If you are using SAS version 7 in Unix... 

Step 1 
Output SAS data to the desired directory as usual. The file extension using SAS version 7 should be .sas7bdat. Suppose a SAS data file named myfile.sas7bdat is located under /home/user/data/

Step 2 
In Splus 5 or above, use the function importData(). For example, if you want to name the data set as "mydata" in Splus, type the following at the Splus prompt: 

      mydata <- importData("/home/user/data/myfile.sas7bdat",type="SAS7") 

For more detail, do help(importData)

If you are using SAS version 8 in Unix... see Method 2. 


Method 2 (recommended). Create a transport file from SAS (this works for SAS version 6.12 or above) 

Step 1 
In the SAS code, include the following line when defining SAS libraries: 

      libname sasdata xport "/home/user/data/file.tpt"; 
 

  • "libname" and "xport" are required in the syntax
  • "sasdata" is the user-defined library name
  • "/home/user/data/file.tpt" is the full path name of the transport file (Note the .tpt extension!). "file.tpt" is the file you want to output from SAS and later to be read into Splus


Don't forget to name the file to be outputted in a SAS procedure as "sasdata.file.tpt" in the SAS code!

Step 2 
In Splus 5 or above, use the function importData(). For example, if you want to name the data set as "mydata" in Splus, type the following at the Splus prompt: 

      mydata <- importData("/home/user/data/file.tpt",type="SAS_TPT") 

For more detail, do help(importData)

Note: 
1. Splus 3.4 does not have the importData() function. 
2. The sas.get() function may not read data files created by SAS of version 7 or above. 
3. The importData() function may not read data files from SAS of version 8 even though the file suffix can be the same as version 7 files (.sas7bdat). 
4. For Splus 4+ in Windows, use the import.data() function instead.

FAQ Category

How to read Microsoft Excel format (.xls) data file by R?

There seem direct way to read .xls format file (see http://maths.newcastle.edu.au/~rking/R/help/00b/2519.html).

However, there some ways to indirectly read .xls file. For example, you can save the .xls file into .csv (comma separated value) format. Then use R's function read.csv to read it.

The reason to save .xls file to .csv file is that usually there are some columns in .xls file which are strings containing white spaces. So if to save .xls file to white space delimited or TAB delimited, then it is still difficult to read the file into R.

FAQ Category

How to call Fortran subroutines in R?

In R, we can call Fortran subroutines. For example, we have the following toy Fortran subroutine in the file test.f.

CCCCCCCCCCCCCCCCC
C      The subroutine is to calculate Hadama product of two matrices.
C      out[i][j]=x[i][j]*y[i][j].
C      Both R and Fortran store matrix by column.
CCCCCCCCCCCCCCCCC

CCCCCCCCC Fortran program (f77) has to be between 7-th and 72-th column.
CCCCCCCCC The 6-th column is for continuation marker.

         subroutine myHadamaProduct(x, y, nrow,  ncol, mo)
           integer i, j, nrow, ncol
CCCCCCC In Fortran, you don't need to specify the second dimension for matrix
           double precision x(nrow, *), y(nrow, *), mo(nrow, *)

           do i = 1, nrow
            do j = 1, ncol
              mo(i,j)=x(i,j)*y(i,j)
            enddo
           enddo
           return
           end
  1. First, we need to compile the file test.f to create a shared library, test.so say, by using the GNU Fortran compiler:

     

        g77 -fpic -shared -fno-gnu-linker -o test.so test.f
    
  2. Next, we need to use the R function dyn.load to load the shared library test.so.

        if(!is.loaded("myhadamaproduct")){ dyn.load("./test.so") }
    

    The R function is.loaded is to check if the Fortran subroutine myHadamaProduct is already be loaded to R. If yes, then we do not need to loaded it again.

  3. Next, we use the R function .Fortran to call the Fortran subroutine myHadamaProduct. For example,

        x<-matrix(1:10,nrow=5, ncol=2) # get a 5x2 matrix
        y<-matrix(1:10,nrow=5, ncol=2) # get a 5x2 matrix
        out<-matrix(0, nrow=5, ncol=2) # initialize output matrix
    
        # to format matrix or array, use function storage.mode()
        storage.mode(x)<-"double"
        storage.mode(y)<-"double"
        storage.mode(out)<-"double"
        nr<-as.integer(nrow(x))
        nc<-as.integer(ncol(x))
    
        # Fortran is *NOT* case-sensitive. So it will change the all characters
        # to lower case. Thus, to use .Fortran call Fortran subroutines, you
        # have to type lower case. Otherwise, R will prompt error message.
        res<-.Fortran("myhadamaproduct", x, y, nr, nc, out=out)
    
        cat("Hadama product >>n")
        print(res$out)
    
  4. If you do not need to use the shared library test.so any more, you can use the R function dyn.unload to unload it.

        if(is.loaded("myhadamaproduct")){ dyn.unload("./test.so") }
    

Note:

  • The Fortran program called by R must be subroutines, not functions. For the example above, myHadamaProduct is defined as subroutine.

             subroutine myHadamaProduct(x, y, nrow,  ncol, mo)
    

    The arguments in Fortran subroutines are passed by address instead of by values. And not like C language, there is no "pointer" concept in Fortran.

     

  • When you use ".Fortran" to call Fortran subroutines, the name of the Fortran subroutines must be in lower case.
  • Any values returned by Fortran subroutines which are called in R must be initialized and must have the format:

        # if the variable is defined as double in the Fortran subroutine
        variablename=as.double(initialized values) # inside .Fortran
        # if the variable is defined as integer in the Fortran subroutine
        variablename=as.integer(initialized values) # inside .Fortran
        # if the output is double precision matrix or array
        storage.mode(variablename)<-"double" # before .Fortran
        variablename=variablename # inside .Fortran
    

    The input values must also be initialized and must have the above format. However, they can be formated before the ".Fortran" function.

     

  • If the output is not written as variablename=variablename format (e.g. out=out in the above example), You still can get results. However, you have to use res[[5]] to refer out in the above example. In fact, the .Fortran function return a list containing all arguments of the Fortran subroutine myHadamaProduct. Since out is the 5-th argument, you can use res[[5]] to refer to the 5-th elements of the list.
  • It is okay that the file test.f contains the main program.
  • Sometimes, the command "dyn.load("test.so")" gets error message. This is probably caused by the environment variable "$PATH" was not set correctly. You can either add the following line to the file ".bashrc" in your home directory:

        export PATH=$PATH:.:
    

    or use the command

        dyn.load("./test.so")
FAQ Category

How to call C functions or Fortran subroutines in Splus?

There are different versions of Splus installed in the department computing system. Splus is installed in Hajek, Newton, Emily, and Statlab. Splus 5 Splus 6 are installed in all servers.

  • For Splus 3.4, the function to load the shared libraries is

        dyn.load.shared
    

    instead of dyn.load. The function dyn.load is used to load the object functions such as test.o obtained by using the command "g77 -c test.f". There is also no dyn.unload function in Splus 3.4.

  • For Splus 5 and Splus 6, the function dyn.load and dyn.load.shared are obsolete. Splus 5 and Splus 6 use the function dyn.open to load the shared libraries and the function dyn.close to unload the shared libraries.
FAQ Category