Re: [R] A question about R environment

From: François Pinard <pinard_at_iro.umontreal.ca>
Date: Tue 09 Jan 2007 - 03:11:03 GMT

[Tong Wang]

>I created environment "mytoolbox" by : mytoolbox <-
>new.env(parent=baseenv()). Is there anyway I put it in the search
>path? In a project, I often write some small functions, and load them
>into my workspace directly, so when I list the objects with ls(), it
>looks pretty messy. So I am wondering if it is possible to creat an
>environment, and put these tools into this environment. For example,
>I have functions fun1(), fun2() ... and creat an environment mytoolbox
>which contains all these functions. And it should be somewhere in the
>search path: ".GlobalEnv" "mytoolbox" "package:methods".

Here is a trick, shown as a fairly simplified copy of my ~/.Rprofile. It allows for a few simple functions always available, yet without having to create a package, and leaving ls() and any later .RData file unencumbered.

The idea is to use local() to prevent any unwanted clutter to leak out (my real ~/.Rprofile holds more than shown below and use temporary variables), to initialise a list meant to hold a bunch of functions or other R things, and to save that list on the search path.

This example also demonstrate a few useful functions for when I read the R mailing list. I often need to transfer part of emails containing code excerpts within the window where R executes, while removing quotation marks, white lines and other noise. I merely highlight-select part of the message with the mouse, and then, within R, do things like:

   xs()   source the highlighted region
   xd()   read in a data.frame
   xm()   read in a matrix
   xe()   evaluate and print an expression
   xv()   read a list of values as a vector

The list above in decreasing order of usefulness (for me). Except for xs(), which has no automatic printout, you may either let the others print what they got, or assign their value to some variable. Arguments are also possible, for example like this:

   xd(T) read in a data.frame when the first line holds column names

if (interactive()) {

    local({

        fp.etc <- list()

        fp.etc$xsel.vector <- function (...) {
            connexion <- textConnection(xselection())
            on.exit(close(connexion))
            scan(connexion, ...)
        }
        fp.etc$xsel.dataframe <- function (...) {
            connexion <- textConnection(xselection())
            on.exit(close(connexion))
            read.table(connexion, ...)
        }
        fp.etc$xsel.matrix <- function (...) {
            connexion <- textConnection(xselection())
            on.exit(close(connexion))
            data.matrix(read.table(connexion, ...))
        }
        fp.etc$xsel.eval <- function (...) {
            connexion <- textConnection(xselection())
            on.exit(close(connexion))
            eval(parse(connexion, ...))
        }
        fp.etc$xsel.source <- function (...) {
            connexion <- textConnection(xselection())
            on.exit(close(connexion))
            source(connexion, ...)
        }

        fp.etc$xselection <- function ()
        {
            lignes <- suppressWarnings(readLines('clipboard'))
            lignes <- lignes[lignes != '']
            stopifnot(length(lignes) != 0)
            marge <- substr(lignes, 1, 1)
            while (all(marge %in% c('>', '+', ':', '|'))
                  || all(marge == ' ')) {
                lignes <- substring(lignes, 2)
                marge <- substr(lignes, 1, 1)
            }
            lignes
        }

        fp.etc$xv <- fp.etc$xsel.vector
        fp.etc$xd <- fp.etc$xsel.dataframe
        fp.etc$xm <- fp.etc$xsel.matrix
        fp.etc$xe <- fp.etc$xsel.eval
        fp.etc$xs <- fp.etc$xsel.source

        attach(fp.etc, warn=FALSE)

    })
}

# vim: ft=r

-- 
François Pinard   http://pinard.progiciels-bpi.ca

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed Jan 10 04:08:05 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 09 Jan 2007 - 17:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.