diff --git a/NAMESPACE b/NAMESPACE
index dc57a93..6e82047 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -1,4 +1,5 @@
# Generated by roxygen2 (4.0.2): do not edit by hand
+export(get_selected)
export(renderTree)
export(shinyTree)
diff --git a/NEWS b/NEWS
index fcdd414..f019141 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,8 @@
- BREAKING CHANGE: Model the `input$list`-style lists after the list structure
provided to `renderTree`, meaning that they are now a simple are list with the
same `st`- prefixed attributes. See `inst/examples/05-structure`.
+- BREAKING CHANGE: removed `selected` attribute from `shinyTree()` in favor of
+ `get_selected()`. See `inst/examples/04-selected/`.
v 0.1.0
--------------------------------------------------------------------------------
diff --git a/R/get-selected.R b/R/get-selected.R
new file mode 100644
index 0000000..5a4e24f
--- /dev/null
+++ b/R/get-selected.R
@@ -0,0 +1,41 @@
+#' Get the selected nodes from a tree
+#'
+#' Extract the nodes from the tree that are selected in a more
+#' convenient format. You can choose which format you prefer.
+#'
+#' @param tree The \code{input$tree} shinyTree you want to
+#' inspect.
+#' @param format In which format you want the output. Use
+#' \code{names} to get a simple list of the names (with attributes
+#' describing the node's ancestry), or \code{slices} to get a list
+#' of lists, each of which is a slice of the list used to get down
+#' to the selected node.
+#' @export
+get_selected <- function(tree, format=c("names", "slices")){
+ format <- match.arg(format, c("names", "slices"), FALSE)
+ switch(format,
+ "names"=get_selected_names(tree),
+ "slices"=get_selected_slices(tree))
+}
+
+get_selected_names <- function(tree, ancestry=NULL, vec=list()){
+ if (is.list(tree)){
+ for (i in 1:length(tree)){
+ anc <- c(ancestry, names(tree)[i])
+ vec <- get_selected_names(tree[[i]], anc, vec)
+ }
+ }
+
+ a <- attr(tree, "stselected", TRUE)
+ if (!is.null(a) && a == TRUE){
+ # Get the element name
+ el <- tail(ancestry,n=1)
+ vec[length(vec)+1] <- el
+ attr(vec[[length(vec)]], "ancestry") <- head(ancestry, n=length(ancestry)-1)
+ }
+ return(vec)
+}
+
+get_selected_slices <- function(tree){
+
+}
\ No newline at end of file
diff --git a/R/shiny-tree.R b/R/shiny-tree.R
index 3b89e3f..267e42a 100644
--- a/R/shiny-tree.R
+++ b/R/shiny-tree.R
@@ -3,9 +3,6 @@
#' This creates a spot in your Shiny UI for a shinyTree which can then be filled
#' in using \code{\link{renderTree}}
#' @param outputId The ID associated with this element
-#' @param selected The input ID associated with tree's currently selected
-#' elements. The names of the node(s) that are currently selected in the tree
-#' will be available in an input by this name.
#' @param checkbox If \code{TRUE}, will enable checkboxes next to each node to
#' make the selection of multiple nodes in the tree easier.
#' @param search If \code{TRUE}, will enable search functionality in the tree by adding
@@ -15,16 +12,7 @@
#' tree.
#' @seealso \code{\link{renderTree}}
#' @export
-shinyTree <- function(outputId, selected=NULL, checkbox=FALSE, search=FALSE, dragAndDrop=FALSE){
- if (is.null(selected)) {
- selected <- ""
- } else {
- if (!is.character(selected)) {
- stop("`selected` must either be NULL or a character value.")
- }
- }
-
-
+shinyTree <- function(outputId, checkbox=FALSE, search=FALSE, dragAndDrop=FALSE){
searchEl <- div("")
if (search == TRUE){
search <- paste0(outputId, "-search-input")
@@ -51,8 +39,7 @@ shinyTree <- function(outputId, selected=NULL, checkbox=FALSE, search=FALSE, dra
searchEl,
div(id=outputId, class="shiny-tree",
`data-st-checkbox`=checkbox,
- `data-st-search`=is.character(search),
- `data-st-selected`=selected,
+ `data-st-search`=is.character(search),
`data-st-dnd`=dragAndDrop)
)
}
\ No newline at end of file
diff --git a/inst/examples/04-selected/server.R b/inst/examples/04-selected/server.R
index f05af48..69df227 100644
--- a/inst/examples/04-selected/server.R
+++ b/inst/examples/04-selected/server.R
@@ -17,11 +17,11 @@ shinyServer(function(input, output, session) {
})
output$selTxt <- renderText({
- sel <- input$treeSel
- if (is.null(sel)){
+ tree <- input$tree
+ if (is.null(tree)){
"None"
} else{
- sel
+ unlist(get_selected(tree))
}
})
})
\ No newline at end of file
diff --git a/inst/examples/04-selected/ui.R b/inst/examples/04-selected/ui.R
index bef3a06..59a6875 100644
--- a/inst/examples/04-selected/ui.R
+++ b/inst/examples/04-selected/ui.R
@@ -9,13 +9,13 @@ shinyUI(
headerPanel("shinyTree with 'selected' input"),
sidebarPanel(
- helpText(HTML("An example of using shinyTree's selected
parameter to monitor which cells are currently selected.
+ helpText(HTML("An example of using shinyTree's get_selected
function to extract the cells which are currently selected.