R: fixed global var issue