Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 闪亮模块的其他输入仅更新一次_R_Shiny_Shiny Reactivity_Shinymodules - Fatal编程技术网

R 闪亮模块的其他输入仅更新一次

R 闪亮模块的其他输入仅更新一次,r,shiny,shiny-reactivity,shinymodules,R,Shiny,Shiny Reactivity,Shinymodules,我试着将其归结为一个最小的示例,但我认为我必须提供或多或少的完整代码来说明问题 基本上,我想要一个闪亮的应用程序作为用户友好的GUI,通过processx包启动/停止(多个)系统进程(主要是BASH脚本,用于科学工作流)。所以我制作了一个闪亮的模块,它可以启动/停止并显示一个进程日志(仅从stderr+stdout输出)。脚本/命令的运行是在调用模块时决定的,而不是在模块本身中。重要的是,根据脚本运行情况,可以将其他选项传递到不同的进程,如选择输入/输出文件夹、数据库文件、设置等 问题在于,没有

我试着将其归结为一个最小的示例,但我认为我必须提供或多或少的完整代码来说明问题

基本上,我想要一个闪亮的应用程序作为用户友好的GUI,通过processx包启动/停止(多个)系统进程(主要是BASH脚本,用于科学工作流)。所以我制作了一个闪亮的模块,它可以启动/停止并显示一个进程日志(仅从stderr+stdout输出)。脚本/命令的运行是在调用模块时决定的,而不是在模块本身中。重要的是,根据脚本运行情况,可以将其他选项传递到不同的进程,如选择输入/输出文件夹、数据库文件、设置等

问题在于,没有在每次单击actionButton时更新任何其他输入的值,因此再次单击start按钮(触发EventResponsive)只会再次启动流程,而不需要新的选项/设置

我已将完整代码附于此处,并在我的shinyapps.io帐户上发布了一个示例应用程序,可在此处获得:

库(闪亮)
#使用processx包启动异步进程的模块
#Shinny必须是1.4.0.9003或更高版本才能使用Shinny模块,请从github安装

installGitHub错误在于没有将响应传递给模块。排队

processxServer(
    "process1",
    command = "echo",
    args = as.character(reactive({input[[NS("process1", "delay")]]})())
  )
在将其传递给模块之前,您需要评估您的
反应性
,因此模块仅在启动时获得默认值。我对其进行了更改,以便将未计算的
反应式
传递给模块,并且仅在您执行
启动过程
函数时进行计算。但是,这会使您在使用
..
时变得不那么灵活,因为现在
startProcess
假定传递了参数
args

library(shiny)
library("processx")

#shiny module to start asynchronous processes using processx package

processxUI <- function(id) {
  shiny::tagList(
    uiOutput(NS(id, "startStopBtn")),
    p(),
    uiOutput(NS(id, "processStatus")),
    h4("Process log"),
    verbatimTextOutput(NS(id, "processLog")),
    downloadButton(NS(id, "downloadLogfile"), label = "Export log file")
  )
}

