Skip to content

as.character.spray #40

@stla

Description

@stla

Hello,

I have something strange. I have a list of four sprays:

> groups
[[1]]
         val
 0 2  =    1
 2 2  =    1
 1 2  =   -2

[[2]]
         val
 2 1  =   -2
 1 1  =    2

[[3]]
         val
 2 0  =    1

[[4]]
         val
 1 2  =   -2
 0 2  =   -2

I want to have them as character. Since there's not as.character.spray function - and I think it would be nice to have one -, I did my own function:

asCharacter <- function(poly) {
  op <- options(polyform = TRUE, sprayvars = letters)
  x <- capture.output(print(poly))
  options(op)
  x
}

I apply my function to the list:

> lapply(groups, asCharacter)
[[1]]
[1] "+b^2 +a^2*b^2 -2*a*b^2" "+b^2 +a^2*b^2 -2*a*b^2"

[[2]]
[1] "-2*a^2*b +2*a*b" "-2*a^2*b +2*a*b"

[[3]]
[1] "+a^2" "+a^2"

[[4]]
[1] "-2*a*b^2 -2*b^2" "-2*a*b^2 -2*b^2"

Every spray is duplicated! Do you have an idea way?

Another strange thing:

> str(groups)
List of 4
 $ :Class 'spray'  hidden list of 2
  ..$ index: int [1:3, 1:2] 0 2 1 2 2 2
  ..$ value: num [1:3] 1 1 -2
 $ :List of 2
  ..$ index: int [1:2, 1:2] 2 1 1 1
  ..$ value: num [1:2] -2 2
  ..- attr(*, "class")= chr "spray"
 $ :Class 'spray'  hidden list of 2
  ..$ index: int [1, 1:2] 2 0
  ..$ value: num 1
 $ :List of 2
  ..$ index: int [1:2, 1:2] 1 0 2 2
  ..$ value: num [1:2] -2 -2
  ..- attr(*, "class")= chr "spray"

Sometimes this print "Class spray hidden list of 2", but not always.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions