How to write a vector ?

27 views
Skip to first unread message

Stéphane Laurent

unread,
Sep 9, 2017, 11:52:46 AM9/9/17
to haskellr
Hello, 

 I've tried to use writeVector like this:

foreign export ccall vec :: Ptr (SEXP s R.Vector) -> Ptr (SEXP s R.Vector) -> IO ()
vec
:: Ptr (SEXP s R.Vector) -> Ptr (SEXP s R.Vector) -> IO ()
vec input result
= do
  input
<- peek input
  let x
= DV.toSEXP $ DV.fromList ([5] :: [Int32]) :: SEXP s R.Int
 
out <- writeVector input 2 x
  poke result
out


I compile to a DLL and I call in R like this:

> .C("vec", input=list(list(1L,2L,3L,4L)), result=list(0L))$result
[[1]]
[1] 5


Why the output is 5 ? I expected list(1,2,5,4).

Another but related question. Assume I have e.g. two SEXP s R.Int objects in Haskell. Is it possible to create the object SEXP s R.Vector corresponding to the list of the two SEXP s R.Int objects ?

Stéphane Laurent

unread,
Sep 9, 2017, 3:31:40 PM9/9/17
to haskellr
Another but related question. Assume I have e.g. two SEXP s R.Int objects in Haskell. Is it possible to create the object SEXP s R.Vector corresponding to the list of the two SEXP s R.Int objects ?

I've managed to do that !!

foreign export ccall makeVector :: Ptr (SEXP s R.Int) -> Ptr CInt -> Ptr (SEXP s R.Vector) -> IO ()
makeVector
:: Ptr (SEXP s R.Int) -> Ptr CInt -> Ptr (SEXP s R.Vector) -> IO ()
makeVector list l result
= do
  l
<- peek l
  list
<- peekArray (fromIntegral l :: Int) list
  poke result $ mkProtectedSEXPVector sing list


> vectors <- list(c(2L, 3L), c(1L))
> .C("makeVector", list=vectors, l=length(vectors), result=list(0L))$result
[[1]]
[[1]][[1]]
[1] 2 3


[[1]][[2]]
[1] 1

Stéphane Laurent

unread,
Sep 15, 2017, 12:45:33 PM9/15/17
to haskellr
I've figured out what I was doing wrong with writeVector: https://github.com/tweag/HaskellR/issues/298
Reply all
Reply to author
Forward
0 new messages