result.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package R
  2. /*
  3. #cgo LDFLAGS: -lm -lR
  4. #cgo CFLAGS: -I /usr/share/R/include/
  5. #include <stdlib.h>
  6. #include <R.h>
  7. #include <Rinternals.h>
  8. #include <Rdefines.h>
  9. #include <R_ext/Parse.h>
  10. #include <Rembedded.h>
  11. */
  12. import "C"
  13. type Result struct {
  14. expr C.SEXP
  15. }
  16. func NewResult(expr C.SEXP) *Result {
  17. return &Result{expr: expr}
  18. }
  19. func (this *Result) IsNumeric() bool {
  20. return C.Rf_isReal(this.expr) != 0
  21. }
  22. func (this *Result) IsComplex() bool {
  23. return C.Rf_isComplex(this.expr) != 0
  24. }
  25. func (this *Result) AsComplex() *ComplexVector {
  26. if !this.IsComplex() {
  27. panic("Not a complex vector")
  28. }
  29. v := ComplexVector{}
  30. v.length = int(C.Rf_length(this.expr))
  31. v.expr = this.expr
  32. return &v
  33. }
  34. func (this *Result) AsNumeric() *NumericVector {
  35. if !this.IsNumeric() {
  36. panic("Not a numeric vector")
  37. }
  38. v := NumericVector{}
  39. v.length = int(C.Rf_length(this.expr))
  40. v.expr = this.expr
  41. return &v
  42. }
  43. func (this *Result) IsGenericVector() bool {
  44. return C.Rf_isVector(this.expr) != 0
  45. }
  46. func (this *Result) AsGenericVector() *Vector {
  47. if !this.IsGenericVector() {
  48. panic("Not a generic vector")
  49. }
  50. v := Vector{}
  51. v.length = int(C.Rf_length(this.expr))
  52. v.expr = this.expr
  53. return &v
  54. }