Wednesday, February 5, 2014

How to sync files and folders outside the dropbox folder

To sync a folder outsider the dropbox folder, we need to create a symbolic link of the target folder to the dropbox folder.

On windows:
Use either the JUNCTION utility from Sysinternals, or the MKLINK command built in to Windows Vista and Server 2008, for example:
junction "C:\Documents and Settings\User\My Documents\My Dropbox\DesiredFolder" "C:\Path\To\DesiredFolder"
mklink /D "C:\Users\Steve\Documents\Dropbox\DesiredFolder" "C:\Path\To\DesiredFolder"
Or, if you prefer a GUI, install Link Shell Extension.
[You could also] use SyncToy to echo changes from another folder to your Dropbox folder. This keeps 2 copies on disk though.

On Mac:
Use the ln command, for example:
ln -s /path/to/desired-folder ~/Dropbox/desired-folder
This works with files too:
ln -s /path/to/desired-file ~/Dropbox/desired-file
Another easy way to do this with Terminal is type the ln -s part, then from Finder drag the folder/file that you want into the Terminal window then drag the Dropbox folder and hit return.
Note that an Alias file or folder does not work.

Wednesday, September 4, 2013

Some notes for building R packages for CRAN

Today I tried to build my own R packages to submit on CRAN. I would like to document the process and some errors I encountered for future reference:

First, about programming and file preparation:

1-1. It is preferred to use object-oriented programming (oop) to enhance readability and to control errors. Commonly adapted OOP methods are S3 and S4 methods. S3 is more flexible but does not filtered errors as good as S4. S4 method is more organized.

1-2. For example data files, save the file object in list or data.frame for easy calling. For example: if the data contains objects x and y, I save them as
> mydata = list(x=x,y=y)
> save(mydata,file='mydata.rda')

Then calling the data in examples using:
> data(mydata)
> attach(mydata)

Note: attach only works for classes 'list' and 'data.frame'.

1-3. In R, use the following code to generate skeletons of R package:


package.skeleton(name_of_package,code_files='name_of_code_files',environment=.GlobalEnv,path='folders_to_put_the_R_package_skeleton',force=TRUE).

Note: I like to put all functions in one file: 'name_of_code_files.R', so that I just need to send the name of the function to R file. Otherwise we need to create a vector of function names :

package.skeleton(name_of_package,list=c(fn_1,fn_2,...),environment=.GlobalEnv,path='folders_to_put_the_R_package_skeleton',force=TRUE).


After get the skeleton, we need to edit the package files.

2-1. Description: 
             - fill in the information and choose license as 'GPL-3' or 'GPL(>=2)'. 
             - in 'Depends', put in the dependence packages and R version, e.g. R (>=1.10.2)
2-2. Namespace:
             - export(fn_1,fn_2,...)  
               # list those functions visible to the users and all the functions needs to be documented.
             - S3method(method, class)
               #list S3 method. If we have a function print.goat that uses S3 method of 'print' on an  
               #object of class goat, then we need to write S3method(print,goat)
2-3. .Rd file (manual file): edit manual file based on the template

Finally, build, check and install the files in terminal:
3-1. To build a '.tar.gz' file, type
               R CMD build package_name
       To install a package, type
               R CMD install package_name
       To check the package, type
               R CMD check package_name

