显示消息,直到加载所有包

时间:2017-03-01 06:34:35

标签: r shiny shiny-server

我使用闪亮的服务器在我的服务器上部署了一个闪亮的应用程序。示例示例如下所示:

ui.R

#library(shinyjs)
library(shiny)

#filenames <- list.files(path = "data",pattern="\\.csv$")
#names(filenames) <- gsub(pattern = "\\.csv$", "", filenames)
shinyUI(fluidPage(
  titlePanel(
    headerPanel( 
      h3("Testing....",
         align="center", style="bold")
      )
    ),
  br(),
  br(),
  #useShinyjs(),  ## initialize shinyjs to reset input files.
  sidebarLayout(
    sidebarPanel(
      h5("Upload Data Files",style="bold"),
      fileInput("files", 
                "Choose CSV/txt processed files or raw files",
                multiple = "TRUE",
                accept=c('text/csv',
'text/comma-separated-values,
text/plain', '.csv','.cel','.TXT','.txt')),
#selectInput('dataset',"Choose platform annotation file", c("Please select a file" ='',filenames)),
fluidRow(
  column(5,
         radioButtons("radio", label = h5("Data uploaded"),
                      choices = list("Affymetrix" = 1, "Codelink" = 2,
                                     "Illumina" = 3),selected = NULL)
         )
  ),
fluidRow(
  column(10,
         h5("Differential Expression Call", style = "bold"),
         checkboxInput("checkbox",
                       label = "Differential Expression", value = FALSE))),
br(),
uiOutput('AnnotationFile')),
mainPanel(                      
  tabsetPanel(id = "MamgedTabs",
              tabPanel("Source-data", dataTableOutput("sourced")),
              tabPanel("Annotation-data",dataTableOutput("annotation"))
              )
  )
)
)
)

server.R

#library(gcrma)
#library(hgu133a.db)
#library(hgu133acdf)
#library(hgu133plus2.db)
#library(hgu133plus2cdf)
#library(hgu133a2frmavecs)
#library(hgu133b.db)
#library(hgu133bcdf)
#library(hgu219.db)
#library(hgu219cdf)
#library(hgu95a.db)
#library(hgu95acdf)
#library(org.Hs.eg.db)
#library(hgu133a2.db)
#library(hgu133a2cdf)
#library(hgu95av2.db)
#library(hgu95av2cdf)
#library(hgu133plus2cdf)
#library(affyPLM)
##library(makecdfenv)
#library(parallel)
#library(base)
#library(S4Vectors)
#library(IRanges)
#library(stats4)
#library(BiocInstaller)
#library(Biobase)
#library(BiocGenerics)
#library(BiocParallel)
#library(biomaRt)
#library(Biostrings)
#library(preprocessCore)
#library(affyio)
#library(zlibbioc)
#library(graphics)
#library(grDevices)
#library(methods)
#library(genefilter)
#library(stats)
#library(AnnotationDbi)
#library(utils)


## Defining the size of file to be accepted. Here it can accept any size.
options(shiny.maxRequestSize= -1) 

shinyServer(function(input, output,session) {
  filenames <- list.files(path = "data", pattern="\\.txt$")
  names(filenames) <- gsub(pattern = "\\.txt$", "", filenames)


  ## Dropdown box for chosing and loading annotation file
  output$AnnotationFile = renderUI({
    if(!input$checkbox | (input$checkbox == T && input$radio != 2)){
      wellPanel(
        h5("Upload Annotation File"),
        selectInput('dataset', "Choose platform annotation file",
                    c("Please select a file" = '', filenames), multiple = TRUE))
    }
  })
})

正如您在server.R中看到的,我需要加载许多软件包,这需要一些时间。以下代码

output$AnnotationFile = renderUI({
    if(!input$checkbox | (input$checkbox == T && input$radio != 2)){
      wellPanel(
        h5("Upload Annotation File"),
        selectInput('dataset', "Choose platform annotation file",
                    c("Please select a file" = '', filenames), multiple = TRUE))
    }
  })

是显示隐藏的东西,需要一些时间才能显示,因为加载包所花费的时间,但有时它根本不显示。

1)我的应用程序在本地工作正常,但在服务器上部署时会显示此行为。有什么原因吗?

2)如何在主面板上显示应用程序准备好的消息,直到它加载所有软件包。

编辑:我可以在shinyServer()中显示消息,但这是针对每个会话(将一次又一次地加载所有包),但是我想显示并加载所有包应用程序的开始。换句话说,就是说如何在shinyServer()

之外显示进度消息

2 个答案:

答案 0 :(得分:1)

也许你想要一个进度指示器或模态对话框?

更新2

以下是详细使用进度条来读取30 R包的文本文件,每行一个:

# server.R
options(shiny.maxRequestSize = -1)
shinyServer(function(input, output, session) {
  withProgress(message = "Please wait", value = 0, expr = {
    for (i in 1:30) {
      source(textConnection(readLines("packages.txt", warn = FALSE)[i]))
      incProgress(amount = 1/30, detail = paste0("Loading package ", i, "/30"))
      Sys.sleep(time = 0.1)
    }
  })

  filenames <- list.files(path = "data", pattern = "\\.txt$")
  names(filenames) <- gsub(pattern = "\\.txt$", "", filenames)

  output$AnnotationFile <- renderUI({
    if (!input$checkbox | (input$checkbox == T && input$radio != 2)) {
      wellPanel(
        h5("Upload Annotation File"),
        selectInput('dataset', "Choose platform annotation file", c("Please select a file" = '', filenames), multiple = TRUE)
      )
    }
  })
})

答案 1 :(得分:1)

如果您希望应用程序等到所有软件包都加载完毕,您只需要将所有软件包放在ui.R中,原因很简单,因为ui.Rserver.R之前执行1}}。因此,您可以将所有包放在ui.R的顶部。当应用程序启动时,它将加载所有包,然后显示您在ui.R中编码的接口,例如

#library(gcrma)
#library(hgu133a.db)
#library(hgu133acdf)

shinyUI(fluidPage(
# your code...
)
)

当您在server.R中使用包时,它将显示应用程序(ui.R部分),但在内部它将加载所有包,这将花费一些时间,哪个用户可能不是知道,最终可能会导致混淆应用程序的工作。为了避免ui.R是包装的好选择。