processxServer <- function(id, ...) {
  moduleServer(id, function(input, output, session) {
    #reactive to store processx R6 class object
    process <- reactiveVal()
    
    #reactive to store logfile created on start
    logfile <- reactiveVal(tempfile())
    
    #start/stop button
    output$startStopBtn <- renderUI({
      if(isFALSE(processAlive())) {
        actionButton(
          inputId = NS(id, "startStopProcess"),
          label = "Start process"
        )
      } else if(isTRUE(processAlive())) {
        actionButton(
          inputId = NS(id, "startStopProcess"),
          label = "Kill process"
        )
      }
    })
    
    #start a new process and logfile when actionbutton is pressed
    observeEvent(input$startStopProcess, {
      #start process if not already running, otherwise kill
      startProcess <- function(...) {
        #generate new log file for each new process
        logfile(tempfile())
        #start process piping stderr+stdout to logfile
        
        # make argument list
        dots <- list(...)
        dots$args <- as.character(dots$args())
        arg_list <- c(dots, stderr = "2>&1", stdout = logfile(), supervise = TRUE)
        
        process(
          do.call(processx::process$new, arg_list)
        )
      }
      if(is.null(process()$is_alive))
        startProcess(...)
      else if(!is.null(process()$is_alive))
        if(isTRUE(process()$is_alive()))
          process()$kill_tree()
      else if(isFALSE(process()$is_alive()))
        startProcess(...)
    })
    
    #read process status every 500 ms (alive or not)
    #(only for updating status message below, otherwise use 
    # process()$is_alive() to avoid refresh interval delay)
    processAlive <- reactivePoll(
      intervalMillis = 500,
      session = session,
      checkFunc = function() {
        if(!is.null(process()$is_alive))
          process()$is_alive()
        else
          FALSE
      },
      valueFunc = function() {
        if(!is.null(process()$is_alive))
          process()$is_alive()
        else
          FALSE
      }
    )
    
    #print status message of process and exit status if finished
    output$processStatus <- renderUI({
      if(isTRUE(processAlive())) {
        p("Process is running...")
      } else if(isFALSE(processAlive()) && !is.null(process()$get_exit_status)) {
        if(process()$get_exit_status() == 0)
          p("Process has finished succesfully")
        else if(process()$get_exit_status() == -9)
          p("Process was killed")
        else if(!process()$get_exit_status() %in% c(0, -9))
          p(paste0("Process has errored (exit status: ", process()$get_exit_status(), ")"))
      }
    })
    
    #read logfile every 500 ms
    readLogfile <- reactivePoll(
      intervalMillis = 500,
      session = session,
      checkFunc = function() {
        if(file.exists(logfile()))
          file.info(logfile())[["mtime"]][1]
        else
          return('No process has run yet')
      },
      valueFunc = function() {
        if(file.exists(logfile()))
          readLines(logfile())
        else
          return('No process has run yet')
      }
    )
    
    #print process logfile
    output$processLog <- renderText({
      readLogfile()
    },
    sep = "\n")
    
    #export process logfile
    output$downloadLogfile <- downloadHandler(
      filename = function() {
        #append module id and date to logfile filename
        paste0("logfile_", id, "_", format(Sys.time(), format = "%y%m%d_%H%M%S"), ".txt")
      },
      content = function(file) {
        file.copy(from = logfile(), to = file)
      },
      contentType = "text/plain"
    )
  })
}

ui <- navbarPage(
  title = "test",
  tabPanel(
    title = "Test",
    column(
      width = 4,
      wellPanel(
        sliderInput(
          NS("process1", "delay"),
          "Sleep delay",
          min = 1,
          max = 5, 
          step = 1,
          value = 2)
      )
    ),
    column(
      width = 8,
      fluidRow(
        processxUI("process1")
      )
    )
  )
)

server <- function(input, output, session) {
  processxServer(
    "process1",
    command = "echo",
    args = reactive({input[[NS("process1", "delay")]]})
  )
}

shinyApp(ui = ui, server = server)
库(闪亮)
库(“processx”)
#使用processx包启动异步进程的模块

processxUI错误在于您没有将响应传递给模块。排队

processxServer(
    "process1",
    command = "echo",
    args = as.character(reactive({input[[NS("process1", "delay")]]})())
  )
在将其传递给模块之前,您需要评估您的
反应性
,因此模块仅在启动时获得默认值。我对其进行了更改,以便将未计算的
反应式
传递给模块,并且仅在您执行
启动过程
函数时进行计算。但是,这会使您在使用
..
时变得不那么灵活,因为现在
startProcess
假定传递了参数
args

library(shiny)
library("processx")

#shiny module to start asynchronous processes using processx package

processxUI <- function(id) {
  shiny::tagList(
    uiOutput(NS(id, "startStopBtn")),
    p(),
    uiOutput(NS(id, "processStatus")),
    h4("Process log"),
    verbatimTextOutput(NS(id, "processLog")),
    downloadButton(NS(id, "downloadLogfile"), label = "Export log file")
  )
}

