counter <- function ( ) #*****************************************************************************80 # ## counter() simply reports how many times it has been called. # # Discussion: # # If you can get a function like this to work, you have made a good # start at implementing persistent data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 May 2021 # # Author: # # John Burkardt # # Persistent: # # integer CALLS: the number of times this function has been called. # { # # Persistent data is stored as attributes associated with this function. # calls <- attr ( counter, "calls" ) # # Initialize the persistent data once. # Note that we need to use a DOUBLE ARROW assignment here! # This makes the assignment "globally". # if ( is.null ( calls ) ) { calls = 0 attr ( counter, "calls" ) <<- calls } # # Update the value. # calls = calls + 1 # # Print the updated value. # cat ( "counter() has been called ", calls, " times.\n" ) # # Save the updated value. # attr ( counter, "calls" ) <<- calls return }