PBSmodelling (version 2.68.8)

writeList: Write a List to a File in PBS Modelling Format

Description

Write an ASCII text representation in either "D" format or "P" format. The "D" format makes use of dput and dget and produces an R representation of the list. The "P" format represents a simple list in an easy-to-read, ad hoc PBSmodelling format.

Usage

writeList(x, fname, format="D", comments="")

Value

String containing the file name.

Arguments

x

R list object to write to an ASCII text file.

fname

file name of the text file to create.

format

format of the file to create: "D" or "P".

comments

vector of character strings to use as initial-line comments in the file.

Author

Alex Couture-Beil, Vancouver Island University, Nanaimo BC

Details

The D Format

The "D" format is equivalent to using R's base functions dput and dget, which support all R objects.

The P Format

The "P" format only supports a list that may contain lists, vectors, matrices, arrays, and data frames. Scalars are treated like vectors. It writes each list element using the following conventions:

  1. $ denotes the start of a list element, and the element's name follows this character; for nested lists, $ separates each nesting level;

  2. $$, on the next line, denotes a line used to describe the element's structure, which includes object type, mode(s), names (if vector), rownames (if matrix or data), and colnames (if matrix or data); and

  3. subsequent lines contain data (one line for a vector and multiple lines for a matrix or other data).

If a list's elements are unnamed, have the name NA, or have the empty string as a name, this function generates names ("P" format only). If two list elements share the same name, the list will export correctly, but it will import incorrectly.

Arrays with three or more dimensions have dim and dimnames arguments. dim describes the dimension of the data (a vector as returned by dim(some_array)) and dimnames is a vector of length sum(dim(some_array)+1) and is constructed as follows:

foreach dimension d, first append the name of the dimension d and then append all labels within that dimension

Multiple rows of data for matrices or data frames must have equal numbers of entries (separated by whitespace).

Note that array data are written the same format as they are displayed in the R console:
nrow=dim()[1], ncol=dim()[2]
repeated by scrolling through successively higher dimensions, increasing the index from left to right within each dimension. The flattened table will have dim()[2] columns.

For complete details, see “PBSmodelling-UG.pdf” at the location described when loading this package.

See Also

packList, readList, unpackList

Examples

Run this code
if (FALSE) {
local(envir=.PBSmodEnv,expr={
  cwd = getwd(); setwd(tempdir())
  test <- list(a=10,b=euro,c=view(WorldPhones),d=view(USArrests))
  writeList(test,"test.txt",format="P",
    comments=" Scalar, Vector, Matrix, Data Frame")
  openFile("test.txt")
  setwd(cwd)
})
local(envir=.PBSmodEnv,expr={
  cwd = getwd(); setwd(tempdir())
  ##Example of dimnames for Arrays
  dimnames(Titanic)
  writeList( list( Titanic ), format="P")
  setwd(cwd)
})
}

Run the code above in your browser using DataLab