Skip to content

Commit

Permalink
added updateTreeview()
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Sep 8, 2022
1 parent 7dd6b9f commit ec42477
Show file tree
Hide file tree
Showing 17 changed files with 392 additions and 206 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shinytreeview
Title: Tree View for 'shiny' Applications
Version: 0.0.9
Version: 0.1.0
Authors@R:
c(person(given = "Victor",
family = "Perrier",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(make_tree)
export(searchTreeview)
export(treecheckInput)
export(treeviewInput)
export(updateTreeview)
importFrom(htmltools,HTML)
importFrom(htmltools,htmlDependencies)
importFrom(htmltools,htmlDependency)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# shinytreeview 0.1.0

* New function `updateTreeview()` to update label and selected value from the server.


# shinytreeview 0.0.9

* Use [phosphoricons](https://github.com/dreamRs/phosphoricons) for icons.
Expand Down
14 changes: 10 additions & 4 deletions R/input-treecheck.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#' @return Server-side: A `character` value or a `list` depending on the `return_value` argument.
#' @export
#'
#' @seealso [updateTreeview()] and others functions to manipulate tree server-side.
#'
#' @importFrom htmltools tags validateCssUnit htmlDependencies HTML
#' @importFrom shiny icon restoreInput
#' @importFrom jsonlite toJSON
Expand Down Expand Up @@ -46,10 +48,14 @@ treecheckInput <- function(inputId,

tags$div(
class = "form-group shiny-input-container",
style = if(!is.null(width)) paste("width:", validateCssUnit(width)),
if (!is.null(label)) {
tags$label(class = "control-label", `for` = inputId, label)
},
style = if (!is.null(width)) paste("width:", validateCssUnit(width)),
tags$label(
id = paste0(inputId, "-label"),
class = "control-label",
class = if (is.null(label)) "shiny-label-null",
`for` = inputId,
label
),
tags$div(
id = inputId, class = "treecheck-input",
`data-return` = return_value,
Expand Down
14 changes: 10 additions & 4 deletions R/input-treeview.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#' @return Server-side: A \code{character} value or a \code{list} depending on the \code{return_value} argument.
#' @export
#'
#' @seealso [updateTreeview()] and others functions to manipulate tree server-side.
#'
#' @importFrom htmltools tags validateCssUnit HTML
#' @importFrom jsonlite toJSON
#' @importFrom shiny restoreInput
Expand Down Expand Up @@ -59,10 +61,14 @@ treeviewInput <- function(inputId,

tags$div(
class = "form-group shiny-input-container",
style = if(!is.null(width)) paste("width:", validateCssUnit(width)),
if (!is.null(label)) {
tags$label(class = "control-label", `for` = inputId, label)
},
style = if (!is.null(width)) paste("width:", validateCssUnit(width)),
tags$label(
id = paste0(inputId, "-label"),
class = "control-label",
class = if (is.null(label)) "shiny-label-null",
`for` = inputId,
label
),
tags$div(
id = inputId, class = "treeview-input",
`data-return` = return_value,
Expand Down
31 changes: 20 additions & 11 deletions R/server-treeview.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

#' Search a \code{treeviewInput}
#' @title Manipulate a tree server-side
#'
#' @description Interact with a tree contructed with [treeviewInput()] or [treecheckInput()] from the server.
#'
#' @param inputId The id of the input object.
#' @param pattern Pattern to search for.
Expand All @@ -9,9 +11,11 @@
#' @param collapse_before Collapse all nodes before revealing results.
#' @param session The session object passed to function given to shinyServer.
#'
#' @return None.
#' @return No value, side-effects only.
#' @export
#'
#' @name tree-server
#'
#' @example examples/search.R
searchTreeview <- function(inputId,
pattern,
Expand All @@ -33,20 +37,14 @@ searchTreeview <- function(inputId,
}



#' Expand or collapse a \code{treeviewInput}
#'
#' @param inputId The id of the input object.
#' @param nodeId Id of the node to expand or collapse,
#' use \code{input$<inputId>_nodes} to see the Ids.
#' If \code{NULL} expand the all tree.
#' @param levels Levels to expand.
#' @param session The session object passed to function given to shinyServer.
#'
#' @return None.
#' @export
#'
#' @name expand-collapse
#' @rdname tree-server
#'
#' @example examples/collapse-expand.R
expandTreeview <- function(inputId,
Expand All @@ -62,8 +60,9 @@ expandTreeview <- function(inputId,
session$sendInputMessage(inputId, message)
}


#' @export
#' @rdname expand-collapse
#' @rdname tree-server
collapseTreeview <- function(inputId,
nodeId = NULL,
session = shiny::getDefaultReactiveDomain()) {
Expand All @@ -74,7 +73,17 @@ collapseTreeview <- function(inputId,
}



#' @inheritParams treeviewInput
#' @export
#' @rdname tree-server
#'
#' @example examples/update.R
updateTreeview <- function(inputId,
label = NULL,
selected = NULL,
session = shiny::getDefaultReactiveDomain()) {
session$sendInputMessage(inputId, dropNulls(list(label = label, selected = list1(selected))))
}



Expand Down
5 changes: 5 additions & 0 deletions examples/collapse-expand.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@



# expand/collapse ---------------------------------------------------------


library(shiny)
library(shinytreeview)

Expand Down
2 changes: 2 additions & 0 deletions examples/search.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# searchTreeview ----------------------------------------------------------


library(shiny)
library(shinytreeview)
Expand Down
39 changes: 39 additions & 0 deletions examples/update.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@



# updateTreeview ----------------------------------------------------------


library(shiny)
library(shinytreeview)

data("cities")

ui <- fluidPage(
tags$h3("Update label & selected value"),
treeviewInput(
inputId = "tree",
label = "Choose a city:",
choices = make_tree(cities, c("continent", "country", "city")),
multiple = FALSE,
prevent_unselect = TRUE
),
verbatimTextOutput(outputId = "result"),
textInput("label", "New label:", "Choose a city:"),
radioButtons(
"selected", "Selected:",
choices = unique(c(cities$continent, cities$country, cities$city)),
inline = TRUE
)
)

server <- function(input, output, session) {
output$result <- renderPrint({
input$tree
})
observe(updateTreeview(inputId = "tree", label = input$label))
observe(updateTreeview(inputId = "tree", selected = input$selected))
}

if (interactive())
shinyApp(ui, server)
2 changes: 1 addition & 1 deletion inst/packer/treeview.js

Large diffs are not rendered by default.

96 changes: 0 additions & 96 deletions man/expand-collapse.Rd

This file was deleted.

84 changes: 0 additions & 84 deletions man/searchTreeview.Rd

This file was deleted.

Loading

0 comments on commit ec42477

Please sign in to comment.