From reijo.sund at helsinki.fi  Sat Oct 19 07:57:48 2013
From: reijo.sund at helsinki.fi (Reijo Sund)
Date: Sat, 19 Oct 2013 01:57:48 -0400
Subject: [Rd] Suggestions for an "official" place to store
	permissions/options for a package?
In-Reply-To: <CABG0rfty=WteccOEFLG5D5fPq9=he8A3t4_a1WZd1E_2H3J0mw@mail.gmail.com>
References: <CABG0rfty=WteccOEFLG5D5fPq9=he8A3t4_a1WZd1E_2H3J0mw@mail.gmail.com>
Message-ID: <668BD9CF-3768-41F1-A273-6074D439FB9B@helsinki.fi>

> to my knowledge, there is not currently an official way to store a
> *package*'s options to a standardized location on a user's computer.

CRAN Repository policy gives some guidance:
"Packages should not write in the users? home filespace, nor anywhere else on the file system apart from the R session?s temporary directory (or during installation in the location pointed to by TMPDIR: and such usage should be cleaned up). Installing into the system?s R installation (e.g., scripts to its bin directory) is not allowed. Limited exceptions may be allowed in interactive sessions if the package obtains confirmation from the user."

Of the existing packages in CRAN, at least Rcmdr allows to use config files.

If there is a need for storing package's options for longer time, it may not be a good idea to write package's directories (or to system's R installation) as updates of the package (or R) would then erase the config file. It is also obvious that storing of options is sensible only for directories for which the user has write access. In this sense, a (subdirectory in) user's home directory is probably the best place to store package's config files unless the user has provided other information in function call or using environmental variables.

Below is an extract of code from my package muste that also uses config files. I'm not claiming that it would provide any general or even good solution, but at least it gives a concrete example that hopefully stimulates discussion on this topic..

Best wishes,
Reijo Sund


- - -


# Create environment for package's global variables 
.muste <- new.env(hash=TRUE, parent=emptyenv())

muste <- function(config="<empty>") 
    {

# Package directory
    .muste$mustepath <- system.file(package="muste")

# Check write access to package directory
    if(file.access(.muste$mustepath,mode=2)==-1) .muste$writeaccess <-FALSE
    else .muste$writeaccess <- TRUE
  
# Start path  
    .muste$startdir <- getwd()
  
# Path to actual R directory 
    .muste$Rhome <- normalizePath(R.home())
  
# Path to home directory (see R documentation for more information)
    .muste$homedir <- normalizePath("~/")

# Path to temp directory with guaranteed write access
    .muste$Rtempdir <- tempdir()
  
# System, OS and R info  
    .muste$sysname<-unlist(Sys.info()["sysname"])[[1]]
    .muste$OS.type <- .Platform$OS.type
    .muste$r_arch <- .Platform$r_arch
  
# Path to R binary  
    if (.muste$sysname=="Windows")
      {
      .muste$Rbin <-  paste(file.path(R.home("bin"),"Rgui --sdi"))  
      }
    else .muste$Rbin <- paste(file.path(R.home("bin"),"R"))  
  
# Location of config file
    if (config=="<empty>") 
        {
        .muste$apufile <- Sys.getenv("MUSTEAPU") # Read path from environmental variable
        if (nchar(.muste$apufile)==0) # If file is not given, use defaults
            {
            if (.muste$sysname=="Windows") .muste$apufile <- paste(.muste$homedir,'\\.muste\\muste.apu',sep="")
            else .muste$apufile <- paste(.muste$homedir,'/.muste/muste.apu',sep="")
            }
        }
    else .muste$apufile <- config  # Path to file given as a parameter

# Check if given setup file exists
    if(!file.exists(.muste$apufile)) .muste.setup()

}

.muste.setup <- function()
    {
# Ask about creation of dir(s)/file(s)    
    viesti <- paste("Configuration file was not found!\nIs it OK to create\n",.muste$apufile,"?",sep=" ")
    response <- "no"
    if (interactive())
        {
        require(tcltk)
        response <- tclvalue(tkmessageBox(message=viesti, icon="question", type="yesno", default="no", title=""))
        }
    if (response == "no") return()

# Directory part of the path to config file	
    .muste$apufiledir <- dirname(.muste$apufile) 
    
# Create directory for config file
    dir.create(.muste$apufiledir,showWarnings=FALSE)
	
# Check write access to given config file directory		
    if(file.access(.muste$apufiledir,mode=2)==-1) stop("No write access!")     
    
# Create and initialize config file
    file.create(.muste$apufile)
    cat("/ Configuration file",file=.muste$apufile,sep="\n",append=TRUE)
    }



[Rd] Suggestions for an "official" place to store permissions/options for a package?
Reijo Sund