Skip to content

Commit

Permalink
- Various fixes for environments
Browse files Browse the repository at this point in the history
- Added a method "keys"
- Tentative type "RPackage"
- Function "rimport" (pretty much like "importr()" in rpy2, but following
  the naming in PyCall ("pyimport")
  • Loading branch information
lgautier committed Aug 25, 2014
1 parent 4ef09ce commit a5f7e64
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
20 changes: 19 additions & 1 deletion src/Rif.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Rif

using Base
#import Base.dlopen, Base.dlsym, Base.length
import Base.setindex!, Base.getindex,
import Base.setindex!, Base.getindex, Base.get,
Base.convert,
Base.eltype,
Base.length, Base.map,
Expand All @@ -18,6 +18,7 @@ export initr, isinitialized, isbusy, hasinitargs, setinitargs, getinitargs,
Sexp, AbstractSexp,
RDataArray, AbstractRDataArray,
getindex, setindex!, map, del,
keys,
call, names, ndims,
convert,
getGlobalEnv, getBaseEnv,
Expand Down Expand Up @@ -349,4 +350,21 @@ function requireR(name::ASCIIString)
evalR(e)
end

type RPackage
env::REnvironment
end

function get(rpack::RPackage, symbol::ASCIIString)
return get(rpack.env, symbol)
end

function rimport(name::ASCIIString)
requireR(name)
be = getBaseEnv()
as_environment = get(be, "as.environment")
env = call(as_environment, "package:" * name)
res = RPackage(env)
return res
end

end
22 changes: 17 additions & 5 deletions src/environments.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include("Rif/src/embeddedr.jl")
#include("Rif/src/sexp.jl")

Expand All @@ -13,7 +12,20 @@ type REnvironment <: AbstractSexp

function REnvironment(x::Sexp)
new(x)
end
end

end

function convert(::Type{Sexp}, x::REnvironment)
return Sexp(x.sexp)
end

function keys(env::REnvironment)
#FIXME: speedup by having r_ls as a global
be = getBaseEnv()
r_ls = get(be, "ls")
res = Rif.call(r_ls, [], ["envir" => env])
return res
end

function getindex(x::REnvironment, i::ASCIIString)
Expand All @@ -35,12 +47,11 @@ function del(x::REnvironment, i::ASCIIString)
end
end

import Base.get
#FIXME: implement get for UTF8 symbols
function get(environment::REnvironment, symbol::ASCIIString)
c_ptr = ccall(dlsym(libri, :SexpEnvironment_get), Ptr{Void},
(Ptr{Void}, Ptr{Uint8}),
environment.sexp, symbol)
(Ptr{Void}, Ptr{Uint8}),
environment.sexp, symbol)
# evaluate if promise
if (@_RL_TYPEOFR(c_ptr)) == PROMSXP
c_ptr = ccall(dlsym(libri, :Sexp_evalPromise), Ptr{Void},
Expand All @@ -61,3 +72,4 @@ function getBaseEnv()
())
return REnvironment(res)
end

0 comments on commit a5f7e64

Please sign in to comment.