Esempio n. 1
0
File: core.go Progetto: Gwill/go-R
func Eval(expression string) (*Result, error) {

	var status C.ParseStatus

	cmd := C.CString(expression)
	defer C.free(unsafe.Pointer(cmd))

	cmdRChar := C.mkChar(cmd)
	protector := Protect(cmdRChar)
	defer protector.Unprotect()

	cmdSexp := C.allocVector(C.STRSXP, 1)
	protector.Protect(cmdSexp)

	C.SET_STRING_ELT(cmdSexp, 0, cmdRChar)

	parsedCmd := C.R_ParseVector(cmdSexp, -1, (*C.ParseStatus)(unsafe.Pointer(&status)), C.R_NilValue)
	if status != C.PARSE_OK {
		return nil, fmt.Errorf("Invalid command: %s", C.GoString(cmd))
	}

	protector.Protect(parsedCmd)

	var result C.SEXP
	errorOccured := 0
	/* Loop is needed here as EXPSEXP will be of length > 1 */
	for i := 0; i < int(C.Rf_length(parsedCmd)); i++ {
		result = C.R_tryEval(C.VECTOR_ELT(parsedCmd, C.R_xlen_t(i)), C.R_GlobalEnv, (*C.int)(unsafe.Pointer(&errorOccured))) //R 3.0
		if errorOccured != 0 {
			return nil, fmt.Errorf("R error occured executing: %s", C.GoString(cmd))
		}
	}
	return NewResult(result), nil
}
Esempio n. 2
0
File: result.go Progetto: Gwill/go-R
func (this *Result) AsNumeric() *NumericVector {
	if !this.IsNumeric() {
		panic("Not a numeric vector")
	}
	v := NumericVector{}
	v.length = int(C.Rf_length(this.expr))
	v.expr = this.expr
	return &v
}
Esempio n. 3
0
File: result.go Progetto: Gwill/go-R
func (this *Result) AsGenericVector() *Vector {
	if !this.IsGenericVector() {
		panic("Not a generic vector")
	}
	v := Vector{}
	v.length = int(C.Rf_length(this.expr))
	v.expr = this.expr
	return &v

}
Esempio n. 4
0
File: result.go Progetto: Gwill/go-R
func (this *Result) AsComplex() *ComplexVector {
	if !this.IsComplex() {
		panic("Not a complex vector")
	}
	v := ComplexVector{}
	v.length = int(C.Rf_length(this.expr))
	v.expr = this.expr
	return &v

}