Browse code

adding comments to code

pablo-rodr-bio2 authored on 20/04/2021 13:46:22
Showing 3 changed files

... ...
@@ -21,7 +21,7 @@ geneSetsServer <- function(id){
21 21
   moduleServer( id, function(input, output, session){
22 22
     geneSets <- reactive({
23 23
       if(input$genesetSourceType == "fileGeneset"){
24
-        if(is.null(input$genesetFile)) return(NULL)
24
+        if(is.null(input$genesetFile)) return(NULL) #this is in order to disable "run" btn
25 25
         genesets <- getGmt(input$genesetFile$datapath)
26 26
       } else {
27 27
         if(is.null(input$genesetVar)) return(NULL)
28 28
deleted file mode 100644
... ...
@@ -1,13 +0,0 @@
1
-name: shinyApp
2
-title: shinyApp
3
-username:
4
-account: prodriguez-bio2
5
-server: shinyapps.io
6
-hostUrl: https://api.shinyapps.io/v1
7
-appId: 3971711
8
-bundleId: 4476074
9
-url: https://prodriguez-bio2.shinyapps.io/shinyapp/
10
-when: 1618503477.40691
11
-lastSyncTime: 1618503477.40694
12
-asMultiple: FALSE
13
-asStatic: FALSE
... ...
@@ -30,26 +30,46 @@ function(input, output, session) {
30 30
   
31 31
   # ARGUMENTS
32 32
   argInp <- argumentsDataServer("argumentsInput")
33
+  
33 34
 
34 35
   #### GSVA RESULTS ####
35 36
   
37
+  ## REACTIVE VALUES
36 38
   rv <- reactiveValues(gs=NULL, dat.t=NULL, n=NULL, dd.col=NULL, p=NULL, 
37
-                       errors.gsva = NULL)
39
+                       p2=NULL, p3=NULL, errors.gsva = NULL, sample.c = NULL)
38 40
   gsva.cancel <- reactiveVal(FALSE)
39 41
   
42
+  ## GSVA RESULT
40 43
   observeEvent( input$button, {
44
+    
45
+    ## This js is in order to reset the event_data from the plotlys,
46
+    ## so every time the .user hits the 'run' button, plotlys get back to null
41 47
     runjs("Shiny.setInputValue('plotly_click-click1', null);")
42 48
     runjs("Shiny.setInputValue('plotly_click-click2', null);")
49
+    
50
+    ## here we reset all the reactiveValues to NULL
43 51
     rv$gs <- NULL
44 52
     rv$dat.t <- NULL
45 53
     rv$p <- NULL
46 54
     rv$p2 <- NULL
47 55
     rv$p3 <- NULL
48
-    rv$errors.gsva = NULL
56
+    rv$sample.c <- NULL
57
+    rv$errors.gsva <- NULL
58
+    
59
+    ## this is a flag for the future. Futures cannot be canceled or
60
+    ## terminated in a strict way, so when they get interrupted they
61
+    ## throw an error that is not related to gsva(). When future is 
62
+    ## interrupted, the flag goes TRUE in order to make the errors
63
+    ## message print NULL
49 64
     gsva.cancel(FALSE)
65
+    
50 66
     modalGSVAUI("modal.text")
51
-    # future() cannot take reactive values, so we must isolate() them
67
+    
68
+    ## future() cannot take reactive values, so we must isolate() them
52 69
     future({
70
+      ## sink() will redirect all console cats and prints to a
71
+      ## text file that the main session will be reading in order
72
+      ## to print the progress bar from bplaply()
53 73
       sink(rout)
54 74
       result <- gsva(isolate(matrix()),
55 75
                      isolate(genesets()), 
... ...
@@ -64,20 +84,35 @@ function(input, output, session) {
64 84
                      ssgsea.norm=isolate(argInp$ssgseaNorm()),
65 85
                      verbose=TRUE)
66 86
       sink()
87
+      ## when gsva() ends, we reset the console text file to empty
67 88
       write("", file=rout)
68 89
       return(result)
69 90
     }, seed = TRUE) %...>%
70 91
       (function(result){
92
+        ## the future's result will be the gsva() result, and we save it
93
+        ## and transform it in reactiveValues(). In order to make the future
94
+        ## not block the app at an inner-session level, we save the results in
95
+        ## reactiveValues() and then at the end of the observeEvent() we return NULL
96
+        ## in order to make the plots.
97
+        ## https://github.com/rstudio/promises/issues/23#issuecomment-386687705
71 98
         rv$gs <- result
72 99
         rv$dat.t <- melt(as.data.table(rv$gs, keep.rownames = "gene.sets"),
73 100
                          variable.name = "Sample", id.vars="gene.sets")
74 101
         rv$n <- length(levels(rv$dat.t$Sample))
75 102
         rv$dd.col <- hcl(h = seq(15, 375, length=rv$n), l = 65, c = 100)[1:rv$n]
76 103
         names(rv$dd.col)  <- levels(rv$dat.t$Sample)
104
+        
105
+        ## finally, we leave the console.text file empty again and
106
+        ## remove the modal
77 107
         write("", file=rout)
78 108
         removeModal()
79 109
       }) %...!%
80 110
       (function(error){
111
+        ## there can be two ways to get an error here: 
112
+        ## 1. gsva() fails, which is an ok error and should be returnet to user
113
+        ## 2. User interrupts the future, which shouldn't be printed, that's
114
+        ## why I use a flag to identify if error comes from pressing "Cancel" btn
115
+        ## on the modal
81 116
         removeModal()
82 117
         write("", file=rout)
83 118
         if(gsva.cancel()){