我有以下代码,其工作原理如下:用户上传文件,按提交,运行外部程序,在输出目录中创建文件,在绘图选项卡和表格选项卡中读取和渲染文件。如果用户在绘图选项卡上按“提交”,则效果很好;但是,如果在表选项卡处于事件状态时按下该按钮,则它不会更新以使用该表新创建的输出目录。我在下面添加了一些通用代码,由于工作性质,无法提供具体示例。
我尝试通过添加 observeEvent
来解决此问题按下按钮时切换到绘图选项卡,但它不起作用。我猜这与按下按钮时选项卡处于事件状态有关,因此它执行 output$table1 <- DT::renderDataTable({ ... })
中的代码首先仍然引用 output.dir
的旧内容。当此选项卡处于事件状态时按下按钮时,有没有办法停止它?或者我应该以不同的方式格式化代码流程?仍在尝试弄清楚 Shiny 。
按下按钮时,无论哪个选项卡处于事件状态,我都需要按照上面指定的顺序执行操作。新目录,运行命令并输出到该目录,然后在每个选项卡中从中读取文件。我将其切换到图表,因为您会先查看该图表,然后查看交互表。每次您按下提交时,都会呈现新的更新的图表和表格。
ui.R
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Upload a file"),
actionButton("submitButton", "Submit")
),
mainPanel(
tabsetPanel(id = "output",
tabPanel("Graph",plotOutput("plot1")),
tabPanel("Interactions", DT::dataTableOutput("table1"))
)
)
)
))
服务器.R
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))
})
})
})
最佳答案
我重新设计了函数的顺序,并将所有内容包装在 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))
})
})
})
关于r - 如果在 Shiny 中发生事件时某些选项卡处于事件状态,如何停止输出代码运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38405008/