Skip to content

Commit

Permalink
Removed the selected parameter in favor of get_selected.
Browse files Browse the repository at this point in the history
  • Loading branch information
trestletech committed Nov 20, 2014
1 parent 9930b98 commit 8befa75
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 40 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
@@ -1,4 +1,5 @@
# Generated by roxygen2 (4.0.2): do not edit by hand

export(get_selected)
export(renderTree)
export(shinyTree)
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -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
--------------------------------------------------------------------------------
Expand Down
41 changes: 41 additions & 0 deletions 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){

}
17 changes: 2 additions & 15 deletions R/shiny-tree.R
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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)
)
}
6 changes: 3 additions & 3 deletions inst/examples/04-selected/server.R
Expand Up @@ -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))
}
})
})
4 changes: 2 additions & 2 deletions inst/examples/04-selected/ui.R
Expand Up @@ -9,13 +9,13 @@ shinyUI(
headerPanel("shinyTree with 'selected' input"),

sidebarPanel(
helpText(HTML("An example of using shinyTree's <code>selected</code> parameter to monitor which cells are currently selected.
helpText(HTML("An example of using shinyTree's <code>get_selected</code> function to extract the cells which are currently selected.
<hr>Created using <a href = \"http://github.com/trestletech/shinyTree\">shinyTree</a>."))
),
mainPanel(
"Currently Selected:",
verbatimTextOutput("selTxt"),
hr(),
shinyTree("tree", selected="treeSel")
shinyTree("tree")
)
))
15 changes: 3 additions & 12 deletions inst/www/shinyTree.js
Expand Up @@ -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');
Expand All @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions 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.
}
10 changes: 2 additions & 8 deletions 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.}
Expand Down

0 comments on commit 8befa75

Please sign in to comment.