The pladdrr package provides complete access to Praat’s
interpreter system, enabling you to:
This vignette demonstrates how to use the interpreter for computational tasks without requiring Praat scripts.
The simplest way to use the interpreter is through standalone expression evaluation functions. These create temporary interpreters and return results immediately.
For more complex workflows, use the PraatInterpreter R6
class which maintains state between operations.
The interpreter automatically detects variable types and adds appropriate Praat suffixes:
# Numeric variables (no suffix)
interp$set_variable("x", 42)
interp$set_variable("pi_value", 3.14159)
# String variables ($ suffix auto-added)
interp$set_variable("name", "John")
interp$set_variable("greeting", "Hello")
# Vector variables (# suffix auto-added)
interp$set_variable("data", c(1.5, 2.5, 3.5, 4.5))
# Matrix variables (## suffix auto-added)
interp$set_variable("matrix", matrix(1:6, nrow = 2, ncol = 3))
# String array variables ($# suffix auto-added)
interp$set_variable("names", c("alpha", "beta", "gamma"))Retrieve variables using Praat suffix notation:
# Get numeric variable
interp$get_variable("x")
#> [1] 42
# Get string variable (must include $ suffix)
interp$get_variable("name$")
#> [1] "John"
# Get vector (# suffix)
interp$get_variable("data#")
#> [1] 1.5 2.5 3.5 4.5
# Get matrix (## suffix)
interp$get_variable("matrix##")
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
# Get string array ($# suffix)
interp$get_variable("names$#")
#> [1] "alpha" "beta" "gamma"Variables can be used directly in expressions:
# Set some variables
interp$set_variable("a", 10)
interp$set_variable("b", 20)
# Use in arithmetic
result <- interp$eval("a + b")
result
#> [1] 30
result <- interp$eval("a * b")
result
#> [1] 200
# String operations with variables
interp$set_variable("first", "hello")
interp$set_variable("second", "world")
result <- interp$eval('first$ + " " + second$')
result
#> [1] "hello world"# Create a vector
interp$set_variable("vec", c(1, 2, 3, 4, 5))
# Get vector size
size <- interp$eval("size(vec#)")
size
#> [1] 5
# Sum vector
total <- interp$eval("sum(vec#)")
total
#> [1] 15
# Mean of vector
mean_val <- interp$eval("mean(vec#)")
mean_val
#> [1] 3
# Create a matrix
interp$set_variable("mat", matrix(1:4, nrow = 2, ncol = 2))
# Query matrix dimensions
rows <- interp$eval("numberOfRows(mat##)")
cols <- interp$eval("numberOfColumns(mat##)")
c(rows = rows, cols = cols)
#> rows cols
#> 2 2Praat uses suffix notation to distinguish variable types:
| R Type | Praat Suffix | Example Variable | Set | Get |
|---|---|---|---|---|
| Numeric scalar | (none) | x |
set_variable("x", 42) |
get_variable("x") |
| String | $ |
name$ |
set_variable("name", "test") |
get_variable("name$") |
| Numeric vector | # |
data# |
set_variable("data", c(1,2,3)) |
get_variable("data#") |
| Numeric matrix | ## |
mat## |
set_variable("mat", matrix(...)) |
get_variable("mat##") |
| String array | $# |
names$# |
set_variable("names", c("a","b")) |
get_variable("names$#") |
Key Points:
Query Praat’s global object list (useful when objects are created through other means):
# Get count of objects
count <- praat_object_count()
count
#> [1] 0
# List all objects (returns data frame)
objects <- praat_list_objects()
objects
#> [1] id name class selected
#> <0 rows> (or 0-length row.names)
# Also available via R6 instance
count2 <- interp$object_count()
objects2 <- interp$list_objects()Note: In library mode (without GUI), object creation
commands like Create Sound... are not available. The object
management functions are ready for use when GUI actions are enabled or
when working with objects created through pladdrr’s direct API (e.g.,
sound_load(), pitch_create(), etc.).
# Create an interpreter for a statistical workflow
stats <- PraatInterpreter$new()
# Set up data
stats$set_variable("values", c(12.5, 15.3, 11.8, 14.2, 13.7, 16.1))
# Calculate statistics using Praat functions
n <- stats$eval("size(values#)")
mean_val <- stats$eval("mean(values#)")
stdev <- stats$eval("stdev(values#)")
# Results
cat("Sample size:", n, "\n")
#> Sample size: 6
cat("Mean:", mean_val, "\n")
#> Mean: 13.93333
cat("Std Dev:", stdev, "\n")
#> Std Dev: 1.630542
# Calculate z-scores manually
stats$set_variable("mean", mean_val)
stats$set_variable("sd", stdev)
# For each value, could calculate (x - mean) / sd
# This demonstrates variable persistence across multiple operationspraat_eval_*)
create temporary interpreters - use for one-off calculationsPraatInterpreter$new()) reuse state - use for workflows
with multiple related operationsUse persistent interpreters for workflows: When you need multiple related calculations, create one interpreter instance and reuse it
Name variables clearly: Since Praat and R variables are separate, use descriptive names to avoid confusion
Remember suffix notation: When getting variables or using them in expressions, always include the appropriate suffix
Leverage type auto-detection: Let
set_variable() automatically determine the correct Praat
type from your R object
Check object types: Use class(),
dim(), etc. to verify R objects before setting as Praat
variables
The Praat interpreter integration provides:
PraatInterpreterThe interpreter can be combined with pladdrr’s other API functions for phonetic analysis workflows.
vignette("getting-started") - Introduction to
pladdrrvignette("formant-analysis") - Formant extraction
workflows?PraatInterpreter - R6 class documentation?praat_eval_numeric - Expression evaluation
functions