library(ptrapr) library(magrittr) library(igraph) library(dplyr) library(purrr) library(stringr) library(tidyr)
You can easily extract features from a panicle object, such as number of branches and spikelets. Any panicle object is build upon an igraph
object, so you can use functions from igraph
to extract features from panicles. Or learn a few tricks below.
We can start from the object good_panicle
.
good_panicle %>% plot_panicle()
The function vertex_attr()
from igraph extracts the attributes from all the nodes (vertexes) of a panicle.
nodes_df <- good_panicle %>% vertex_attr() %>% # The output of vertex_attr() is a list, # but we can manipulate it more easily # if we turn it into a data frame as_tibble()
This is the output of vertex_attr()
, the column type
stores the type of each node (primary, secondary, etc.):
# use head() to take only the first 6 rows nodes_df %>% head() %>% knitr::kable()
We can count the feature with dplyr's count()
. These are the features from one panicle.
pan_feats <- nodes_df %>% group_by(type) %>% count()
pan_feats %>% knitr::kable()
As in the previous vignette "Visualize Many Panicles at Once with a Loop", you can parse many panicles and put them in a list. Check that vignette for a thorough explanation of these step:
path_to_folder <- system.file(package = "ptrapr") %>% paste0("/extdata") # First record the names of the files # substitute path_to_folder with the path to your folder panicle_paths <- list.files(path_to_folder) %>% # remove the extension (variable part) # each panicle has two files with different extension str_remove(".ricegr|.ricepr") %>% # store the name of the files without the extension unique() # Store file names in named object, to use them later panicle_paths <- panicle_paths %>% {set_names(x = paste(path_to_folder, ., sep = "/"), nm = .)} # Parse them in a list, using file name as list names panicle_list <- panicle_paths %>% map(~read_full_panicle(.)) # Then correct the edges on the main axis with invert_edges() panicle_list <- panicle_list %>% map(~invert_edges(., check_before = TRUE)) names(panicle_list)
Now all the panicles are collected in a named list,
you can use map()
, bind_rows()
and spread()
to collect all the features in a database.
panicles_feature <- panicle_list %>% # This is the core of the loop in which # we apply the steps to extract panicle features # on all the panicles map(~vertex_attr(.) %>% as_tibble() %>% group_by(type) %>% count()) %>% # The next steps transform the list of panicle features # into a tidy dataset bind_rows(.id = "id") %>% spread(key = "type", value = "n")
panicles_feature %>% knitr::kable()
The last step, spread()
, is not necessary, but it is suggested to tidy your dataset.
Since, according to the tidy data concept, each observation must have its own row. Here we use spread()
to collect all observations about one single panicle that are scattered across multiple rows, and to put them in one single row.
Let me know if you want me to code these steps in a function.
Check the igraph website for a details about it, also tidygraph can be useful.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.