top_dir: Set the top-level directory for a system

Description Usage Details Author(s) See Also Examples

Description

You can explicitly set the directory by providing the argument .top.dir with a path, or you can call the function without an argument and top_dir will make an operating-system specific guess as to the appropriate path.

Usage

1
top_dir(.top.dir = NULL)

Details

This is necessary for the sweave script's operation because it needs to know the top level and set it before it calls R.

.top.dir is usually ~/tc on Linux and c:/tc on Windoze.

Regardless, once top_dir is called it assigns the chosen path to the variable .top.dir in .GlobalEnv

Making a call to top_dir with no arguments will return the value of the variable .top.dir that has been assigned in .GlobalEnv, i.e. it acts an enquiry only.

@param .top.dir character directory name other than default

.top.dir

Why does top_dir assign the variable .top.dir to .GlobalEnv? This is simply because top_dir takes an argument .top.dir which can be different from the default. Typically in a project the top-level directory is defined once, and this may be different from the default, say "c:/" on Windows instead of "c:/tc". Instead of calling top_dir as top_dir(.top.dir="c:") this call can be made once at the start of the code block and subsequent references to it can be made via the variable .top.dir instead of calling the function explicitly each time as top_dir(.top.dir="c:"). This isolates the change in the top-level directory to the variable .top.dir.

Thus the calling convention is to establish the top-level directory with a call to top_dir either explicitly naming the top-level directory, e.g. top_dir(.top.dir="c:") or relying on the default for the system using top_dir(). After this all references to the top-level directory can be made through the variable .top.dir.

Author(s)

Thomas P. Harte

See Also

Sys.info

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
	# set the top-level directory to the system default
	# (every project should start this way, the value .top.dir
	# is assigned in .GlobalEnv
	`check_top_dir`<- function() {
		print( whos(sort="Name", env=.GlobalEnv) )
		if (exists(".top.dir", env=.GlobalEnv)) {
		get(".top.dir", env=.GlobalEnv)
	}
	else {
		# guess the top-level directory depending on the OS:
		top_dir()
		get(".top.dir", env=.GlobalEnv)
	}
}
check_top_dir()

tharte/tutils documentation built on Feb. 11, 2020, 9:17 a.m.