2017-06-25 66 views
1

我在试着理解“进度指示器”是如何运作的,所以我创建了一个需要大约7秒(1.8GHz)运行的循环(虚构)。 我想在用户点击一个按钮后显示一个进度条Go!使用“withProgress”in shiny

这是代码:

ui <- fluidPage(
    headerPanel("Progress indicators"), 
    sidebarPanel(
    numericInput("n", "N:", min = 0, max = 100, value = 50000), 
    br(), 
    actionButton("goButton", "Go!") 

), 
    mainPanel(
    verbatimTextOutput("nText") 
) 
) 

server <- function(input, output) { 

    fictional <- reactive({ 
    n=input$n 
    p = rep(0,n) 
    for(j in 1:n){ 
     data1=rnorm(1000,1,2) 
     data2=runif(1000,1,2) 
     p[j] = min(data1,data2) 
    } 
    pw1 = mean(p) 
    return(pw1) 
}) 
    ntext <- eventReactive(input$goButton, { fictional()}) 

    output$nText <- eventReactive(input$goButton, { 

    withProgress(message = 'Progress indicators', { 
    ntext() 
    }) 
    }) 
} 
shinyApp(ui, server) 

我试图用withProgress,但我不知道如何使用它来包装,因为当我在去点击代码!它显示进度条,但停止。循环结束时消失

有什么建议吗?

预先感谢您!

回答

1

请参阅?withProgress - 您必须告诉您的进度条,例如,

ui <- fluidPage(
    headerPanel("Progress indicators"), 
    sidebarPanel(
    numericInput("n", "N:", min = 0, max = 100, value = 50000), 
    br(), 
    actionButton("goButton", "Go!") 

), 
    mainPanel(
    verbatimTextOutput("nText") 
) 
) 

server <- function(input, output) { 

    fictional <- reactive({ 
    n=input$n 
    p = rep(0,n) 
    for(j in 1:n){ 
     if (j%%100==0) incProgress(100/n) 
     data1=rnorm(1000,1,2) 
     data2=runif(1000,1,2) 
     p[j] = min(data1,data2) 
    } 
    pw1 = mean(p) 
    return(pw1) 
}) 
    ntext <- eventReactive(input$goButton, { fictional()}) 

    output$nText <- eventReactive(input$goButton, { 

    withProgress(message = 'Progress indicators', { 
    ntext() 
    }) 
    }) 
} 
shinyApp(ui, server)