# Groups and TileDB-R API

**URL:** https://forum.tiledb.com/t/groups-and-tiledb-r-api/667
**Category:** Uncategorized
**Created:** [March 20, 2024, 4:06pm UTC](https://forum.tiledb.com/t/groups-and-tiledb-r-api/667 "2024-03-20T16:06:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jerlinden](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@jerlinden](https://forum.tiledb.com/u/jerlinden)
#### Post date: [March 20, 2024, 4:06pm UTC](https://forum.tiledb.com/t/groups-and-tiledb-r-api/667/1 "2024-03-20T16:06:30Z")

</div>

Hi!

I was looking to use grouped arrays within TileDB R API, but I seem to have hit a wall . Here is a reproducible example below:

> library(tiledb)
> 
> ## Data to store
> 
> data\_1 ← list(a = c(1:3), b = c(“A”, “B”, “C”))  
> data\_2 ← list(a = c(4:6), b = c(“D”, “E”, “F”))
> 
> ## URIs
> 
> uri\_1 ← paste0(getwd(), “/”, “array\_1”)  
> if(tiledb\_vfs\_is\_dir(uri\_1)) {tiledb\_vfs\_remove\_dir(uri\_1)}  
> uri\_2 ← paste0(getwd(), “/”, “array\_2”)  
> if(tiledb\_vfs\_is\_dir(uri\_2)) {tiledb\_vfs\_remove\_dir(uri\_2)}
> 
> ## Define the TileDB array
> 
> dim ← tiledb\_dim(name = ‘dimension’,  
> domain = c(1L, 3L),  
> tile = 1L,  
> type = “INT32”)  
> attr\_a ← tiledb\_attr(name = “a”, type = “INT32”, nullable = TRUE)  
> attr\_b ← tiledb\_attr(name = “b”, type = “CHAR”, nullable = TRUE, ncells = NA)  
> dom ← tiledb\_domain(dims = c(dim))  
> schema ← tiledb\_array\_schema(domain = dom,  
> attrs = c(attr\_a, attr\_b),  
> sparse = FALSE)  
> tiledb\_array\_create(uri\_1, schema)  
> tiledb\_array\_create(uri\_2, schema)
> 
> ## Write data
> 
> write\_array ← tiledb\_array(uri = uri\_1)  
> write\_array ← data.frame(data\_1, row.names = c(1L:3L))  
> write\_array ← tiledb\_array(uri = uri\_2)  
> write\_array ← data.frame(data\_2, row.names = c(1L:3L))
> 
> ## Group arrays
> 
> uri\_grp = paste0(getwd(), “/”, “group”)  
> if(tiledb\_vfs\_is\_dir(uri\_grp)) {tiledb\_vfs\_remove\_dir(uri\_grp)}
> 
> tiledb\_group\_create(uri = uri\_grp)  
> group ← tiledb\_group(uri = uri\_grp,  
> type = c(“WRITE”))  
> tiledb\_group\_add\_member(grp = group,  
> uri = uri\_1,  
> relative = FALSE,  
> name = “array\_1”)  
> tiledb\_group\_add\_member(grp = group,  
> uri = uri\_2,  
> relative = FALSE,  
> name = “array\_1”)
> 
> # Check if groups have been populated:
> 
> group  
> tiledb\_group\_member\_count(tiledb\_group(uri = uri\_grp, type = “READ”))

The group I have created do not contain anything except a name, and it seems the tiled\_group\_add\_member command does not do anything.

> > group  
> > group GROUP  
> > tiledb\_object\_type(uri = uri\_grp)  
> > [1] “GROUP”  
> > tiledb\_group\_member\_count(tiledb\_group(uri = uri\_grp, type = “READ”))  
> > [1] 0

Would anyone have else encountered the same issue?

J.

---

<div class="post-metadata">

### Author: ![Dirk\_Eddelbuettel](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/dirk_eddelbuettel/32/54_2.png) [@Dirk\_Eddelbuettel](https://forum.tiledb.com/u/Dirk_Eddelbuettel)
#### Post date: [March 20, 2024, 4:40pm UTC](https://forum.tiledb.com/t/groups-and-tiledb-r-api/667/2 "2024-03-20T16:40:43Z")

</div>

Hi Jérôme,

Thanks for getting in touch, and for posting a detailed reproducible example. After swapping out all the non-printable character (which may have come in via the html rendering, see below for a hint on how to avoid that) I can reproduce almost all.

But for example fail on the second `tiledb_group_add_member` as you supply a new uri but reuse the name leading to an error:

```auto
> tiledb_group_add_member(grp = group, uri = uri_1, relative = FALSE, name = "array_1")
> tiledb_group_add_member(grp = group, uri = uri_2, relative = FALSE, name = "array_1")
Error: Group Details: Cannot add group member array_1, a member with the same name or URI has already been added.
> 

```

Of course that is easy to fix. I can then replicate the finding that the count returns as zero. However, it is also worth noting that you (accidentally?) wrote the arrays _next to rather than inside_ the group directory. From listing in the temporary directory I used:

```sh
edd@rob:/tmp/tiledb$ ls -ld array_? group/
drwxr-xr-x 8 edd edd 4096 Mar 20 11:14 array_1
drwxr-xr-x 8 edd edd 4096 Mar 20 11:14 array_2
drwxr-xr-x 4 edd edd 4096 Mar 20 11:15 group/
edd@rob:/tmp/tiledb$ 

```

Below is a quick demo of writing arrays and marking them as members of a group along with count checks. This should get you onto the right track. If not, please do not hesitate to follow-up here, via a GitHub issue or in email (where I can be reached at my first name at tiledb dot com).

With best regards, Dirk

```r
library(tiledb)
setwd("/tmp/tiledb") # adjust locally

uri <- file.path(getwd(), "demo_group") # prefer absolute path here, likely not required
if (tiledb_vfs_is_dir(uri)) tiledb_vfs_remove_dir(uri)

tiledb_group_create(uri)
grp <- tiledb_group(uri)
grp <- tiledb_group_close(grp)

## create some temp arrays to adds as groups
uri1 <- file.path(uri, "tic")
uri2 <- file.path(uri, "tac")
uri3 <- file.path(uri, "toe")
df1 <- data.frame(val=seq(100, 200, by=10))
df2 <- data.frame(letters=letters)
df3 <- data.frame(nine=rep(9L, 9))
tiledb::fromDataFrame(df1, uri1)
tiledb::fromDataFrame(df2, uri2)
tiledb::fromDataFrame(df3, uri3)

## add member
grp <- tiledb_group_open(grp, "WRITE")
grp <- tiledb_group_add_member(grp, uri1, FALSE) # use absolute URL
grp <- tiledb_group_close(grp)
grp <- tiledb_group_open(grp, "READ")
cat("Cound should now be one:", tiledb_group_member_count(grp), "\n")
grp <- tiledb_group_close(grp)
grp <- tiledb_group_open(grp, "WRITE")
grp <- tiledb_group_add_member(grp, uri2, FALSE) # use absolute URL
grp <- tiledb_group_add_member(grp, uri3, FALSE) # use absolute URL
grp <- tiledb_group_close(grp)
grp <- tiledb_group_open(grp, "READ")
cat("Cound should now be three:", tiledb_group_member_count(grp), "\n")

```

The reference output and resulting directories (relative to my chosen target directory) are:

```sh
$ Rscript post_20240320_reply.R
Cound should now be one: 1 
Cound should now be three: 3 
$ ls -ld /tmp/tiledb/demo_group/*
drwxr-xr-x 2 edd edd 4096 Mar 20 11:46 /tmp/tiledb/demo_group/__group
drwxr-xr-x 2 edd edd 4096 Mar 20 11:46 /tmp/tiledb/demo_group/__meta
drwxr-xr-x 8 edd edd 4096 Mar 20 11:46 /tmp/tiledb/demo_group/tac
drwxr-xr-x 8 edd edd 4096 Mar 20 11:46 /tmp/tiledb/demo_group/tic
-rw-r--r-- 1 edd edd 0 Mar 20 11:46 /tmp/tiledb/demo_group/__tiledb_group.tdb
drwxr-xr-x 8 edd edd 4096 Mar 20 11:46 /tmp/tiledb/demo_group/toe
$ 

```

PS Markdown formatting here works. So

````auto
```r
# R code here

````

starts an code, and three backticks close it.

---

<div class="post-metadata">

### Author: ![jerlinden](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@jerlinden](https://forum.tiledb.com/u/jerlinden)
#### Post date: [March 21, 2024, 3:15pm UTC](https://forum.tiledb.com/t/groups-and-tiledb-r-api/667/3 "2024-03-21T15:15:10Z")

</div>

Hi Dirk,

Thanks a lot for your help. The problem was indeed the relative position of group’s and arrays’s URIs, it was not clear to me that they have to be nested, but it makes total sense now!
