https://github.com/matt-dray/choosethis
:computer_mouse::crossed_swords: R package: off-label concepts for executing R code from links in the console, using {cli}
https://github.com/matt-dray/choosethis
cli r-package r-stats
Last synced: over 1 year ago
JSON representation
:computer_mouse::crossed_swords: R package: off-label concepts for executing R code from links in the console, using {cli}
- Host: GitHub
- URL: https://github.com/matt-dray/choosethis
- Owner: matt-dray
- License: other
- Created: 2023-09-15T08:37:40.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-17T11:37:05.000Z (almost 3 years ago)
- Last Synced: 2025-03-27T04:38:16.774Z (over 1 year ago)
- Topics: cli, r-package, r-stats
- Language: R
- Homepage: https://www.rostrum.blog/posts/2023-09-17-choosethis/
- Size: 47.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#",
fig.path = "man/figures/",
out.width = "100%",
eval = FALSE
)
```
# {choosethis}
[](https://www.repostatus.org/#concept)
[](https://www.rostrum.blog/posts/2023-09-17-chosethis/)
Demonstrations of using [{cli}](https://cran.r-project.org/package=cli) to present clickable links to the user in their console. Read more in [the accompanying blogpost](https://www.rostrum.blog/posts/2023-09-17-chosethis/).

## Demos
You can install {choosethis} from GitHub.
```{r install}
if (!require(remotes)) install.packages("remotes")
install_github("matt-dray/choosethis")
library(choosethis)
```
### 1. Text adventure
To start a new game:
```{r begin}
begin()
```
```
You reach a fork in the road.
* Go left
* Go right
```
If your console supports links (e.g. RStudio), then the words 'left' and 'right' in the printout will be clickable (not shown above). Click one and you'll be taken to the next choice. If unsupported, you'll be shown functions to copy-paste yourself.
### 2. Summarise a data.frame
To begin:
```{r means}
ask_col_mean(ChickWeight)
```
```
What column would you like the mean of?
* weight
* Time
```
If your console supports links (e.g. RStudio), then the column names in the printout (`weight`, `Time`) will be clickable (not shown above). Click one and you'll be returned the mean value for that column.
## Theory
The [{cli} package](https://cran.r-project.org/package=cli) lets you present [clickable ANSI links](https://cli.r-lib.org/reference/links.html) to the user in supported terminals, such as RStudio.
`choosethis::begin()` uses [cli::ansi_has_hyperlink_support()](https://cli.r-lib.org/reference/style_hyperlink.html) to discover whether a user's terminal can support these links. If links aren't supported, then the underlying expression will be presented to the user instead of a clickable link.