Skip to content

Commit

Permalink
Merge pull request #175 from JuliaInterop/randy3k/convert
Browse files Browse the repository at this point in the history
major refactoring of conversion related code
  • Loading branch information
randy3k authored Mar 22, 2017
2 parents 35a4095 + f1c55d4 commit b6e1d5e
Show file tree
Hide file tree
Showing 31 changed files with 1,257 additions and 515 deletions.
8 changes: 5 additions & 3 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
julia 0.5
DataStructures 0.4.3
DataStructures 0.5.0
DataFrames 0.9
NullableArrays 0.0.10
CategoricalArrays 0.0.6
NullableArrays 0.1.0
CategoricalArrays 0.1.0
AxisArrays 0.0.6
NamedArrays 0.5.3
Compat 0.20.0
@windows WinReg 0.2.0
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ makedocs(
"Introduction" => "index.md",
"Installation" => "installation.md",
"Getting Started" => "gettingstarted.md",
"Conversions" => "conversions.md",
"Internal" => "internal.md"
]
)
Expand Down
123 changes: 123 additions & 0 deletions docs/src/conversions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Conversions

RCall supports conversions to and from most base Julia types and popular Statistics packages, e.g., `DataFrames`, `DataArrays`, `NullableArrays`, `CategoricalArrays` `NamedArrays` and `AxisArrays`.

```@setup 1
using RCall
using DataFrames
using NamedArrays
using AxisArrays
```

## Base Julia Types

```@example 1
# Julia -> R
a = RObject(1)
```

```@example 1
# R -> Julia
rcopy(a)
```

```@example 1
# Julia -> R
a = RObject([1.0, 2.0])
```

```@example 1
# R -> Julia
rcopy(a)
```

## Dictionaries

```@example 1
# Julia -> R
d = Dict(:a => 1, :b => [4, 5, 3])
r = RObject(d)
```

```@example 1
# R -> Julia
rcopy(r)
```

## Date

```@example 1
# Julia -> R
d = Date(2012, 12, 12)
r = RObject(d)
```

```@example 1
# R -> Julia
rcopy(r)
```

## DateTime

```@example 1
# julia -> R
d = DateTime(2012, 12, 12, 12, 12, 12)
r = RObject(d)
```

```@example 1
# R -> Julia
rcopy(r)
```

## DataFrames and DataArrays

```@example 1
d = DataFrame([[1.0, 4.5, 7.0]], [:x])
# Julia -> R
r = RObject(d)
```

```@example 1
# R -> Julia
rcopy(r)
```

In default, the column names of R data frames are sanitized such that `foo.bar`
would be replaced by `foo_bar`.

```@example 1
rcopy(R"data.frame(a.b = 1:3)")
```

To avoid the sanitization, use `sanitize` option.
```@example 1
rcopy(R"data.frame(a.b = 1:10)"; sanitize = false)
```

## NamedArrays

```@example 1
# Julia -> R
aa = NamedArray([1,2,3], [["a", "b", "c"]], [:id])
r = RObject(aa)
```

```@example 1
# R -> Julia
rcopy(r)
```


## AxisArrays

```@example 1
# Julia -> R
aa = AxisArray([1,2,3], Axis{:id}(["a", "b", "c"]))
r = RObject(aa)
```

```@example 1
# R -> Julia
rcopy(r)
```
17 changes: 12 additions & 5 deletions docs/src/gettingstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RCall provides multiple ways to allow R interacting with Julia.
- R REPL mode
- [`@rput`](@ref) and [`@rget`](@ref) macros
- `R""` string macro
- A low level API: [`reval`](@ref), [`rcall`](@ref) and [`rcopy`](@ref) etc.
- RCall API: [`reval`](@ref), [`rcall`](@ref) and [`rcopy`](@ref) etc.

## R REPL mode
The R REPL mode allows real time switching between the Julia prompt and R prompt. Press `$` to activate the R REPL mode and the R prompt will be shown. (Press `backspace` to leave R REPL mode in case you did not know.)
Expand Down Expand Up @@ -140,13 +140,20 @@ The [`rcopy`](@ref) function converts `RObject`s to Julia objects. It uses a var

```@repl 1
rcopy(R"c(1)")
rcopy(R"c(1,2)")
rcopy(R"list(1,'zz')")
rcopy(R"list(a=1,b='zz')")
rcopy(R"c(1, 2)")
rcopy(R"list(1, 'zz')")
rcopy(R"list(a=1, b='zz')")
```

It is possible to force a specific conversion by passing the output type as the first argument:

```@repl 1
rcopy(Array{Int},R"c(1,2)")
rcopy(Array{Int}, R"c(1,2)")
```

Converters and Constructors could also be used specifically to yield the desired type.

```@repl 1
convert(Array{Float64}, R"c(1,2)")
Float64(R"1+3")
```
17 changes: 13 additions & 4 deletions src/RCall.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
__precompile__()
module RCall
using Compat, DataFrames, NullableArrays, CategoricalArrays
using Compat

using DataFrames
# using DataTables
using NullableArrays, CategoricalArrays
using AxisArrays, NamedArrays

import DataStructures: OrderedDict

Expand All @@ -21,9 +26,13 @@ include("setup.jl")
include("types.jl")
include("constants.jl")
include("methods.jl")
include("convert-base.jl")
include("convert-data.jl")
include("convert-default.jl")
include("convert/base.jl")
include("convert/dataframe.jl")
include("convert/datatable.jl")
include("convert/datetime.jl")
include("convert/axisarray.jl")
include("convert/namedarray.jl")
include("convert/default.jl")
include("eventloop.jl")
include("eval.jl")
include("io.jl")
Expand Down
31 changes: 0 additions & 31 deletions src/callback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ function registerCFinalizerEx(s::ExtPtrSxpPtr)
end


sexp(::Type{ExtPtrSxp}, s::Ptr{ExtPtrSxp}) = s
sexp(::Type{ExtPtrSxp}, r::RObject{ExtPtrSxp}) = sexp(r)
sexp(::Type{ClosSxp}, s::Ptr{ClosSxp}) = s
sexp(::Type{ClosSxp}, r::RObject{ClosSxp}) = sexp(r)

"""
Wrap a Julia object an a R `ExtPtrSxpPtr`.
Expand All @@ -114,30 +109,6 @@ end

const juliaCallback = RObject{ExtPtrSxp}()

"""
Wrap a callable Julia object `f` an a R `ClosSxpPtr`.
Constructs the following R code
function(...) .External(juliaCallback, fExPtr, ...)
"""
function sexp(::Type{ClosSxp}, f)
fptr = protect(sexp(ExtPtrSxp,f))
body = protect(rlang_p(Symbol(".External"),
juliaCallback,
fptr,
Const.DotsSymbol))
local clos
try
lang = rlang_p(:function, sexp_arglist_dots(), body)
clos = reval_p(lang)
finally
unprotect(2)
end
clos
end

"""
Create an argument list for an R function call, with a varargs "dots" at the end.
"""
Expand All @@ -162,5 +133,3 @@ function sexp_arglist_dots(args...;kwargs...)
end
rarglist
end

sexp(f::Function) = sexp(ClosSxp, f)
Loading

0 comments on commit b6e1d5e

Please sign in to comment.