-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiny_mod_file_viz.R
127 lines (114 loc) · 3.19 KB
/
shiny_mod_file_viz.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
mod_file_viz_ui <- function(id) {
ns <- NS(id)
tagList(
uiOutput(outputId = ns("file_viz"))
)
}
mod_file_viz_server <- function(id, repos, file_tree, filenames) {
moduleServer(id, function(input, output, session) {
print("mod_file_viz_server running")
ns <- session$ns
id_int <- as.integer(gsub("file_viz_", "", id))
file_tree_id <- paste0("file_tree_", id_int)
tree <- reactive({
file_tree[[file_tree_id]]
})
files <- reactive({
files <- grep(file_tree_id, filenames(), value = TRUE)
as.integer(gsub(paste0(file_tree_id, "-"), "", files))
})
observe({
print(paste(repos[[file_tree_id]], ":", paste(tree()$filename[files()], collapse = ", ")))
})
output$file_viz <- renderUI({
lapply(
rev(files()),
function(i) {
card(
card_header(
tags$span(
"File: ",
tags$a(
tree()$filename[i],
href = URLencode(paste0(tree()$ServerUrlString[i],"?download=true")),
target = "_blank"
)
)#, container = htmltools::h3, padding = "0px"
),
card_body(
getFileUI(tree()[i, ], ns)
)
)
}
)
})
})
}
# UI utilities
getFileUI <- function(info, ns) {
# Download the file from the URL
file_extension <- tolower(tools::file_ext(info$ServerUrlString))
# print(file_extension)
fileURL <- URLencode(info$ServerUrlString)
if (file_extension == "csv") {
# data <- read.table(fileURL, sep = ",", header = TRUE)
renderTable({
fileToDisplay <- read.table(fileURL, sep = ",", header = TRUE)
})
} else if (file_extension %in% c("png", "jpg")) {
renderUI({
HTML(c('<div><img src="', fileURL, '" width="85%" height="85%"></div>'))
})
} else if (file_extension == "bib") {
renderUI({
fileToDisplay <- getURL(fileURL)
# html_text <- gsub("\r\n", "</br>", fileToDisplay)
# HTML(html_text)
aceEditor(
outputId = ns("code_bib"),
value = fileToDisplay,
mode = "yaml",
theme = "clouds_midnight",
fontSize = 14,
height = "80vh",
readOnly = TRUE
)
})
} else if (file_extension %in% c("r", "R", "rmd")) {
renderUI({
fileToDisplay <- getURL(fileURL)
aceEditor(
outputId = ns("code"), ,
value = fileToDisplay,
mode = "r",
theme = "chrome",
fontSize = 14,
height = "80vh",
readOnly = TRUE
)
})
} else if (file_extension == "html") {
renderUI({
fileToDisplay <- getURL(fileURL)
HTML(fileToDisplay)
# print(fileToDisplay)
# html_text <- gsub("\r\n", "</br>", fileToDisplay)
# HTML(html_text)
})
} else if (file_extension %in% c("txt", "dat")) {
renderUI({
fileToDisplay <- getURL(fileURL)
aceEditor(
outputId = ns("dat"),
value = fileToDisplay,
mode = "text",
theme = "chrome",
fontSize = 14,
height = "80vh",
readOnly = TRUE
)
})
} else {
# shinyjs::alert("Invalid file type or file format.")
}
}