Shinny-如何在操作按钮中为source()-ed脚本编写并行处理代码?
我的R脚本是高度可变的,因此我将其分解为一系列由控制器脚本运行的源ed R文件。如果我想运行一个脚本,我会创建一个对象并用一个条件语句包装源ed脚本。我想使用shiny的可编程小部件来控制输入,但我仍然希望能够通过RStudio自由运行代码。为了在R中运行它,我创建了一个Rlauncher脚本,您可以在其中手动设置对象参数和源控制器脚本。不幸的是,对于ShinyLancher服务器.R,隔离所有输入并运行脚本所需的包装actionButton要求代码在ShinyLancher的.GlobalEnv中运行。我已经提出了一个与原始脚本并行运行server.R的部分解决方案,但是我无法绕过在.GlobalEnv中运行控制器脚本的问题。关于如何在parent.frame中操作环境以运行控制器脚本的任何帮助都将非常棒 这是我的原始代码。如果在运行控制器脚本时将对象加载到.GlobalEnv上,一切正常,但渲染一批贴图需要几周时间Shinny-如何在操作按钮中为source()-ed脚本编写并行处理代码?,r,parallel-processing,widget,shiny,shiny-server,R,Parallel Processing,Widget,Shiny,Shiny Server,我的R脚本是高度可变的,因此我将其分解为一系列由控制器脚本运行的源ed R文件。如果我想运行一个脚本,我会创建一个对象并用一个条件语句包装源ed脚本。我想使用shiny的可编程小部件来控制输入,但我仍然希望能够通过RStudio自由运行代码。为了在R中运行它,我创建了一个Rlauncher脚本,您可以在其中手动设置对象参数和源控制器脚本。不幸的是,对于ShinyLancher服务器.R,隔离所有输入并运行脚本所需的包装actionButton要求代码在ShinyLancher的.GlobalEn
shinyServer(function(input, output, session) {
observe({
if (input$actnBut_runController==0) {return} else{
isolate({
tmp.env <- new.env()
run_script1 <- input$run_script1
run_script2 <- input$run_script2
run_script3 <- input$run_script3
assign("run_script1",run_script1,envir=tmp.env)
assign("run_script2",run_script2,envir=tmp.env)
assign("run_script3",run_script3,envir=tmp.env)
save(list=ls(all.names=TRUE,pos=tmp.env),envir=tmp.env,file="obs.RData")
rm(tmp.env)
})
print("Running sourced scripts...")
source("controller.R"),echo=TRUE)
}
})
这段代码模仿了我从其他工作示例中看到的并行编码。它将创建对象并运行控制器脚本,但我似乎仍然无法让它在parent.frame中运行
shinyServer(function(input, output, session) {
iso_obs <- function() {
Sys.sleep(1)
shiny::isolate({
tmp.env <- new.env()
run_script1 <- input$run_script1
run_script2 <- input$run_script2
run_script3 <- input$run_script3
assign("run_script1",run_script1,envir=tmp.env)
assign("run_script2",run_script2,envir=tmp.env)
assign("run_script3",run_script3,envir=tmp.env)
save(list=ls(all.names=TRUE,pos=tmp.env),envir=tmp.env,file="obs.RData")
rm(tmp.env)
})}
runController <- reactive({
if (input$actnBut_runController==0) {return} else {
iso_obs()
print("Running sourced scripts...")
source("controller.R"),echo=TRUE)
}})
此代码还创建了对象,但没有正确运行控制器脚本。我试图让两个函数为同一个输入执行,其中源代码是一个延迟函数
shinyServer(function(input, output, session) {
observe({
if (input$actnBut_runController==0) {return} else{
isolate({
tmp.env <- new.env()
run_script1 <- input$run_script1
run_script2 <- input$run_script2
run_script3 <- input$run_script3
assign("run_script1",run_script1,envir=tmp.env)
assign("run_script2",run_script2,envir=tmp.env)
assign("run_script3",run_script3,envir=tmp.env)
save(list=ls(all.names=TRUE,pos=tmp.env),envir=tmp.env,file="obs.RData")
rm(tmp.env)
})
}
})
runController <- reactive({
if (input$actnBut_runController==0) {return} else {
Sys.sleep(1) # This is to delay the R environment so .GlobalEnv can create objects
print("Running sourced scripts...")
source("controller.R"),echo=TRUE)
}
})
您可能需要将local=TRUE选项添加到源函数调用中 使用带参数的函数,而不是使用全局变量的源脚本。