3-2. Make sure to check the file before submitting to CRAN. CRAN only accepts packages without check warnings or errors.  Here are some steps that are likely to have errors:

     - checking S3 generic/method consistency ... 
           Make sure the S3 method has the same argument name in the manual file as the original 
           function. For example: print.goat (x,...) (has to be x instead of y)

     - * checking PDF version of manual ... WARNING
     > LaTeX errors when creating PDF version.
     > This typically indicates Rd problems.
     > * checking PDF version of manual without index ... ERROR
          Check if pdflatex is in the current path. In terminal, type 

                         pdflatex --version 

          If an error occurs, pdflatex is not under the current path. We need to add it to the path by 

                         export PATH=/usr/texbin:$PATH

       - if the log file says 
            ! LaTeX Error: File `Rd.sty' not found. 

          Rd.sty is a style file provided by R to handle some of the macros used in help pages.  You           can have R tell pdflatex where to find it by using 
        
                        R CMD texify --pdf package.tex 

          Alternatively, if your package is in directory "package", you can do 

                        R CMD Rd2pdf package 

          and it will produce the .tex and texify it all in one step.   I believe both will leave the log             file for you to examine if there's an error, but  you can force that using the "--no-clean"             command line option. 

Here is a good tutorial for building R packages: https://groups.google.com/forum/#!topic/mystat100/JxGR0YcYnZw

Thursday, August 29, 2013

Convert powerpoint file to high resolution .tif file in Mac

Powerpoint is a powerful tool for making versatile plots for publication, which can be exported as low resolution figure files. Many journals, however, requires high resolution .tif file for publication. Here is how to convert .pptx or .ppt file (in Powerpoint 2011) to .tif file in Mac OS.


1. Convert .ppt file to pdf file:
a.     File -> “Reduce file size” -> In “picture quality” dropdown box, select “Best for Printing (220ppi)” -> click “OK”
b.     File -> Save As -> In “format” dropdown box, select “PDF” -> In “Options” , enter the largest number possible for width and height -> Click “OK” ->  Click “Save”

      2. Convert .pdf file to .tif file. This can be done in two methods.

2-1 .The first method is open .pdf file in Adobe Acrobat Pro, then
a.     File -> Save As -> in “Format” dropdown box, select “TIFF”
b.     Click “Settings” to the right of “Format”. In the dialog box,
                                               i.     select “LZW” for both “Grayscale” and “Color”,
                                             ii.     select “Color:RGB” in “Colorspace”
                                            iii.      in “Resolution” box, select “300 ppi” or higher.
2-2. The second method needs Photoshop.
c.     File→Open the PDF. You will need to do this one page at a time. Make sure you're importing it at 300ppi, RGB.
d.     Use the Crop Tool (fifth from the top of the toolbar) to select an area close to the borders of your image. Hit Enter to apply the crop.
e.     Layer→Flatten Image
f.      Image→Image Size. Uncheck the Resample Image checkbox. If the Width is over 17.35cm, type 17.35 in the Width box (17.35cm is our maximum allowable width for figures). The Resolution will go up automatically as the Width decreases. If the resolution does not hit 300 when you make the Width 17.35, type 300 in Resolution and as long as Width doesn't go below 8.3cm, everything is fine. Also, the height cannot be more than 23.35. If the Height and Width are within these prescribed limits, no adjustment to your figure size needs to be made.
g.     File→Save As. Save as TIFF, Image Compression set to LZW, Pixel Order set to Interleaved, Byte Order set to IBM PC.


Tuesday, August 6, 2013

Latex template

This webpage presents a good collection of latex template, including a variety of writing styles:
http://www.latextemplates.com/

Monday, August 5, 2013

Matlab gcc compiler installation for Mac Mountain Lion

Today I was trying to mex functions in matlab but an error occurs: "gcc-4.2: command not found". Xcode 4.2 or later does not have gcc-4.2 although it is required for matlab/python programming, so users have to install the compiler manually. I found a useful discussion regarding this issue

http://www.mathworks.com/support/solutions/en/data/1-FR6LXJ/

Allow me to copy and paste the solution here:
LAST UPDATED: 2013/02/04
If you have downloaded patches from this solution before this date, and still experiencing issues, please re-download and reapply the patches below, as they have been updated with additional fixes. Note that the "Date Last Modified" value shown at the top of this solution may be different, due to minor edits that do not affect the patches.

Update History:
2013/02/04: added Xcode 4.6 support.
2012/10/17: added Xcode 4.5 support and Simulink patch link.
2012/09/13: added R2012b support.
2012/09/05: added patch for Mac OS X 8.

*** Scope of this solution ***
This solution concerns the Mac platform only, with the following software versions only:
MATLAB versions:
- R2011a
- R2011b
- R2012a
- R2012b

AND

Xcode versions:
- 4.6
- 4.5
- 4.4
- 4.3
- 4.2
- 4.1

AND

Mac OS X versions:
10.6 (Snow Leopard), for Xcode versions 4.1 and 4.2 only
10.7 (Lion), for Xcode versions 4.1 to 4.6 only
10.8 (Mountain Lion), for Xcode versions 4.4 to 4.6 only

Do not apply the steps described in this solution to any other combination of MathWorks and Apple software.

For MATLAB releases before R2011a, use an older compatible version of Xcode as documented in the page “Supported and Compatible Compilers - Previous Releases”:

http://www.mathworks.com/support/sysreq/previous_releases.html

---For Simulink Users---
For Simulink users utilizing Accelerator, Rapid Accelerator and Code Generation functionality, refer to the solution 1-HDKZEH (linked below) to download additional Simulink patches AFTER applying the MATLAB patches below.


*** Description of the issue ***
If you use one of the above listed MATLAB releases with one of the above Xcode releases, you may see an error message like the following when using MEX, or attempting to use any other feature reliant on the presence of a C/C++ compiler:

1.
/Applications/MATLAB_R2011b.app/bin/mex: line 305: gcc-4.2: command not found
This is happening because MATLAB is expecting GCC 4.2 to be present. This compiler was distributed with Xcode 4.0 and 4.1, but is no longer supplied as of Xcode 4.2.

However, Xcode 4.2 and later include a similar compiler (GCC 4.2 front-end to LLVM) that MATLAB can be instructed to use instead. Applying the patch attached to this solution will instruct MATLAB to use the compiler supplied with Xcode 4.2 or later.

2.
/Applications/MATLAB_R2012a.app/extern/include/matrix.h:852:20: error: stdlib.h: No such file or directory
This is happening because the SDKROOT directory for Xcode 4.2 and earlier are:
/Developer/SDKs/MacOSX10.7.sdk/but Apple changed this directory to
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/in its Xcode versions 4.3 and higher. Applying the patch sets the correct SDK root depending on the Xcode compiler present on the computer.

You may need administrative permissions on your Mac to complete these steps.


*** Before applying the patch ***
1. Ensure your Xcode is installed in the default directory and has the "Xcode.app" default name. For Xcode 4.3 and later, this is:
/Applications/Xcode.appFor Xcode 4.1 and 4.2, this is:
/Developer/Applications/Xcode.app
2. If using Xcode 4.3 and later, ensure that the Xcode "Command Line Tools" are installed. To do this:

Since Xcode 4.3, the command line tools needed by MATLAB must be downloaded after the installation of Xcode itself. This does not happen by default. In Xcode, go to Preferences -> Downloads -> Components, and check “Check for and install updates automatically” and install the “Command Line Tools”.

3. After installing the "Command Line Tools" above, confirm that the following command (in the MATLAB command window)
!xcodebuild -versionreturns the proper version of your Xcode. If this command does not return the version of Xcode, your Xcode was either installed incorrectly or installed in a non-default location.

4. If you have multiple versions of Xcode, make certain that the version you want to use for MATLAB has the default "Xcode.app" name, is in the default installation directory, and that this version has the "Command Line Tools" installed, i.e., the Xcode version must be the same for all three steps above. Note that only one version of "Command Line Tools" can be installed on a machine at any given time (installing it from different versions will overwrite the previous version).


*** To apply the patch ***
1. Download the appropriate patch for your MAC OS X version, attached to this Solution:

optsPatch_MACOSX6.patch
optsPatch_MACOSX7.patch
optsPatch_MACOSX8.patch

These instructions assume you download it to the default location Safari uses, namely the “Downloads” folder of your home folder.

2. Start MATLAB, if it is not already running.

3. In the MATLAB Command Window, type:
>> cd(matlabroot)
>> !unzip -o ~/Downloads/optsPatch_MACOSX7.patch

or

>> cd(matlabroot)
>> !unzip -o ~/Downloads/optsPatch_MACOSX6.patch

or

>> cd(matlabroot)
>> !unzip -o ~/Downloads/optsPatch_MACOSX8.patch

depending on the patch downloaded. Administrator privileges may be required for this step.

4. Set your compiler configuration by running:
>> mex -setup
5. If you use MATLAB Compiler, run the command:
>> mbuild -setup
MEX, MATLAB Compiler, and other MathWorks products should now be configured to use the compiler provided with Xcode 4.2 or later.


*** Troubleshooting ***
If, after installing the patch, you get an error such as this:
/Applications/MATLAB_R2011b.app/bin/mex: line 305: llvm-gcc-4.2: command not foundyou should check to make sure that a supported version of Xcode is installed on your machine. Launch Xcode, either from the Dock or from your Applications folder.

If Xcode launches, click "About Xcode" to make sure the version is within 4.1 to 4.6:
Can I use Xcode 4.2 to 4.6 as my C or C++ compiler in MATLAB? (Updated: 2013/02/04)

If you cannot find Xcode, cannot launch it, or the version is not Xcode 4.1, 4.2, 4.3, 4.4, 4.5 or 4.6 you should reinstall Xcode and repeat steps 4 and 5 above.

If Xcode launches, but issues remain, confirm that all the prerequisites listed in the "Before applying the patch" section above is followed. 

If after the above steps, Xcode is installed correctly and works as intended, but it still cannot be used in MATLAB, try applying an older version of the patch below:

Xcode4_OLD.patch
(For MAC OS X 6, 7, or 8 with Xcode 4.2 or higher)

WARNING: This patch does not set the SDKROOT property and may fail depending on your code to be compiled. Only try this patch as a last resort, if the other OS specific patch fails and Xcode is tested to be working properly outside of MATLAB.

*** Note *** 
The patches attached to this solution can be used with any combination of MATLAB release / Xcode version as described in the scope of this solution (see above).




Wednesday, February 6, 2013

Open Access Library

Currently free access to 125,546 academic articles.

http://www.oalib.net