You are here

hangovers from Mx

2 posts / 0 new
Last post
tbates's picture
Offline
Joined: 07/31/2009 - 14:25
hangovers from Mx

There are places in the demo scripts that inherit things from Mx that are not necessary: like the fact that Mx couldn't read files with a labels line..

So in , we see:

data(twinData)
twinVars <- c('fam','age','zyg','part','wt1','wt2','ht1','ht2','htwt1','htwt2','bmi1','bmi2')

Much easier to say

data(twinData)
twinVars = names(twinData) 
# maybe with a comment so people know what is going on
# 'fam','age','zyg','part','wt1','wt2','ht1','ht2','htwt1','htwt2','bmi1','bmi2'

Also enhances script re-use, avoids maintainance hassles and error.

Steve's picture
Offline
Joined: 07/30/2009 - 14:03
Agreed. Good practice to add

Agreed. Good practice to add the comment too.

In my R code I always have a "variables block" so that I can go back and see what I've done. It helps when I go back to code from 1992 (yes, I do have S code from 1992).

I learned to code in assembly and C, so declaration blocks seem friendly to me. I put this in a comment in R since there is no need for a declaration block in R.

I start all of my R scripts with a comment block that looks something like this. I use a Textmate template that spits out the block so all I need to do is fill it in. Note that I name my scripts with an included date in reverse order so that file names also sort themselves by date when they are sorted alphabetically.

# ---------------------------------------------------------------------
# Program: MyScript20090919.R  
#  Author: Steven M. Boker
#    Date: Wed Aug 19 14:06:52 EDT 2009
#
# This program is an example header for posting to the OpenMx forum.
#
# ---------------------------------------------------------------------
# Revision History
#   Steven M. Boker -- Wed Aug 19 14:06:52 EDT 2009
#      Created MyScript20090919.R.
#
# ---------------------------------------------------------------------

# ---------------------------------------------------------------------
# Variables 
# ---------------------------------------------------------------------
# fam  = The family ID number
# age  = Age of the twin pair
# zyg  = Zygosity of the twin pair
# etc  = You get the picture 
# ---------------------------------------------------------------------

I find these to be useful programming habits that have helped me over a span of decades.