---
output:
 md_document:
   variant: markdown_github
---




```{r}
library("RNeXML")
library("dplyr")
library("geiger")
knitr::opts_chunk$set(message = FALSE, warning=FALSE, comment = NA)
```

Let's generate a `NeXML` file using the tree and trait data from the `geiger` package's "primates" data:

```{r}
data("primates")
add_trees(primates$phy) %>% 
  add_characters(primates$dat, ., append=TRUE) %>% 
  taxize_nexml() -> nex 
```

(Note that we've used `dplyr`'s cute pipe syntax, but unfortunately our `add_` methods take the `nexml` object as the _second_
argument instead of the first, so this isn't as elegant since we need the stupid `.` to show where the piped output should go...)

We now read in the three tables of interest.  Note that we tell `get_characters` to give us species labels as there own column, rather than as rownames.  The latter is the default only because this plays more nicely with the default format for character matrices that is expected by `geiger` and other phylogenetics packages, but is in general a silly choice for data manipulation. 

```{r}
otu_meta <- get_metadata(nex, "otus/otu")
taxa <- get_taxa(nex)
char <- get_characters(nex, rownames_as_col = TRUE)
```


We can take a peek at what the tables look like, just to orient ourselves:

```{r}
otu_meta
taxa
head(char)
```

Now that we have nice `data.frame` objects for all our data, it's easy to join them into the desired table with a few obvious `dplyr` commands:

```{r}
taxa %>% 
  left_join(char, by = c("label" = "taxa")) %>% 
  left_join(otu_meta, by = c("id" = "otu")) %>%
  select(id, label, x, href)
```

Because these are all from the same otus block anyway, we haven't selected that column, but were it of interest it is also available in the taxa table.

