Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeroenjanssens/rexpect
Automate Interactive Applications in R
https://github.com/jeroenjanssens/rexpect
automation expect rstats
Last synced: 14 days ago
JSON representation
Automate Interactive Applications in R
- Host: GitHub
- URL: https://github.com/jeroenjanssens/rexpect
- Owner: jeroenjanssens
- License: other
- Created: 2019-12-27T12:26:22.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-04-26T12:15:40.000Z (7 months ago)
- Last Synced: 2024-10-13T02:14:26.063Z (29 days ago)
- Topics: automation, expect, rstats
- Language: R
- Homepage: https://jeroenjanssens.github.io/rexpect/
- Size: 4.04 MB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - jeroenjanssens/rexpect - Automate Interactive Applications in R (R)
README
---
output:
github_document:
html_preview: true
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# rexpect
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-green.svg)](https://www.tidyverse.org/lifecycle/#stable)
`rexpect` is an R package that allows you to automate interactions with programs that
expose a text terminal interface. The API is inspired by the original Expect tool by Don Libes.## Installation
You can install the development version from [GitHub](https://github.com/) with:
```{r, eval = FALSE}
# install.packages("remotes")
remotes::install_github("jeroenjanssens/rexpect")
```## Example
Let's say we have a Python script that repeatedly asks for a number.
When the user enters `sum`, the script prints the sum of all the numbers.
The script exits when the user presses `CTRL-C`.
Here's the contents of _bin/sum.py_:```{verbatim, file = "bin/sum.py", lang = "python"}
```We can use `rexpect` to run this script and interact with it.
Let's enter the numbers 7, 13, and 22 and see what the sum is:```{r}
library(rexpect)session <- spawn("bin/sum.py", prompt = "^(.*):$")
send_lines(session, c("7", "13", "22", "sum"), wait = TRUE)
```Under the hood, `rexpect` starts a tmux session using [tmuxr](https://jeroenjanssens.github.io/tmuxr):
```{r}
session
```We can capture the output and extract the sum:
```{r}
print(output <- read_all(session))
as.numeric(gsub("[^0-9.]", "", output[length(output) - 1]))
```We've accomplished our goal, so let's exit:
```{r}
send_lines(session, "exit")
read_all(session)
```Oh that's right, we need to press `CTRL-C` to exit the script:
```{r}
send_control_c(session)
```This was a rather simple example to demonstrate some of the capabilities of `rexpect`.
The session could have taken place inside a Docker container or over SSH.
We could also have recorded the session using [asciinema](https://asciinema.org/).
Have a look at [the function reference](https://jeroenjanssens.github.io/rexpect/reference/) to learn more about what `rexpect` has to offer.## License
The `rexpect` package is licensed under the MIT License.