doInStata: Execute some Stata code in a running Stata instance

View source: R/core.R

doInStataR Documentation

Execute some Stata code in a running Stata instance

Description

Execute some Stata code in a running Stata instance

Usage

doInStata(id, code = "", df = NULL, import_df = !is.null(df),
  results = c("e", "r"), timeout = Inf, preserve_restore = FALSE,
  cleanup = TRUE, nolog = FALSE, future = FALSE)

Arguments

id

An object of S3 class 'StataID' generated by startStata

code

A Stata code in a string to be executed

df

A data frame to be loaded into Stata before the Stata code is executed (optional)

import_df

Logical: should the resulting Stata dataset be returned? Defaults: TRUE if df is provided, else FALSE.

results

NULL or a character vector with either "e" or "r" or both of these characters. Default: c("e", "r") which means that both e-class and r-class Stata results will be imported (stored in e() and r() Stata macros, scalars, matrices respectively). If NULL, none of these results will be imported.

timeout

Seconds for R to wait for Stata results. Default: infinity.

preserve_restore

Logical: should the Stata code start with preserve and end with restore? Default: FALSE.

cleanup

Logical: should the time-stamped temporary files (.do file, .log file, and possible input and output .tsv files) be deleted at the end? Default: TRUE.

nolog

Logical: should the Stata log be discarded in the returned value? Default: FALSE.

future

Logical: should this function block R until Stata finishes the job and return what Stata produces (future = FALSE, default) or should this function send the job to Stata and not make R wait for Stata output (future = TRUE). In latter case, the Stata output can be obtained with the function getStataFuture.

Value

If future = FALSE, a list with:

  • log – A character vector with Stata display log if nolog = FALSE. May be an empty string if Stata does not finish executing the code before timeout.

  • error – If Stata displays an error – an integer number with the Stata error code number (see http://www.stata.com/manuals14/perror.pdf). The error message should be visible in the log (see the point above).

  • df – Optional: a data frame saved by Stata if import_df = TRUE and if it can be read by read.delim, else NULL if read.delim returns an error.

  • results – if the argument results is not NULL, a list with one or both of the elements named e_class and/or r_class (depending on what was specified in the argument results), each including (if available):

    • scalars – a named list of numeric scalar values,

    • macros – a named list of character (string/text) values,

    • matrices – a named list of numeric matrices,

    • modeldf – only for e_class: a data.frame with the estimated coefficients (column coef), and standard errors (column stderr), with the Stata variable names recorded in row.names.

If future = TRUE, an object of S3 class 'StataFuture' to be used by getStataFuture.


alekrutkowski/RStataLink documentation built on March 22, 2023, 2:18 a.m.