Skip to content

Allow searching for arguments by name (regardless of value)#48

Merged
rossellhayes merged 10 commits intomainfrom
feat/find-argument-name
Oct 12, 2022
Merged

Allow searching for arguments by name (regardless of value)#48
rossellhayes merged 10 commits intomainfrom
feat/find-argument-name

Conversation

@rossellhayes
Copy link
Copy Markdown
Contributor

@rossellhayes rossellhayes commented Oct 5, 2022

If the match argument of find_arguments() is a character vector, find arguments by name regardless of value.

library(gradecode)
.user_code <- "mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)"

find_arguments(.user_code, match = "na.rm")
#> 
#> ── gradecode found ─────────────────────────────────────────────────────────────
#> mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)
#> 
#> ── Request ──
#> argument na.rm
#> Found 1 result.
#> 
#> ── Result 1 ──
#> mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)
#>                                     ‾‾‾‾‾‾‾‾‾‾‾‾

If match is a character vector of length > 1, find any of those arguments.

find_arguments(.user_code, match = c("mean", "sd"))
#> 
#> ── gradecode found ─────────────────────────────────────────────────────────────
#> mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)
#> 
#> ── Request ──
#> argument mean > NA sd
#> Found 2 results.
#> 
#> ── Result 1 ──
#> mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)
#>                ‾‾‾‾‾‾‾‾
#> 
#> ── Result 2 ──
#> mean(rnorm(10, mean = 1, sd = 0.1), na.rm = TRUE)
#>                          ‾‾‾‾‾‾‾‾

Created on 2022-10-05 with reprex v2.0.2

Closes #45.

@rossellhayes rossellhayes added the feat New feature or request label Oct 5, 2022
@rossellhayes rossellhayes requested a review from gadenbuie October 5, 2022 18:00
@rossellhayes rossellhayes self-assigned this Oct 5, 2022
@gadenbuie
Copy link
Copy Markdown
Member

Very nice!

@rossellhayes
Copy link
Copy Markdown
Contributor Author

@gadenbuie One final detail worth reviewing before I merge. 569f506 adds support for finding unnamed arguments with an empty string, like this:

library(gradecode)
find_arguments("mean(1:10, na.rm = TRUE)", match = "")
#> 
#> ── gradecode found ─────────────────────────────────────────────────────────────
#> mean(1:10, na.rm = TRUE)
#> 
#> ── Request ──
#> argument ""
#> Found 1 result.
#> 
#> ── Result 1 ──
#> mean(1:10, na.rm = TRUE)
#>      ‾‾‾‾

Created on 2022-10-06 with reprex v2.0.2

Is that a good interface?

(function("" = value) fails, so there's no risk of confusing unnamed arguments with arguments that are literally named "").

@rossellhayes rossellhayes merged commit 862d60c into main Oct 12, 2022
@rossellhayes rossellhayes deleted the feat/find-argument-name branch October 12, 2022 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Find an argument without filtering on its value

2 participants