Skip to content

Add as.list() method for gradecode_found objects#49

Merged
rossellhayes merged 24 commits intomainfrom
feat/as.list.gradecode_found
Oct 17, 2022
Merged

Add as.list() method for gradecode_found objects#49
rossellhayes merged 24 commits intomainfrom
feat/as.list.gradecode_found

Conversation

@rossellhayes
Copy link
Copy Markdown
Contributor

library(gradecode)

gradecode_found <- find_functions("mean(1:10, na.rm = TRUE)", mean) %>%
  find_arguments() %>%
  find_operators(":")

as.list(gradecode_found) %>% str()
#> List of 3
#>  $ :List of 3
#>   ..$ type   : chr "function"
#>   ..$ request: chr "mean"
#>   ..$ result : chr "mean"
#>  $ :List of 3
#>   ..$ type   : chr "argument"
#>   ..$ request: chr NA
#>   ..$ result : chr [1:2] "1:10" "na.rm = TRUE"
#>  $ :List of 3
#>   ..$ type   : chr "operator"
#>   ..$ request: chr "\":\""
#>   ..$ result : chr ":"

Use the as_xml argument to get $result as XML instead of text.

as.list(gradecode_found, as_xml = TRUE)
#> [[1]]
#> [[1]]$type
#> [1] "function"
#> 
#> [[1]]$request
#> [1] "mean"
#> 
#> [[1]]$result
#> {xml_nodeset (1)}
#> [1] <expr line1="1" col1="1" line2="1" col2="4" start="26" end="29">\n  <SYMB ...
#> 
#> 
#> [[2]]
#> [[2]]$type
#> [1] "argument"
#> 
#> [[2]]$request
#> [1] NA
#> 
#> [[2]]$result
#> [[2]]$result[[1]]
#> {xml_nodeset (1)}
#> [1] <expr line1="1" col1="6" line2="1" col2="9" start="31" end="34">\n  <expr ...
#> 
#> [[2]]$result[[2]]
#> {xml_nodeset (3)}
#> [1] <SYMBOL_SUB line1="1" col1="12" line2="1" col2="16" start="37" end="41">n ...
#> [2] <EQ_SUB line1="1" col1="18" line2="1" col2="18" start="43" end="43">=</EQ ...
#> [3] <expr line1="1" col1="20" line2="1" col2="23" start="45" end="48">\n  <NU ...
#> 
#> 
#> 
#> [[3]]
#> [[3]]$type
#> [1] "operator"
#> 
#> [[3]]$request
#> [1] "\":\""
#> 
#> [[3]]$result
#> {xml_nodeset (1)}
#> [1] <OP-COLON line1="1" col1="7" line2="1" col2="7" start="32" end="32">:</OP ...

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

Closes #46.

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

What does this look like when there are multiple results in the user code? Also I'm realizing that what might be missing in this structure is something that describes the hierarchy of requests, e.g. functions → arguments → operators.

@rossellhayes
Copy link
Copy Markdown
Contributor Author

rossellhayes commented Oct 6, 2022

@gadenbuie If there are multiple results, you get a character vector if as_xml = FALSE:

library(gradecode)

as.list(find_arguments("mean(1:10, trim = 0.1, na.rm = TRUE)")) %>% str()
#> List of 1
#>  $ :List of 3
#>   ..$ type   : chr "argument"
#>   ..$ request: chr NA
#>   ..$ result : chr [1:3] "1:10" "trim = 0.1" "na.rm = TRUE"

Or a list if as_xml = TRUE:

as.list(find_arguments("mean(1:10, trim = 0.1, na.rm = TRUE)"), as_xml = TRUE)
#> [[1]]
#> [[1]]$type
#> [1] "argument"
#> 
#> [[1]]$request
#> [1] NA
#> 
#> [[1]]$result
#> [[1]]$result[[1]]
#> {xml_nodeset (1)}
#> [1] <expr line1="1" col1="6" line2="1" col2="9" start="43" end="46">\n  <expr ...
#> 
#> [[1]]$result[[2]]
#> {xml_nodeset (3)}
#> [1] <SYMBOL_SUB line1="1" col1="12" line2="1" col2="15" start="49" end="52">t ...
#> [2] <EQ_SUB line1="1" col1="17" line2="1" col2="17" start="54" end="54">=</EQ ...
#> [3] <expr line1="1" col1="19" line2="1" col2="21" start="56" end="58">\n  <NU ...
#> 
#> [[1]]$result[[3]]
#> {xml_nodeset (3)}
#> [1] <SYMBOL_SUB line1="1" col1="24" line2="1" col2="28" start="61" end="65">n ...
#> [2] <EQ_SUB line1="1" col1="30" line2="1" col2="30" start="67" end="67">=</EQ ...
#> [3] <expr line1="1" col1="32" line2="1" col2="35" start="69" end="72">\n  <NU ...

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

In terms of showing the precedence, it's somewhat communicated by the order of the elements in the list. Can you see an elegant way to explicitly include it in the list? Adding an separate element kind of breaks the idea of each list element equals one find_*() call.

@gadenbuie
Copy link
Copy Markdown
Member

How would something like this be handled?

library(gradecode)

"mean(1:10, trim = 0.1, na.rm = TRUE)
median(na.rm = FALSE, 1:10)" %>%
  find_functions() %>%
  find_arguments() %>%
  as.list() %>%
  str()

@rossellhayes
Copy link
Copy Markdown
Contributor Author

Oh okay, that'll take some work to get multiple expressions working!

… in `$initialize()`

This saves having to call `strsplit()` in multiple functions that deal with `$user_code`
@rossellhayes
Copy link
Copy Markdown
Contributor Author

@gadenbuie Multiple expressions are now supported. Right now, the results aren't separated into their respective expressions, but that that's true in gradecode generally, not just the as.list() method.

library(gradecode)

"mean(1:10, trim = 0.1, na.rm = TRUE)
median(na.rm = FALSE, 1:10)" %>%
  find_functions() %>%
  find_arguments() %>%
  as.list() %>%
  str()
#> List of 2
#>  $ :List of 3
#>   ..$ type   : chr "function"
#>   ..$ request: chr NA
#>   ..$ result : chr [1:2] "mean" "median"
#>  $ :List of 3
#>   ..$ type   : chr "argument"
#>   ..$ request: chr NA
#>   ..$ result : chr [1:5] "1:10" "trim = 0.1" "na.rm = TRUE" "na.rm = FALSE" ...

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

@rossellhayes
Copy link
Copy Markdown
Contributor Author

TODO

  • Add tests for as_tibble() method

@rossellhayes rossellhayes merged commit ac35715 into main Oct 17, 2022
@rossellhayes rossellhayes deleted the feat/as.list.gradecode_found branch October 17, 2022 23:37
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.

Method for as.list() to convert a gradecode_found result to a list

2 participants