gpt4 book ai didi

R Shiny : Update tabsetpanel before finishing all the observeEvent code

转载 作者:行者123 更新时间:2023-12-04 12:35:37 26 4
gpt4 key购买 nike

我想立即更新 tabsetpanel,而不是等到完成下载功能。在这里你可以找到一个简单的代码它有一个按钮,当它按下时,它模拟下载,并更新一个 tabsetpanel。我想在完成下载之前更新面板。

谢谢!

server <- function(input, output,session) {

observeEvent(input$goPlot,{

updateTabsetPanel(session, "inTabset",
selected = 'Summary'
)

output$plot <- renderPlot({
input$goPlot # Re-run when button is clicked

# Create 0-row data frame which will be used to store data
dat <- data.frame(x = numeric(0), y = numeric(0))

withProgress(message = 'Making plot', value = 0, {
# Number of times we'll go through the loop
n <- 10

for (i in 1:n) {
# Each time through the loop, add another row of data. This is
# a stand-in for a long-running computation.
dat <- rbind(dat, data.frame(x = rnorm(1), y = rnorm(1)))

# Increment the progress bar, and update the detail text.
incProgress(1/n, detail = paste("Doing part", i))

# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(1)
}
})

plot(dat$x, dat$y)
})



})
}

ui <- shinyUI(fluidPage(
actionButton('goPlot', 'Go plot'),
tabsetPanel(id = "inTabset",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary")

)
)

)

shinyApp(ui = ui, server = server)

最佳答案

Shiny 仅在更新所有无效的观察或 react 语句后更新 UI。因此,当您想要这样的工作流程时,您必须构建 react 链。我通过在一个单独的 react 语句中提取数据准备来解决这个问题(这不是真正必要的,但总是一个好主意)然后我将绘图移动到摘要选项卡。我想切换标签的原因是为了看情节。如果这不正确,请纠正我。但这会推迟计算,直到显示选项卡。现在为了防止在单击 goPlot 按钮之前开始计算,我刚刚添加了行

req(input$goPlot) 

到 react 语句的开头。

server <- function(input, output,session) {
observeEvent(input$goPlot,{

updateTabsetPanel(session, "inTabset",
selected = 'Summary'
)
generate_plot <- reactive({

req(input$goPlot)

# Create 0-row data frame which will be used to store data
dat <- data.frame(x = numeric(0), y = numeric(0))

withProgress(message = 'Making plot', value = 0, {
# Number of times we'll go through the loop
n <- 10

for (i in 1:n) {
# Each time through the loop, add another row of data. This is
# a stand-in for a long-running computation.
dat <- rbind(dat, data.frame(x = rnorm(1), y = rnorm(1)))

# Increment the progress bar, and update the detail text.
incProgress(1/n, detail = paste("Doing part", i))

# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(1)
}
})

plot(dat$x, dat$y)

})
output$plot <- renderPlot({
generate_plot()
})



})
}

ui <- shinyUI(fluidPage(
actionButton('goPlot', 'Go plot'),
tabsetPanel(id = "inTabset",
tabPanel("Plot"),
tabPanel("Summary", plotOutput("plot"))

)
)

)

shinyApp(ui = ui, server = server)

希望这对您有所帮助!

关于R Shiny : Update tabsetpanel before finishing all the observeEvent code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54009234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com