Skip to content

Commit 861012d

Browse files
committed
jstreeExample treeNavigator
1 parent 349f17e commit 861012d

File tree

6 files changed

+173
-2
lines changed

6 files changed

+173
-2
lines changed

R/jstreeExamples.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' @title jsTreeR examples
22
#' @description List of Shiny examples.
33
#'
4-
#' @return No return value, just prints a message listing the example names.
4+
#' @return No returned value, just prints a message listing the example names.
55
#'
66
#' @export
77
#'
@@ -50,6 +50,9 @@ jstreeExamples <- function(){
5050
#' if(interactive()){
5151
#' jstreeExample("gridFiltering")
5252
#' }
53+
#' if(interactive()){
54+
#' jstreeExample("treeNavigator")
55+
#' }
5356
jstreeExample <- function(example, display.mode = "showcase", ...) {
5457
Folder <- system.file("examples", package = "jsTreeR")
5558
Examples <- list.dirs(Folder, full.names = FALSE, recursive = FALSE)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
library(shiny)
2+
library(jsTreeR)
3+
library(shinyAce)
4+
5+
rootFolder <- R.home()
6+
7+
# binary files extensions (we don't want to open these files)
8+
binaryExtensions <- c(
9+
"3dm", "3ds", "3g2", "3gp", "7z", "a", "aac", "adp",
10+
"ai", "aif", "aiff", "alz", "ape", "apk", "appimage", "ar",
11+
"arj", "asf", "au", "avi", "bak", "baml", "bh", "bin",
12+
"bk", "bmp", "btif", "bz2", "bzip2", "cab", "caf", "cgm",
13+
"class", "cmx", "cpio", "cr2", "cur", "dat", "dcm", "deb",
14+
"dex", "djvu", "dll", "dmg", "dng", "doc", "docm", "docx",
15+
"dot", "dotm", "dra", "DS_Store", "dsk", "dts", "dtshd", "dvb",
16+
"dwg", "dxf", "ecelp4800", "ecelp7470", "ecelp9600", "egg", "eol", "eot",
17+
"epub", "exe", "f4v", "fbs", "fh", "fla", "flac", "flatpak",
18+
"fli", "flv", "fpx", "fst", "fvt", "g3", "gh", "gif",
19+
"graffle", "gz", "gzip", "h261", "h263", "h264", "icns", "ico",
20+
"ief", "img", "ipa", "iso", "jar", "jpeg", "jpg", "jpgv",
21+
"jpm", "jxr", "key", "ktx", "lha", "lib", "lvp", "lz",
22+
"lzh", "lzma", "lzo", "m3u", "m4a", "m4v", "mar", "mdi",
23+
"mht", "mid", "midi", "mj2", "mka", "mkv", "mmr", "mng",
24+
"mobi", "mov", "movie", "mp3", "mp4", "mp4a", "mpeg", "mpg",
25+
"mpga", "mxu", "nef", "npx", "numbers", "nupkg", "o", "oga",
26+
"ogg", "ogv", "otf", "pages", "pbm", "pcx", "pdb", "pdf",
27+
"pea", "pgm", "pic", "png", "pnm", "pot", "potm", "potx",
28+
"ppa", "ppam", "ppm", "pps", "ppsm", "ppsx", "ppt", "pptm",
29+
"pptx", "psd", "pya", "pyc", "pyo", "pyv", "qt", "rar",
30+
"ras", "raw", "rds", "resources", "rgb", "rip", "rlc", "rmf",
31+
"rmvb", "rpm", "rtf", "rz", "s3m", "s7z", "scpt", "sgi", "shar",
32+
"snap", "sil", "sketch", "slk", "smv", "snk", "so", "stl",
33+
"suo", "sub", "swf", "tar", "tbz", "tbz2", "tga", "tgz",
34+
"thmx", "tif", "tiff", "tlz", "ttc", "ttf", "txz", "udf",
35+
"uvh", "uvi", "uvm", "uvp", "uvs", "uvu", "viv", "vob",
36+
"war", "wav", "wax", "wbmp", "wdp", "weba", "webm", "webp",
37+
"whl", "wim", "wm", "wma", "wmv", "wmx", "woff", "woff2",
38+
"wrm", "wvx", "xbm", "xif", "xla", "xlam", "xls", "xlsb",
39+
"xlsm", "xlsx", "xlt", "xltm", "xltx", "xm", "xmind", "xpi",
40+
"xpm", "xwd", "xz", "z", "zip", "zipx"
41+
)
42+
43+
# ace editor mode
44+
aceMode <- function(filepath){
45+
ext <- tolower(tools::file_ext(filepath))
46+
if(ext %in% binaryExtensions){
47+
return(NULL)
48+
}
49+
switch(ext,
50+
c = "c_cpp",
51+
cpp = "c_cpp",
52+
"c++" = "c_cpp",
53+
dockerfile = "dockerfile",
54+
frag = "glsl",
55+
h = "c_cpp",
56+
hpp = "c_cpp",
57+
css = "css",
58+
f = "fortran",
59+
f90 = "fortran",
60+
gitignore = "gitignore",
61+
hs = "haskell",
62+
html = "html",
63+
java = "java",
64+
js = "javascript",
65+
jsx = "jsx",
66+
json = "json",
67+
jl = "julia",
68+
tex = "latex",
69+
md = "markdown",
70+
map = "json",
71+
markdown = "markdown",
72+
rmd = "markdown",
73+
mysql = "mysql",
74+
ml = "ocaml",
75+
perl = "perl",
76+
pl = "perl",
77+
php = "php",
78+
py = "python",
79+
r = "r",
80+
rd = "rdoc",
81+
rhtml = "rhtml",
82+
rnw = "latex",
83+
ru = "ruby",
84+
rs = "rust",
85+
scala = "scala",
86+
scss = "scss",
87+
sh = "sh",
88+
sql = "sql",
89+
svg = "svg",
90+
txt = "text",
91+
ts = "typescript",
92+
vb = "vbscript",
93+
xml = "xml",
94+
yaml = "yaml",
95+
yml = "yaml",
96+
"plain_text"
97+
)
98+
}
99+
100+
# custom context menu
101+
menuItems <- JS("function customMenu(node) {
102+
$treeNavigator = $('div[id^=\"explorer-\"]');
103+
var tree = $treeNavigator.jstree(true);
104+
var items = {
105+
'view' : {
106+
'label': 'View (files only)',
107+
'action': function (obj) {
108+
if(tree.is_leaf(node)) {
109+
var path = tree.get_path(node, '/');
110+
Shiny.setInputValue('path', path, {priority: 'event'});
111+
}
112+
},
113+
'icon': 'fas fa-eye'
114+
}
115+
};
116+
return items;
117+
}")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
shinyServer(function(input, output, session){
2+
3+
Paths <- treeNavigatorServer(
4+
"explorer", rootFolder = rootFolder,
5+
search = list( # (search in the visited folders only)
6+
show_only_matches = TRUE,
7+
case_sensitive = TRUE,
8+
search_leaves_only = TRUE
9+
),
10+
contextMenu = list(items = menuItems, select_node = FALSE)
11+
)
12+
13+
observeEvent(input[["path"]], {
14+
mode <- aceMode(input[["path"]])
15+
if(is.null(mode)){
16+
showModal(modalDialog(
17+
"Cannot open this file!",
18+
title = "Binary file",
19+
footer = NULL,
20+
easyClose = TRUE
21+
))
22+
}else{
23+
contents <- paste0(suppressWarnings(
24+
readLines(input[["path"]])
25+
), collapse = "\n")
26+
showModal(modalDialog(
27+
aceEditor(
28+
"aceEditor",
29+
value = contents,
30+
mode = mode,
31+
theme = "cobalt",
32+
tabSize = 2,
33+
height = "60vh"
34+
),
35+
size = "l"
36+
))
37+
}
38+
})
39+
40+
})

inst/examples/treeNavigator/ui.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
shinyUI(fluidPage(
2+
h1("Right-click on a file to view it."),
3+
fluidRow(
4+
column(
5+
width = 12, treeNavigatorUI("explorer")
6+
)
7+
)
8+
))

man/jstreeExample.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/jstreeExamples.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)