避免在R中的被动表达式中执行耗时的步骤

避免在R中的被动表达式中执行耗时的步骤,r,shiny,shiny-server,shiny-reactivity,R,Shiny,Shiny Server,Shiny Reactivity,在我闪亮的应用程序中,用户可以上传一个文件,该文件存储为reactivedataframe。在下面显示的反应式表达式中,我调用了一个外部耗时函数(称为performDigestion),它需要几秒钟才能完成 fastafile_data <- reactive(){ inFile_fastafile <- input$fastaFile req(inFile_fastafile) ext <- tools::file_ext(inFile_fastafi

在我闪亮的应用程序中,用户可以上传一个文件,该文件存储为
reactive
dataframe。在下面显示的反应式表达式中,我调用了一个外部耗时函数(称为performDigestion),它需要几秒钟才能完成

fastafile_data <- reactive(){
    inFile_fastafile <- input$fastaFile
    req(inFile_fastafile)
    ext <- tools::file_ext(inFile_fastafile$datapath)
    validate(need(ext == "fasta", "Please upload a fasta file"))
    dt.seq <- readAAStringSet(inFile_fastafile$datapath)
    tbl <- performDigestion(dt.seq) ##the time-consuming step
    return(tbl)
  }

您可以定义一个新的reactive,它将
fastafile_数据
和其他UI输入作为输入并对其进行过滤。然后你使用这个新的反应来呈现你的表格我更新了我的代码。正如建议的那样,我尝试使用第二个反应式
fastafile\u data\u new
,它将
fastafile\u data
作为输入,但每次触发UI中的更改时(无论是从
sliderInput
还是从
numericInput
),第二个反应式表达式都会与第一个反应式表达式一起重新执行(具有耗时功能)。您能提供MRE吗?一般来说,在这种情况下不应触发第一个反应。try
dt
output$dt_fastafile <- DT::renderDataTable({
withProgress(message = 'Computation in progress, this step might take a while. Please wait...', {
  incProgress(1/1)
  fastafile_data()
  })
  }, options = list(scrollX = TRUE, dom = 'lfrtip', pageLength = 10, lengthMenu = c(10, 25, 50, 100)), rownames = FALSE)
fastafile_data_new <- reactive({
    dt <- fastafile_data()
    ##### the condition I'd like to apply
    dt$identifiable <- ifelse(dt$length >= min_peptide_length$choice & dt$length <= max_peptide_length$choice & dt$`mass [Da]` < max_peptide_mass$choice, 1, 0)
    return(dt)
  })