Learn R Programming

CHNOSZ (version 0.9-1)

sideeffects: Side effects of functions in CHNOSZ

Description

Some functions in the package access thermodynamic data and system definitions contained in the thermo data object, as well as modify the object. This help topic should help users understand the major side effects, but does not contain a comprehensive description of these interactions (the code is the ultimate reference).

Arguments

Details

When the package is loaded, sys.source is used to create a list object named thermo that is placed in an environment named CHNOSZ:thermo. Because that environment is attached, the thermo object, including all of its components, is accessible in the search path. Some functions in CHNOSZ have side effects that modify the contents of thermo; all such changes can be reverted, and the object restored to its original state, by calling data(thermo).

In the functions in the package, the greatest number of accessions are to the thermodynamic database (thermo$obigt), followed by the basis and species definitions (thermo$basis and thermo$species). For example, info can be used to look up thermodynamic data in thermo$obigt by the name or chemical formula of a species. As another example, subcrt attempts to balance unbalanced chemical reactions with the user-defined basis species in thermo$basis.

Some functions modify the thermodynamic database or system definition in thermo. These are examples of side effects, since the functions have an effect on the state of the program that persists beyond the lifetime of the objects returned by the functions. In the code, side effects can be recognized by the presence of the superassignment operator <<-< a="">.

Side effects are not highly desirable in functional programming languages such as R. The reason this design is adopted in CHNOSZ is that interactive use of basis and species appeared to the author, in the early stages of developing the package and of learning R, to be facilitated by not requiring users to assign the results of these functions to objects. Instead, using side effects, the program remembers the results of these function calls. Experience has shown that this design is usable (especially for new users), and is adaptable to many usage scenarios, but the dependence on side effects probably should be eliminated in the future.

The two major side effects, that most users will encounter, are the basis and species definitions. These functions and a few other modifications (writing) and accessions (reading) of data objects are listed below. The names of objects in this table refer to the components of the thermo object; for example, one can type thermo$opt at the command line to access all of the contents of the opt component, including those not listed in the table.

llll{ object writer reader notes obigt mod.obigt info thermodynamic database basis basis species, subcrt basis definition species species affinity species definition opt$T.units T.units convert units opt$water -- water formulation for properties of water opt$Tr, Pr -- GHS reference temperature and pressure opt$state -- info physical state opar thermo.plot.new -- graphical parameters }

Beginning with CHNOSZ version 0.9-9, instances of <<-< a=""> in the code have the effect of superassignment to an enclosing environment (CHNOSZ:thermo on the search path), instead of the very highly discouraged assignment to the global environment (user's workspace) used in previous versions of the package. This convention means that if, from the command line, you wish to alter something in thermo, you also should use the <<-< a=""> operator; examples of changing thermo$opt$water in this manner can found in the help page for water.

See Also

A discussion on What does the "<<-" operator="" mean?<="" dquote=""> appeared on the R-help mailing list, https://stat.ethz.ch/pipermail/r-help/2011-April/275905.html.

Examples

Run this code
data(thermo)    # side effect: reset the system definition
basis()         # NULL
basis("CHNOS")  # side effect: define the basis species
basis()         # not NULL
data(thermo)    # side effect: reset the system definition
basis()         # NULL

Run the code above in your browser using DataLab