[R] problem with toString
Tobias Elze
tobias-elze at tobias-elze.de
Fri Aug 30 14:03:10 CEST 2002
>Why are you using toString? The main purpose of toString is to
>get truncate the character representation of objects: otherwise
>as.character would suffice.
>
>The problem you are seeing is in as.character:
>
>> as.character(q2)
>[1] "c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, "
>
>and it comes from the cutoff value of 60 in deparse. I would use
>
>tmp <- deparse(q2, 500)
>substring(tmp, 12, nchar(tmp)-1)
>
>--
>Brian D. Ripley, ripley at stats.ox.ac.uk
>Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>University of Oxford, Tel: +44 1865 272861 (self)
>1 South Parks Road, +44 1865 272860 (secr)
>Oxford OX1 3TG, UK Fax: +44 1865 272595
Thank you for your helpful answer. As far as I can understand, that means if I don't know how long my expression q is, I
should use something as this:
q <- expression(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19))
d <- deparse(q)
s=""
for (i in 1:length(d))
{
s <- paste(tmp, d[i])
}
substring(s, 13, nchar(s)-1)
(The superfluous spaces in string s don't bother me and can easily be removed.) Maybe one could use a shorter method
using apply for this?
I'd like to add some additional information about the background answering the question why I need this: I am planning to
write a kind of plugin of a GPL Computer Algebra System to R enabling it to calculate symbolically.
What I have done is to use unevaluated expressions as input to my symbolic functions. These expressions are converted to
strings (therefore my question), the strings are syntactically converted and then piped to the CAS from which I get strings
back. Those answer strings are then converted into R expressions again, returned by the function. For example:
> x<-expression(pi/3)
> sym.sin(x) # symbolic sine function
expression(sqrt(3)/2)
There are still some problems doing it this way: Long integers and fractions are shortened during the conversion from string
to R expression. If I get back 1000! as a string, R will make it to inf. But this is due to the fact that R has never been
intended to do such work, I know... (I will use unconverted strings in such cases)
By the way, converting strings to R expressions I am using parse together with temporary files. Is there an easier way doing
this without temp files?
All the same, my testing results are promising: Even although R has not been intended to calculate symbolically, many things
seem to be implementable. And maybe I can make a package for R in the future containing the sources of the CAS, providing
many essential symbolic functions - I think this could be of general interest.
Thanks once more for your help,
king regards,
Tobias Elze.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list