Search code examples
rsymbolic-math

How to transform solution of Ryacas into character string?


I want to get out the solution Ryacas gives me as a character string - but it doesn't work:

> require("Ryacas")
> x <- Sym("x")
> expr <- Solve(x + 1 == 0, x)
> expr
expression(list(x == -1))
> as.character(expr)
[1] "( Solve( ( ( x + 1 ) == 0 ) , x ) )"

Strangely enough when I print the variable I get the solution but when I try to read it out as a string I get the original equation.

My question
How can I transform the solution of Ryacas into a character string? (So that I can modify it further with standard R?)


Solution

  • If you want to get that expression into a string, you should use Eval() to get the results of evaluation

    Eval(expr)
    # [[1]]
    # expression(x == -1)
    

    if you want to extract the result as a character, in this case you can do

    Eval(expr)[[1]][[1]]
    # [1] "( x == -1 )"