Skip to contents

parse_netlogo_list() parses NetLogo-style lists represented as strings (e.g., "[1 2 3]") into R lists. It automatically detects numeric, integer, logical, and character types within the lists and converts them accordingly.

Usage

parse_netlogo_list(x)

Arguments

x

An atomic vector potentially containing NetLogo-style list strings.

Value

The return value will depend on the input:

  • If x does not contain NetLogo-style lists, returns the original vector unchanged.

  • If x contains NetLogo-style lists, returns a list where each element is the parsed result of the corresponding input element. Parsed elements may be atomic vectors (for homogeneous lists) or nested lists (for mixed-type or nested lists).

Details

The function handles the following cases:

  • Homogeneous lists: Lists containing elements of the same type are returned as atomic vectors (e.g., "[1 2 3]" becomes c(1L, 2L, 3L)).

  • Mixed-type lists: Lists containing elements of different types are returned as R lists (e.g., '[1.1 "a" true]' becomes list(1.1, "a", TRUE)).

  • Nested lists: Lists containing other lists are returned as nested R lists (e.g., '["a" "b" [1 2]]' becomes list(c("a", "b"), c(1L, 2L))).

NetLogo boolean values (true/false) are converted to R logical values (TRUE/FALSE). NetLogo NaN values are preserved as character strings.

Examples

# Scalar Examples -----

'[1]' |> parse_netlogo_list()
#> [[1]]
#> [1] 1
#> 

'["a" "b" "c"]' |> parse_netlogo_list()
#> [[1]]
#> [1] "a" "b" "c"
#> 

'[1 2 3]' |> parse_netlogo_list()
#> [[1]]
#> [1] 1 2 3
#> 

'[1.1 2.1 3.1]' |> parse_netlogo_list()
#> [[1]]
#> [1] 1.1 2.1 3.1
#> 

'[true false true]' |> parse_netlogo_list()
#> [[1]]
#> [1]  TRUE FALSE  TRUE
#> 

# Vector Examples -----

c('["a" "b" "c"]', '["d" "e" "f"]') |> parse_netlogo_list()
#> [[1]]
#> [1] "a" "b" "c"
#> 
#> [[2]]
#> [1] "d" "e" "f"
#> 

c('[1 2 3]', '[4 5 6]') |> parse_netlogo_list()
#> [[1]]
#> [1] 1 2 3
#> 
#> [[2]]
#> [1] 4 5 6
#> 

c('[1.1 2.1 3.1]', '[4.1 5.1 6.1]') |> parse_netlogo_list()
#> [[1]]
#> [1] 1.1 2.1 3.1
#> 
#> [[2]]
#> [1] 4.1 5.1 6.1
#> 

c('[true false true]', '[false true false]') |> parse_netlogo_list()
#> [[1]]
#> [1]  TRUE FALSE  TRUE
#> 
#> [[2]]
#> [1] FALSE  TRUE FALSE
#> 

# Combined Examples -----

c('["a" "b" "c"]', '[4 5 6]') |> parse_netlogo_list()
#> [[1]]
#> [1] "a" "b" "c"
#> 
#> [[2]]
#> [1] 4 5 6
#> 

c('[1.1 2.1 3.1]', '[true false true]') |> parse_netlogo_list()
#> [[1]]
#> [1] 1.1 2.1 3.1
#> 
#> [[2]]
#> [1]  TRUE FALSE  TRUE
#> 

c('[1.1 "a" true]') |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] 1.1
#> 
#> [[1]][[2]]
#> [1] "a"
#> 
#> [[1]][[3]]
#> [1] TRUE
#> 
#> 

# Nested Examples -----

c('["a" "b" "c" [1 2]]', '[4 5 6]') |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] "a" "b" "c"
#> 
#> [[1]][[2]]
#> [1] 1 2
#> 
#> 
#> [[2]]
#> [1] 4 5 6
#> 

c('["a" "b" "c" [1 2] true ["d" "c"]]') |> parse_netlogo_list()
#> [[1]]
#> [[1]][[1]]
#> [1] "a" "b" "c"
#> 
#> [[1]][[2]]
#> [1] 1 2
#> 
#> [[1]][[3]]
#> [1] TRUE
#> 
#> [[1]][[4]]
#> [1] "d" "c"
#> 
#>