From 8befa753f4f9a06584b835e6349829077fe86875 Mon Sep 17 00:00:00 2001 From: trestletech Date: Thu, 20 Nov 2014 00:31:06 -0600 Subject: [PATCH] Removed the selected parameter in favor of get_selected. --- NAMESPACE | 1 + NEWS | 2 ++ R/get-selected.R | 41 ++++++++++++++++++++++++++++++ R/shiny-tree.R | 17 ++----------- inst/examples/04-selected/server.R | 6 ++--- inst/examples/04-selected/ui.R | 4 +-- inst/www/shinyTree.js | 15 +++-------- man/get_selected.Rd | 22 ++++++++++++++++ man/shinyTree.Rd | 10 ++------ 9 files changed, 78 insertions(+), 40 deletions(-) create mode 100644 R/get-selected.R create mode 100644 man/get_selected.Rd 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.
Created using shinyTree.")) ), mainPanel( "Currently Selected:", verbatimTextOutput("selTxt"), hr(), - shinyTree("tree", selected="treeSel") + shinyTree("tree") ) )) \ No newline at end of file diff --git a/inst/www/shinyTree.js b/inst/www/shinyTree.js index a1a7ac4..a3f874d 100644 --- a/inst/www/shinyTree.js +++ b/inst/www/shinyTree.js @@ -22,18 +22,6 @@ var shinyTree = function(){ var tree = $(el).jstree({'core' : { "check_callback" : ($elem.data('st-dnd') === 'TRUE') },plugins: plugins}); - - var selectedId = $elem.data('st-selected') - if (selectedId) { - $elem.on("changed.jstree", function(e, data) { - // Get $elements - var $sels = $('#' + data.selected.join(',#')) - // Remove descendant nodes in the clone before getting text. - // TODO: We don't really want to trim but have to b/c of Shiny's pretty-printing. - var names = $sels.map(function(i, el){return $(el).clone().find('ul').remove().end().text().trim(); }).get() - Shiny.onInputChange(selectedId, names) - }); - } } }); Shiny.outputBindings.register(treeOutput, 'shinyTree.treeOutput'); @@ -43,6 +31,9 @@ var shinyTree = function(){ find: function(scope) { return $(scope).find(".shiny-tree"); }, + getType: function(){ + return "shinyTable" + }, getValue: function(el, keys) { /** * Prune an object recursively to only include the specified keys. diff --git a/man/get_selected.Rd b/man/get_selected.Rd new file mode 100644 index 0000000..9b843e1 --- /dev/null +++ b/man/get_selected.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2 (4.0.2): do not edit by hand +\name{get_selected} +\alias{get_selected} +\title{Get the selected nodes from a tree} +\usage{ +get_selected(tree, format = c("names", "slices")) +} +\arguments{ + \item{tree}{The \code{input$tree} shinyTree you want to + inspect.} + + \item{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.} +} +\description{ +Extract the nodes from the tree that are selected in a more +convenient format. You can choose which format you prefer. +} + diff --git a/man/shinyTree.Rd b/man/shinyTree.Rd index 5146858..9cabb16 100644 --- a/man/shinyTree.Rd +++ b/man/shinyTree.Rd @@ -1,19 +1,13 @@ -% Generated by roxygen2 (4.0.1): do not edit by hand +% Generated by roxygen2 (4.0.2): do not edit by hand \name{shinyTree} \alias{shinyTree} \title{Create a Shiny Tree} \usage{ -shinyTree(outputId, selected = NULL, checkbox = FALSE, search = FALSE, - dragAndDrop = FALSE) +shinyTree(outputId, checkbox = FALSE, search = FALSE, dragAndDrop = FALSE) } \arguments{ \item{outputId}{The ID associated with this element} - \item{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.} - \item{checkbox}{If \code{TRUE}, will enable checkboxes next to each node to make the selection of multiple nodes in the tree easier.}