How to show the progress of code in R?
This should work:
mydata=matrix(rnorm(6000*300),ncol = 300)
result=as.data.frame(matrix(nrow = 6000,ncol = 2))
progression<-winProgressBar(title = "Progress bar", min = 0,max = 6000 , width = 300)
for (i in 1:6000) {
abc=MannKendall(mydata[i,])
result[i,1]=abc$tau
result[i,2]=abc$sl
setWinProgressBar(progression, i, title=paste(round(progress/6000)*100,"% done"))
}
How to show the progress of code in parallel computation in R?
The doSNOW package has support for progress bars, while doParallel does not. Here's a way to put a progress bar in your example:
require(doSNOW)
require(Kendall)
cores <- parallel::detectCores()
cl <- makeSOCKcluster(cores)
registerDoSNOW(cl)
mydata <- matrix(rnorm(8000*500), ncol=500)
pb <- txtProgressBar(min=1, max=8000, style=3)
progress <- function(n) setTxtProgressBar(pb, n)
opts <- list(progress=progress)
result <-
foreach(i=1:8000, .packages="Kendall", .options.snow=opts,
.combine='rbind') %dopar% {
abc <- MannKendall(mydata[i,])
data.frame(tau=abc$tau, sl=abc$sl)
}
close(pb)
stopCluster(cl)
R: Text progress bar in for loop
for progress bar to work you need a number to track your progress. that is one of the reasons as a general rule I prefer using for with (i in 1:length(ind))
instead of directly putting the object I want there. Alternatively you'll just create another stepi
variable that you do stepi = stepi + 1
in every iteration.
you first need to create the progressbar object outside the loop
pb = txtProgressBar(min = 0, max = length(ind), initial = 0)
then inside you need to update with every iteration
setTxtProgressBar(pb,stepi)
or
setTxtProgressBar(pb,i)
Remember to close the progress bar to output the newline character. From the documentation:
The progress bar should be closed when finished with: this outputs the
final newline character.
Simply add at the end of your loop:
close(pb)
This will work poorly if the loop also has print
commands in it
How to show progress bar for computation in eventReactive()?
eventReactive
isn't executed when the event expression is triggered, it is only flagged as invalidated. You'll have to request their reactive value to have them executed - please check the following:
library(shiny)
ui <- fluidPage(
actionButton("go1", "Go! Number 1"),
actionButton("go2", "Go! Number 2"),
plotOutput("plot")
)
server <- function(input, output) {
myData <- eventReactive(input$go1, {
withProgress({
for (i in 1:15) {
incProgress(1/15)
Sys.sleep(0.25)
}
}, message = "Doesn't show up!")
cars
})
output$plot <- renderPlot({
input$go2
req(myData())
withProgress({
for (i in 1:15) {
incProgress(1/15)
Sys.sleep(0.1)
}
}, message = "Shows up!")
plot(myData())
})
}
shinyApp(ui = ui, server = server)
For your example scenario observeEvent
is the better choice. Please see this.
Related Topics
How to Create a List in R from Two Vectors (One Would Be the Keys, the Other the Values)
Arrange_() Multiple Columns with Descending Order
Why Are Lubridate Functions So Slow When Compared with As.Posixct
Adding Scale Bar to Ggplot Map
How to Prevent Functions Polluting Global Namespace
Format Latitude and Longitude Axis Labels in Ggplot
What Does Passing an Ellipsis (...) as an Argument Mean in R
Convert Begin and End Coordinates into Spatial Lines in R
Xpath to Extract Text After Br Tags in R
Trycatch with Parlapply (Parallel Package) in R
Cumulative Count of Unique Values in R
How to Increase the Space Between Grouped Bars in Ggplot2
How to Edit and Save Changes Made on Shiny Datatable Using Dt Package
Retrieve Census Tract from Coordinates
R: Split Elements of a List into Sublists
How to Check If a Vector Contains N Consecutive Numbers