This function runs a SLiM script, specified as a slimr_script object, a character vector, or a text file.

slim_run(
  x,
  slim_path = NULL,
  script_file = NULL,
  simple_run = FALSE,
  capture_output = "file",
  keep_all_output = FALSE,
  show_output = FALSE,
  callbacks = NULL,
  cb_args = NULL,
  new_grdev = FALSE,
  parallel = FALSE,
  progress = FALSE,
  throw_error = FALSE,
  ...
)

# S3 method for character
slim_run(
  x,
  slim_path = NULL,
  script_file = NULL,
  simple_run = FALSE,
  capture_output = "file",
  keep_all_output = FALSE,
  show_output = FALSE,
  callbacks = NULL,
  cb_args = NULL,
  new_grdev = FALSE,
  parallel = FALSE,
  progress = FALSE,
  throw_error = FALSE,
  ...
)

# S3 method for slimr_script
slim_run(
  x,
  slim_path = NULL,
  script_file = NULL,
  simple_run = FALSE,
  capture_output = "file",
  keep_all_output = FALSE,
  show_output = FALSE,
  callbacks = NULL,
  cb_args = NULL,
  new_grdev = FALSE,
  parallel = FALSE,
  progress = FALSE,
  throw_error = FALSE,
  ...
)

# S3 method for slimr_script_coll
slim_run(
  x,
  slim_path = NULL,
  script_file = NULL,
  simple_run = FALSE,
  capture_output = "file",
  keep_all_output = FALSE,
  show_output = FALSE,
  callbacks = NULL,
  cb_args = NULL,
  new_grdev = FALSE,
  parallel = FALSE,
  progress = FALSE,
  throw_error = FALSE,
  ...
)

Arguments

x

Object containing script to run (e.g. a character vector or a slimr_script object)

slim_path

Path to the SLiM executable. If left NULL slimr will attempt to automatically determine it, typically by examining environmental variables.

script_file

If the script you want to run is in a text file, you can add the pather here. If this is argument is not NULL argument x will be ignored

simple_run

Whether to do a "simple run", which just runs the script, capturing all output is capture_output is TRUE and additionally sending all output to the R console if show_output is TRUE the script to the R console if show_output is TRUE

capture_output

If TRUE, output from the script will be captured and included in the returned object. Unless keep_all_output is TRUE, only non-data output will be kept (e.g. output not produced by a r_output call)

keep_all_output

If there is data produced by r_output calls, should it be captured as well? Ignored if capture_output is not TRUE

show_output

Should output from the script be sent to the R console? Note that SLiM scripts can sometimes produce a large amount of output, which could overwhelm the console if you are not careful, potentially locking it up. Be careful with this option if you are using any of SLiM's output functions that output genomic data. This can be handy though for simply status print outs..

callbacks

A list of functions to be called during the SLiM run. This can be used to dynamically transform or visualise output from the simulation while it is running. It should be of the form function(data, ...) {do something..}. If using r_output to get formatted data output from SLiM, data will be a four column tibble containing output from the current iteration of the simulation. Columns are:

generation

A vector of generations processed in the current iteration

name

Names of the output data.

expression

The SLiM expression used to generate the output

data

The raw data output from SLiM as a character vector

cb_args

Additional arguments to be passed to any callback functions. Should be a named list where the names refer to the callback's arguments.

new_grdev

Should a new graphics device window be opened on RStudio? This is mainly useful if you are using custom callbacks that generate live figures, and want a faster plotting experience. This is because the default plot viewer in RStudio can be quite slow. Setting this to TRUE also allows record_graphics to work.

parallel

If x is a slimr_script_coll, should the elements in x be run in parallel. For this to work, you must have setup a parallel plan using plan

progress

Should a progress bar be displayed?

throw_error

Should an error be thrown in R is an error is encountered in SLiM? If FALSE, the error message from SLiM is stored in the object returned by slim_run, but execution continues in R. Setting this to TRUE is useful in a script if subsequent code assumes that the simulation finished successfully.

...

Additional arguments to be passed to or from other methods.

Value

A slimr_results object which has the following components:

output

A character vector of raw output. Will be NULL if capture_output is FALSE

exit_status

The exit status code returned by the SLiM process. 0 means success.

output_data

A `tibble` containing output from r_output calls.

process

A processx object containing information about the SLiM process used during the run.

error

If an error was encountered during the run, this will be a character vector containing the error message.

output_file

The path to the file containing captured output from SLiM during the run.

Methods (by class)

  • slim_run(character): Run a SLiM script from character vector

  • slim_run(slimr_script): Run a SLiM script from slimr_script object

  • slim_run(slimr_script_coll): Run a SLiM script from slimr_script object

Examples

if(slim_is_avail()) {
  test_sim <- slim_script(
    slim_block_init_minimal(mutation_rate = 1e-6),
    slim_block_add_subpops(1, 100),
    slim_block(1, 20, late(), {
      r_output(sim.outputFull(), "out", do_every = 10)
    })
  ) %>%
    slim_run()
  test_sim
}
#> 
#> 
#> Simulation finished with exit status: 0
#> 
#> Success!
#> $output
#>  [1] "// Initial random seed:"                  
#>  [2] "1530461718"                               
#>  [3] ""                                         
#>  [4] "// RunInitializeCallbacks():"             
#>  [5] "initializeMutationRate(1e-06);"           
#>  [6] "initializeMutationType(1, 0.5, \"f\", 0);"
#>  [7] "initializeGenomicElementType(1, m1, 1);"  
#>  [8] "initializeGenomicElement(g1, 0, 99999);"  
#>  [9] "initializeRecombinationRate(1e-08);"      
#> [10] ""                                         
#> [11] "// Starting run at tick <start>:"         
#> [12] "1 "                                       
#> [13] ""                                         
#> [14] ""                                         
#> [15] "<slimr_out:start>"                        
#> [16] "<slimr_out:end>"                          
#> [17] ""                                         
#> [18] "<slimr_out:start>"                        
#> 
#> $exit_status
#> [1] 0
#> 
#> $output_data
#> # A tibble: 2 × 5
#>   generation name  expression       type        data                            
#>        <int> <chr> <chr>            <chr>       <chr>                           
#> 1         10 out   sim.outputFull() slim_output "#OUT: 10 10 A\nVersion: 3\nPop…
#> 2         20 out   sim.outputFull() slim_output "#OUT: 20 20 A\nVersion: 3\nPop…
#> 
#> $process
#> PROCESS 'slim.exe', finished.
#> 
#> $error
#> character(0)
#> 
#> $output_file
#> [1] "F:\\Rtemp\\RtmpG056mn\\file5a2c5f14277f.txt"
#> 
#> attr(,"class")
#> [1] "slimr_results"