@@ -143,7 +143,7 @@ ShinyDummyCheck <- function(directory = ".", ui = "ui.R", server = "server.R"){
143
143
# ' @param showInternalFunctions a boolean that specifies whether to show the functions internal to each node. These are shown
144
144
# ' on mouse_over in the hierarchy chart. FALSE by default.
145
145
# ' @param showCommentedOutChunks a boolean that specifies whether to show the chunks that have been commented out. FALSE by default.
146
- # '
146
+ # ' @param output a character string that specifies what kind of plot should be outputted: either "network" or "sankey" are accepted.
147
147
# '
148
148
# ' @return
149
149
# ' It returns a very very nice network chart with BASICALLY three-ish ROWS of nodes.
@@ -163,10 +163,11 @@ ShinyDummyCheck <- function(directory = ".", ui = "ui.R", server = "server.R"){
163
163
# ' @importFrom tidyr unnest separate
164
164
# ' @importFrom stats setNames runif
165
165
# ' @importFrom visNetwork visNetwork visEdges visLegend visHierarchicalLayout visOptions
166
+ # ' @importFrom networkD3 sankeyNetwork
166
167
# ' @export
167
168
ShinyHierarchy <- function (directory = getwd(),ui = " ui.R" ,server = " server.R" ,
168
169
offsetReactives = T ,showInternalFunctions = F ,
169
- showCommentedOutChunks = F ){
170
+ showCommentedOutChunks = F , output = " network " ){
170
171
171
172
# # Get input again
172
173
a <- read_file(paste(directory ," /" ,server ,sep = " " ))
@@ -335,39 +336,78 @@ ShinyHierarchy <- function(directory=getwd(),ui="ui.R",server="server.R",
335
336
edges <- data.frame (to = nodes $ id [match(x = BofDF $ ChunkName ,table = nodes $ Thingie )],
336
337
from = nodes $ id [match(x = BofDF $ Input ,table = nodes $ Thingie )])
337
338
338
- visNetwork(nodes ,edges ) %> % visEdges(arrows = ' to' ) %> %
339
+ # # Now output network or sankey
340
+ if (output == " network" ){
341
+ visNetwork(nodes ,edges ) %> % visEdges(arrows = ' to' ) %> %
339
342
visLegend() %> % visHierarchicalLayout() %> %
340
343
visOptions(highlightNearest = list (enabled = T , degree = 1 , hover = T ))
344
+ } else if (output == " sankey" ){
345
+ # # make links unique
346
+ edges <- edges %> %
347
+ group_by(from ,to ) %> %
348
+ summarize(value = n())
349
+ # # zero indexing
350
+ edges $ from <- edges $ from - 1
351
+ edges $ to <- edges $ to - 1
352
+
353
+ sankeyNetwork(Links = edges ,Nodes = nodes ,Source = ' from' ,NodeID = ' Thingie' ,
354
+ Target = ' to' , Value = ' value' ,NodeGroup = ' group' ,fontSize = 15 )
341
355
356
+ } else {
357
+ stop(" Only 'network' or 'sankey' are accepted as outputs" )
358
+ }
342
359
}
343
360
344
361
345
- ProcessReactives <- function (directory = getwd(),ui = " ui.R" ,server = " server.R" ,
346
- showCommentedOutChunks = F ){
347
-
348
- # # Get input again
349
- a <- read_file(paste(directory ," /" ,server ,sep = " " ))
350
- b <- gsub(" \r ?\n " ," " ,a )
351
- # # Identify code chunks, basically each little shiny minifunction
352
- Chunks <- str_extract_all(b , " [a-zA-Z0-9\\ ._]+ *\\ <\\ - *[a-zA-Z0-9\\ ._]+?\\ (\\ {.+?\\ }\\ )" ,
353
- simplify = F ) %> % . [[1 ]]
354
- if (length(Chunks )== 0 ) stop(" Hrm, I can't detect any chunks. I expect assignments to use '<-'... so if
355
- you're using '=' or '->' assignments or 'source'ing stuff in, then that would be why." )
356
-
357
- # # Identify only the reactive chunks
358
- Chunks <- Chunks [grep(" reactive\\ (" ,Chunks )]
359
-
360
- # # get only the guts
361
- Chunks2 <- str_extract(string = Chunks ," (?<=\\ (\\ {).+(?=\\ }\\ ))" )
362
-
363
- # # And make stuff evaluatable by replacing multiple space w/ semicolon, however, except the first one.
364
- Chunks3 <- gsub(" +" ," ; " ,str_trim(Chunks2 ),perl = T )
365
-
366
- # # But deal w/ pipes
367
- Chunks4 <- gsub(" \\ %;" ," \\ %" ,Chunks3 )
368
-
369
- for (i in 1 : length(Chunks4 )){
370
- # eval(parse(text = Chunks4[i]))
371
- eval(parse(text = str_split(Chunks4 [i ]," ;" )[[1 ]]))
372
- }
373
- }
362
+ # ' #' ProcessReactives
363
+ # ' #'
364
+ # ' #' This function runs all the guts from reactive blocks in console (for local testing/debugging).
365
+ # ' #'
366
+ # ' #' For now, it only works where the server and ui files are seperate (ie, it doesn't work for `app.R` yet)
367
+ # ' #'
368
+ # ' #' @param directory the directory or website containing the files for the Shiny App. Defaults to current working directory
369
+ # ' #' @param ui a character vector size 1 containing the name of the UI files. defaults to "ui.R"
370
+ # ' #' @param server a character vector size 1 containing the names of the SERVER file. defaults to "server.R"
371
+ # ' #' @param inputs a 1 row dataframe containing values for all the inputs that would be required by the reactive scripts (so as to simulate a live version of the Shiny app)
372
+ # ' #'
373
+ # ' #' @return
374
+ # ' #' Returns nothing, but loads all items into memory
375
+ # '
376
+ # ' #' @examples
377
+ # ' #' ProcessReactives(directory = system.file("example2", package = "ShinyTester"))
378
+ # ' #'
379
+ # ' #' @details You can test with your own app, go to your shiny app, make that your
380
+ # ' #' working directory, and then type `ProcessReactives()`
381
+ # ' #' @importFrom stringr str_extract_all str_extract str_trim str_split
382
+ # ' #' @importFrom readr read_file
383
+ # ' #' @export
384
+ # '
385
+ # ' ProcessReactives <- function(directory=getwd(),ui="ui.R",server="server.R",
386
+ # ' inputs=inputs){
387
+ # '
388
+ # ' ## Get input again
389
+ # ' a <- read_file(paste(directory,"/",server,sep=""))
390
+ # ' b <- gsub("\r?\n","",a)
391
+ # ' ## Identify code chunks, basically each little shiny minifunction
392
+ # ' Chunks <- str_extract_all(b, "[a-zA-Z0-9\\._]+ *\\<\\- *[a-zA-Z0-9\\._]+?\\(\\{.+?\\}\\)",
393
+ # ' simplify = F) %>% .[[1]]
394
+ # ' if (length(Chunks)==0) stop("Hrm, I can't detect any chunks. I expect assignments to use '<-'... so if
395
+ # ' you're using '=' or '->' assignments or 'source'ing stuff in, then that would be why.")
396
+ # '
397
+ # ' ## Identify only the reactive chunks
398
+ # ' Chunks <- Chunks[grep("reactive\\(",Chunks)]
399
+ # '
400
+ # ' ## get only the guts
401
+ # ' Chunks2 <- str_extract(string = Chunks,"(?<=\\(\\{).+(?=\\}\\))")
402
+ # '
403
+ # ' ## And make stuff evaluatable by replacing multiple space w/ semicolon, however, except the first one.
404
+ # ' Chunks3 <- gsub(" +","; ",str_trim(Chunks2),perl=T)
405
+ # '
406
+ # ' ## But deal w/ pipes
407
+ # ' Chunks4 <- gsub("\\%;","\\%",Chunks3)
408
+ # '
409
+ # ' for(i in 1:length(Chunks4)){
410
+ # ' # eval(parse(text = Chunks4[i]))
411
+ # ' eval(parse(text = str_split(Chunks4[i],";")[[1]]))
412
+ # ' }
413
+ # ' }
0 commit comments