core.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package R
  2. /*
  3. #cgo LDFLAGS: -lm -lR
  4. #define CSTACK_DEFNS 1
  5. #include <stdlib.h>
  6. #include <Rinterface.h>
  7. #include <R.h>
  8. #include <Rinternals.h>
  9. #include <Rdefines.h>
  10. #include <R_ext/Parse.h>
  11. #include <Rembedded.h>
  12. int initR() {
  13. char *argv[] = {"REmbeddedMy", "--gui=none", "--silent", "--slave"};
  14. int argc = sizeof(argv)/sizeof(argv[0]);
  15. int result = Rf_initEmbeddedR(argc, argv);
  16. R_CStackLimit = (uintptr_t)-1;
  17. R_Interactive = (Rboolean)0;
  18. return result;
  19. }
  20. */
  21. import "C"
  22. import (
  23. "fmt"
  24. "unsafe"
  25. )
  26. var (
  27. isInitialized int
  28. )
  29. type RProtector interface {
  30. Protect(sexpr C.SEXP) RProtector
  31. Unprotect()
  32. }
  33. type protector struct {
  34. count int
  35. }
  36. func Protect(sexpr C.SEXP) RProtector {
  37. p := protector{}
  38. p.Protect(sexpr)
  39. return &p
  40. }
  41. func (this *protector) Protect(sexpr C.SEXP) RProtector {
  42. C.Rf_protect(sexpr)
  43. this.count++
  44. return this
  45. }
  46. func (this *protector) Unprotect() {
  47. C.Rf_unprotect(C.int(this.count))
  48. this.count = 0
  49. }
  50. func EvalOrDie(expression string) *Result {
  51. r, err := Eval(expression)
  52. if err != nil {
  53. panic(fmt.Sprintf("FAILED: %s", err))
  54. }
  55. return r
  56. }
  57. func Eval(expression string) (*Result, error) {
  58. var status C.ParseStatus
  59. cmd := C.CString(expression)
  60. defer C.free(unsafe.Pointer(cmd))
  61. cmdRChar := C.mkChar(cmd)
  62. protector := Protect(cmdRChar)
  63. defer protector.Unprotect()
  64. cmdSexp := C.allocVector(C.STRSXP, 1)
  65. protector.Protect(cmdSexp)
  66. C.SET_STRING_ELT(cmdSexp, 0, cmdRChar)
  67. parsedCmd := C.R_ParseVector(cmdSexp, -1, (*C.ParseStatus)(unsafe.Pointer(&status)), C.R_NilValue)
  68. if status != C.PARSE_OK {
  69. return nil, fmt.Errorf("Invalid command: %s", C.GoString(cmd))
  70. }
  71. protector.Protect(parsedCmd)
  72. var result C.SEXP
  73. errorOccured := 0
  74. /* Loop is needed here as EXPSEXP will be of length > 1 */
  75. for i := 0; i < int(C.Rf_length(parsedCmd)); i++ {
  76. result = C.R_tryEval(C.VECTOR_ELT(parsedCmd, C.R_xlen_t(i)), C.R_GlobalEnv, (*C.int)(unsafe.Pointer(&errorOccured))) //R 3.0
  77. if errorOccured != 0 {
  78. return nil, fmt.Errorf("R error occured executing: %s", C.GoString(cmd))
  79. }
  80. }
  81. return NewResult(result), nil
  82. }
  83. func SetSymbol(name string, val Expression) {
  84. nameC := C.CString(name)
  85. defer C.free(unsafe.Pointer(nameC))
  86. C.Rf_protect(val.ToSexp())
  87. defer C.Rf_unprotect(1)
  88. C.defineVar(C.install(nameC), val.ToSexp(), C.R_GlobalEnv)
  89. }
  90. func VariableExists(name string) bool {
  91. nameC := C.CString(name)
  92. defer C.free(unsafe.Pointer(nameC))
  93. return C.findVar(C.install(nameC), C.R_GlobalEnv) == C.R_UnboundValue
  94. }
  95. func Init() int {
  96. if isInitialized != 0 {
  97. return isInitialized
  98. }
  99. isInitialized = int(C.initR())
  100. return isInitialized
  101. }