R System and Configurations
Posted on Nov 21, 2012 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
-
To allow opening R from a directory in R in Windows system, we can modify the registry. More specifically, we can add a command name (to be appear in the right-click menu) under
HKEY_CLASSES_ROOT\Directory\Backgroup\shell
, and then add a subkeycommand
under it. Last, we just need to change the default value to be the command that we want to run each time we click the menu item. -
There are many ways to change configurations of R. For example, to change the default working directory of R in Windows, you can change the properties of the shortcut of R software, which also applies to other softwares in Windows. Another way is to put some code which changes the default working directory into file
Rprofile.site
(The code inside this file will be run on the startup of R.). The latter way can also be used to set other configurations for R, and it is preferred to other ways of setting configurations for R, because it is a universal way and is guaranteed to work (If you set configurations for R in other ways and at the same time, set configurations in fileRprofile.site
, these configurations in fileRprofile.site
will be in effect.) -
R.Version
can display the version information of R. -
Function
args
can show the arguments of functions in R. -
We can use function
Sys.sleep
to suspend execution of R for given time. -
By default, function
ls
returns non-hidden objects (By default,ls
does not return objects whose names start with a dot. For this reason I call these objects whose name start with a dot hidden objects.) in current environment. For example if we usels()
in a function, then it only returns these non-hidden objects defined in the function. To get non-hidden object in the top level environment (R workspace), you can usels(pos=1)
; to get non-hidden objects in a package, we can usels(pos="package:pkgname")
. Sometimes, you might want to see all objects including these hidden ones in an environment. To do so, you can use the optionall.anmes=T
. Notice thatls
also support regular expression (use the optionpattern
), which enables you to find objects in R workspace faster. -
In order to be convenient, usually we would like to specify short names of executables. Sometimes this could lead to failure because some functions in R do not accept short names of executables, e.g.
shell.exec
, which is really stupid. A good way to solve the problem is to use functionSys.which
to find the full name of executables. Conversely, functionbasename
can get the base name of a path. -
system
calls invokes a system command, but the command passed to it must be an executable (extensions.exe
or.com
) or a batch file (extensions.cmd
or.bat
). Redirection, pipes, DOS internal commands and so on cannot be used withsystem
.shell
is a more user-friendly wrapper forsystem
which is more powerful (support redirection, pipes, DOS internal commands and so on). If you want to make use of Windows file association, useshell.exec
. -
Function
search
returns search path for R, which includes loaded packages. -
Function
demo
is helpful to show how to use a function or package, however, it creates many global variables which is annoying. Using functionexample
can solve this problem, but it seems that these functions are not exactly the same. I'm a little confused about these two functions. -
R support partial matching on tags, which means that usually you do not have to use full names for arguments of a R function. You can use partial names for arguments. However, always be careful when a R function takes
ldots
as an argument. -
Function
Sys.setenv
andSys.unsetenv
can set and unset environment variables. -
Sys.info
contains information about the operating system. Variable.Platform
contains information about R system (Actually information related to R system are usually stored in variables starting with dot). -
Sys.time
returns the system time, i.e. the time of the computer on which the code is run. There is another function calledsystem.time
which can calculate how much time an evaluation takes. -
gc
collect garbage. R is infamous for extensive memory using. If you delete some big objects in R workspace, you'd better usegc()
to manually trigger the garbage collection. -
the good us of
source
with local = TRUE ... -
options
allows one to get and set a variety of global options which affect the way in which R computes and displays ints results. For example,options(width=40)
forces R to format its output results to have at most 40 characters (if possible). Notice that this can be very helpful if you use Sweave. For example, if the outputs of some R code in Sweave is too long, you can add a similar command as above to format the R outputs.