processxServer <- function(id, ...) {
  moduleServer(id, function(input, output, session) {
    #reactive to store processx R6 class object
    process <- reactiveVal()
    
    #reactive to store logfile created on start
    logfile <- reactiveVal(tempfile())
    
    #start/stop button
    output$startStopBtn <- renderUI({
      if(isFALSE(processAlive())) {
        actionButton(
          inputId = NS(id, "startStopProcess"),
          label = "Start process"
        )
      } else if(isTRUE(processAlive())) {
        actionButton(
          inputId = NS(id, "startStopProcess"),
          label = "Kill process"
        )
      }
    })
    
    #start a new process and logfile when actionbutton is pressed
    observeEvent(input$startStopProcess, {
      #start process if not already running, otherwise kill
      startProcess <- function(...) {
        #generate new log file for each new process
        logfile(tempfile())
        #start process piping stderr+stdout to logfile
        
        # make argument list
        dots <- list(...)
        dots$args <- as.character(dots$args())
        arg_list <- c(dots, stderr = "2>&1", stdout = logfile(), supervise = TRUE)
        
        process(
          do.call(processx::process$new, arg_list)
        )
      }
      if(is.null(process()$is_alive))
        startProcess(...)
      else if(!is.null(process()$is_alive))
        if(isTRUE(process()$is_alive()))
          process()$kill_tree()
      else if(isFALSE(process()$is_alive()))
        startProcess(...)
    })
    
    #read process status every 500 ms (alive or not)
    #(only for updating status message below, otherwise use 
    # process()$is_alive() to avoid refresh interval delay)
    processAlive <- reactivePoll(
      intervalMillis = 500,
      session = session,
      checkFunc = function() {
        if(!is.null(process()$is_alive))
          process()$is_alive()
        else
          FALSE
      },
      valueFunc = function() {
        if(!is.null(process()$is_alive))
          process()$is_alive()
        else
          FALSE
      }
    )
    
    #print status message of process and exit status if finished
    output$processStatus <- renderUI({
      if(isTRUE(processAlive())) {
        p("Process is running...")
      } else if(isFALSE(processAlive()) && !is.null(process()$get_exit_status)) {
        if(process()$get_exit_status() == 0)
          p("Process has finished succesfully")
        else if(process()$get_exit_status() == -9)
          p("Process was killed")
        else if(!process()$get_exit_status() %in% c(0, -9))
          p(paste0("Process has errored (exit status: ", process()$get_exit_status(), ")"))
      }
    })
    
    #read logfile every 500 ms
    readLogfile <- reactivePoll(
      intervalMillis = 500,
      session = session,
      checkFunc = function() {
        if(file.exists(logfile()))
          file.info(logfile())[["mtime"]][1]
        else
          return('No process has run yet')
      },
      valueFunc = function() {
        if(file.exists(logfile()))
          readLines(logfile())
        else
          return('No process has run yet')
      }
    )
    
    #print process logfile
    output$processLog <- renderText({
      readLogfile()
    },
    sep = "\n")
    
    #export process logfile
    output$downloadLogfile <- downloadHandler(
      filename = function() {
        #append module id and date to logfile filename
        paste0("logfile_", id, "_", format(Sys.time(), format = "%y%m%d_%H%M%S"), ".txt")
      },
      content = function(file) {
        file.copy(from = logfile(), to = file)
      },
      contentType = "text/plain"
    )
  })
}

ui <- navbarPage(
  title = "test",
  tabPanel(
    title = "Test",
    column(
      width = 4,
      wellPanel(
        sliderInput(
          NS("process1", "delay"),
          "Sleep delay",
          min = 1,
          max = 5, 
          step = 1,
          value = 2)
      )
    ),
    column(
      width = 8,
      fluidRow(
        processxUI("process1")
      )
    )
  )
)

server <- function(input, output, session) {
  processxServer(
    "process1",
    command = "echo",
    args = reactive({input[[NS("process1", "delay")]]})
  )
}

shinyApp(ui = ui, server = server)
库(闪亮)
库(“processx”)
#使用processx包启动异步进程的模块

我非常感谢你!我现在明白了,这解决了问题。我知道在使用闪亮的模块时,这样做并不完全是老生常谈,但我看不到任何其他选项。我目前有8个不同的进程可以在同一个应用程序中运行,所以仅仅因为几个输入不同就必须有8倍的复制代码,这真的让我很困扰。非常感谢!我现在明白了,这解决了问题。我知道在使用闪亮的模块时,这样做并不完全是老生常谈,但我看不到任何其他选项。我目前在同一个应用程序中有8个不同的进程可以运行,因此,仅仅因为几个输入不同就必须有8倍的复制代码,这真的让我很烦恼。