Skip to content

Commit bd7154d

Browse files
committed
support custom icons shinyTree#9
also, deal with hyphenated icons
1 parent a2d9eda commit bd7154d

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.Rproj.user
22
.Rhistory
33
.RData
4-
shinyapps/
4+
shinyapps/
5+
.DS_Store

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ importFrom(jsonlite,toJSON)
99
importFrom(methods,loadMethod)
1010
importFrom(utils,head)
1111
importFrom(utils,tail)
12+
importFrom(stringr,str_match)
13+
importFrom(stringr,str_subset)

R/update-tree.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ fixIconName <- function(icon){
3131
}
3232
}
3333

34+
library(stringr)
35+
36+
#fix icon retains backward compatibility for icon entries that are not fully specified
37+
fixIconName <- function(icon){
38+
if(is.null(icon)){
39+
NULL
40+
}else if(grepl("[/\\]",icon)){ #ie. "/images/ball.jpg"
41+
icon
42+
}else{
43+
iconGroup <- str_subset(icon,"(\\S+) \\1-") #ie "fa fa-file"
44+
if(length(iconGroup) > 0){
45+
icon
46+
}else{
47+
iconGroup <- str_match(icon,"(fa|glyphicon)-") #ie "fa-file"
48+
if(length(iconGroup) > 1 && !is.na(iconGroup[2])){
49+
paste(iconGroup[2],icon)
50+
}else{ #ie. just "file"
51+
paste0("fa fa-",icon)
52+
}
53+
}
54+
}
55+
}
56+
3457
get_flatList <- function(nestedList, flatList = NULL, parent = "#") {
3558
for (name in names(nestedList)) {
3659
additionalAttributeNames <- c("icon","type")

inst/examples/13-icons/server.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
library(shiny)
2+
library(shinyTree)
3+
4+
#' Examples of using jstree types to define node attributes
5+
#' @author Michael Bell \email{bellma@@lilly.com}
6+
shinyServer(function(input, output, session) {
7+
log <- c(paste0(Sys.time(), ": Interact with the tree to see the logs here..."))
8+
9+
treeData <- reactive({
10+
list(
11+
root1 = structure("", stselected=TRUE,sttype="root"),
12+
root2 = structure(list(
13+
SubListA = structure(list(
14+
leaf1 = structure("",sttype="file",sticon="fa fa-signal"),
15+
leaf2 = structure("",sttype="file"),
16+
leaf3 = structure("",sttype="file")),
17+
sttype="root",stopened=TRUE
18+
),
19+
SubListB = structure(list(
20+
leafA = structure("",sttype="default",sticon="glyphicon glyphicon-leaf"),
21+
leafB = structure("",sttype="default",sticon="shinyTree/icon.png")
22+
),stopened=TRUE,sttype="root")
23+
),
24+
sttype="root",stopened=TRUE
25+
)
26+
)
27+
})
28+
29+
observeEvent(input$updateTree,{
30+
updateTree(session, treeId = "tree", data = treeData())
31+
})
32+
33+
output$tree <- renderTree({
34+
treeData()
35+
})
36+
})

inst/examples/13-icons/ui.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library(shiny)
2+
library(shinyTree)
3+
4+
#' Demonstrates icons by using types and by paticular items in the tree
5+
#' @author Michael Bell \email{bellma@@lilly.com}
6+
shinyUI(
7+
pageWithSidebar(
8+
# Application title
9+
headerPanel("shinyTree with icons!"),
10+
11+
sidebarPanel(
12+
helpText(HTML("A shinyTree example for displaying various icons using types and specifying particular items in the tree
13+
<hr>Created using shinyTree<hr>"))
14+
),
15+
mainPanel(
16+
# Show a simple table.
17+
shinyTree("tree", dragAndDrop = TRUE,types= #Types is in the same format that jstree expects
18+
"{
19+
'#': { 'max_children' : 2, 'max_depth' : 4, 'valid_children' : ['root'] },
20+
'root' : { 'valid_children' : ['file'] },
21+
'default' : { 'valid_children' : ['default','file'] },
22+
'file' : { 'icon' : 'fa fa-file', 'valid_children' : [] }
23+
}"
24+
)
25+
)
26+
))

inst/www/icon.png

2.67 KB
Loading

0 commit comments

Comments
 (0)