R 当系统中发生事件时,如果某个选项卡处于活动状态,如何停止输出代码的运行?
我的代码如下:用户上传文件,按submit,运行外部程序在输出目录中创建文件,在plot选项卡和table选项卡中读取和呈现文件。如果用户在“打印”选项卡上按下“提交”,则可以正常工作;但是,如果在table选项卡处于活动状态时按下该按钮,则不会更新为使用新创建的表输出目录。我在下面添加了一些通用代码,由于工作的性质,无法提供具体的示例R 当系统中发生事件时,如果某个选项卡处于活动状态,如何停止输出代码的运行?,r,shiny,R,Shiny,我的代码如下:用户上传文件,按submit,运行外部程序在输出目录中创建文件,在plot选项卡和table选项卡中读取和呈现文件。如果用户在“打印”选项卡上按下“提交”,则可以正常工作;但是,如果在table选项卡处于活动状态时按下该按钮,则不会更新为使用新创建的表输出目录。我在下面添加了一些通用代码,由于工作的性质,无法提供具体的示例 我试图通过添加observeEvent来解决这个问题,当按下按钮时切换到绘图选项卡,但它不起作用。我猜这与按下按钮时选项卡处于活动状态有关,因此它执行outpu
我试图通过添加
observeEvent
来解决这个问题,当按下按钮时切换到绘图选项卡,但它不起作用。我猜这与按下按钮时选项卡处于活动状态有关,因此它执行output$table1中的代码。我修改了函数顺序,并将所有内容包装在observeEvent
中,并将系统调用移到输出之外,现在似乎可以工作了
shinyServer(function(input, output, session) {
output$plot1 <- renderPlot({
validate(
need(input$file1, 'Provide a file')
)
})
output$table1 <- DT::renderDataTable({
validate(
need(input$file1, 'Provide a file')
)
})
# When button pressed, create output directory, switch to plot tab, run program
observeEvent(input$submitButton, {
input$submitButton
isolate({
validate(
need(input$file1, 'Provide a file')
)
})
updateTabsetPanel(session, "output", "Graph")
dateTime <- as.numeric(as.POSIXct(Sys.time()))
output.dir <<- paste0("~/Sites/Shiny/",dateTime,"/")
dir.create(output.dir)
# External system commands which creates output files in output.dir
# Concatenate some strings for arguments, then run system(...)
# Capture output, check for error string and validate
command.output <- system(..., intern=T)[1]
output$plot1 <- renderPlot({
validate(
need(!(gdata::startsWith(command.output, "Error:")), 'Error: ...')
)
plot(readLines(paste0(output.dir,"plot.file")))
})
output$table1 <- DT::renderDataTable({
validate(
need(!(gdata::startsWith(command.output, "Error:")), 'Error: ...')
)
DT::datatable(read.table(
paste0(output.dir,list.files(output.dir,pattern="^StartsWithA|^StartsWithB")),
col.names = c("ColA","ColB","ColC","ColD"), sep="\t"),
options = list(pageLength = 10))
})
})
})
shinyServer(功能(输入、输出、会话){
输出$plot1您是否正在使用表格选项卡执行某些操作,即将其用作某项操作的输入$…`吗?是否收到任何错误消息?如何确保观察事件(…)
在检查创建文件的dir
之前执行?您是否尝试在DT::renderDataTable
中的控制台上打印一些内容?我最终将所有内容包装在observeEvent
中,首先运行代码并创建目录,然后设置输出。它现在似乎工作正常检查是否已按下input$submitButton
。即使尚未按下此按钮,在创建按钮并接收值0时,代码将运行output$plot1
。使用req()
函数应阻止此情况发生:。
shinyServer(function(input, output, session) {
# When button pressed, create output directory and switch to plot tab
observeEvent(input$submitButton, {
updateTabsetPanel(session, "output", "Graph")
dateTime <- as.numeric(as.POSIXct(Sys.time()))
output.dir <<- paste0("~/Sites/Shiny/",dateTime,"/")
dir.create(output.dir)
})
output$plot1 <- renderPlot({
# Check if button pressed and only run if pressed
input$submitButton
isolate({
validate(
need(input$file1, 'Provide a file')
)
})
# External system commands which creates output files in output.dir
# Concatenate some strings for arguments, then run system(...)
# Capture output, check for error string and validate
validate(
need(!(gdata::startsWith(output, "Error:")), 'Error: ...')
)
plot(readLines(paste0(output.dir,"plot.file")))
})
output$table1 <- DT::renderDataTable({
# Check if button pressed and only run if pressed
input$submitButton
isolate({
validate(
# Make sure files exist in the directory
need(length(list.files(output.dir,pattern="^StartsWithA|^StartsWithB")) > 0,
'Run model first to receive interactions')
)
DT::datatable(read.table(
paste0(output.dir,list.files(output.dir,pattern="^StartsWithA|^StartsWithB")),
col.names = c("ColA","ColB","ColC","ColD"), sep="\t"),
options = list(pageLength = 10))
})
})
})
shinyServer(function(input, output, session) {
output$plot1 <- renderPlot({
validate(
need(input$file1, 'Provide a file')
)
})
output$table1 <- DT::renderDataTable({
validate(
need(input$file1, 'Provide a file')
)
})
# When button pressed, create output directory, switch to plot tab, run program
observeEvent(input$submitButton, {
input$submitButton
isolate({
validate(
need(input$file1, 'Provide a file')
)
})
updateTabsetPanel(session, "output", "Graph")
dateTime <- as.numeric(as.POSIXct(Sys.time()))
output.dir <<- paste0("~/Sites/Shiny/",dateTime,"/")
dir.create(output.dir)
# External system commands which creates output files in output.dir
# Concatenate some strings for arguments, then run system(...)
# Capture output, check for error string and validate
command.output <- system(..., intern=T)[1]
output$plot1 <- renderPlot({
validate(
need(!(gdata::startsWith(command.output, "Error:")), 'Error: ...')
)
plot(readLines(paste0(output.dir,"plot.file")))
})
output$table1 <- DT::renderDataTable({
validate(
need(!(gdata::startsWith(command.output, "Error:")), 'Error: ...')
)
DT::datatable(read.table(
paste0(output.dir,list.files(output.dir,pattern="^StartsWithA|^StartsWithB")),
col.names = c("ColA","ColB","ColC","ColD"), sep="\t"),
options = list(pageLength = 10))
})
})
})