If you happen to’re like me, I exploit numerous loops in R. I don’t profess to be essentially the most environment friendly coder, however loops make sense to me and I’m usually not involved about make the quickest simulations.
However generally my loops take a while to complete, so I typically add a rolling textual content replace throughout the simulation to understand how far it has progressed. However after all, I’ve to take a look at the R console to see how far issues have come. Being a bit away from the central tendency of the spectrum, I can get absorbed in doing different issues, so I typically miss when the simulation is full.
In a match of extra geekiness, I’ve just lately found voice prompts in MacOS that I can now code immediately into my R simulations to present verbal updates on their progress. I discover these immensely helpful. I’ve due to this fact determined to share the essential code, as a result of I do know another geeks on the market may also recognize the software. Apologies — I haven’t investigated how to do that in a PC setting, so the next examples are MacOS-specific.
First, go to your Accessibility settings in System Settings in your Mac. Click on on System Voice to see what voices you will have entry to, and which voices you want to obtain to your machine. There are lots of languages supported.
While you assemble a loop in R, add the next code inside and earlier than the loop content material (this instance is in English):
iter <- 1000 # variety of iterations
itdiv <- iter/100 # iteration divisor 1
itdiv2 <- iter/10 # iteration divisor 2
st.time <- Sys.time() # time at begin of simulation
# loop from 1 to iter
for (i in 1:iter) {
# pause execution for 0.05 seconds (this might usually be the heart of your loop capabilities)
Sys.sleep(0.05)
# loop updaters with voice (English)
if (i %% itdiv==0) print(paste("iter = ", i, sep=""))
if (i %% itdiv2==0 & i < iter) system2("say", c("-v", "Fiona", paste(spherical(100*(i/iter), 0),
"per cent full"))) # updates each 10% full
if (i == 0.95*iter) system2("say", c("-v", "Fiona", paste(spherical(100*(i/iter), 0),
"per cent full"))) # announce at 95% full
if (i == 0.99*iter) system2("say", c("-v", "Fiona", paste(spherical(100*(i/iter), 0),
"per cent full"))) # announce at 99% full
if (i == iter) system2("say", c("-v", "Lee", "simulation full"))
if (i == iter) system2("say", c("-v", "Lee", paste(spherical(as.numeric(Sys.time() - st.time,
models = "minutes"), 2), "minutes elapsed")))
}
Right here I’ve used the feminine Scottish voice ‘Fiona’ and the male Australian voice ‘Lee’.
Right here’s an instance in French:
iter <- 1000 # nombre d'iterations
itdiv <- iter/100 # diviseur 1
itdiv2 <- iter/10 # diviseur 2
st.time <- Sys.time() # heure du graduation de la simulation
# boucle
for (i in 1:iter) {
# pauser l'éxécution pendant 0,05 secondes (emplacement typique des fonctions dans la boucle)
Sys.sleep(0.05)
# mise à jour avec les voix
if (i %% itdiv==0) print(paste("iter = ", i, sep=""))
if (i %% itdiv2==0 & i < iter) system2("say", c("-v", "Aurélie", paste(spherical(100*(i/iter), 0),
"% terminé")))
if (i == 0.95*iter) system2("say", c("-v", "Aurélie", paste(spherical(100*(i/iter), 0), "% terminé")))
if (i == 0.99*iter) system2("say", c("-v", "Aurélie", paste(spherical(100*(i/iter), 0), "% terminé")))
if (i == iter) system2("say", c("-v", "Aurélie", "simulation terminée"))
if (i == iter) system2("say", c("-v", "Aurélie", paste(spherical(as.numeric(Sys.time() - st.time,
models = "minutes"), 2), "minutes se sont écoulées")))
}
And an instance in Italian:
iter <- 1000 # numero di iterazioni
itdiv <- iter/100 # divisore 1
itdiv2 <- iter/10 # divisore 2
st.time <- Sys.time() # tempo all'inizio della simulazione
# ciclo
for (i in 1:iter) {
# mettere in pausa l'esecuzione per 0,05 secondi (inserisci qui le tue funzioni di simulazione tipiche)
Sys.sleep(0.05)
# aggiornamenti vocali
if (i %% itdiv==0) print(paste("iter = ", i, sep=""))
if (i %% itdiv2==0 & i < iter) system2("say", c("-v", "Alice", paste("completato al",
spherical(100*(i/iter), 0), "%")))
#if (i == 0.95*iter) system2("say", c("-v", "Alice", paste("completato al", spherical(100*(i/iter), 0),
"%")))
#if (i == 0.99*iter) system2("say", c("-v", "Alice", paste("completato al", spherical(100*(i/iter), 0),
"%")))
if (i == iter) system2("say", c("-v", "Alice", "simulazione completa"))
#if (i == iter) system2("say", c("-v", "Alice", paste("sono passati", spherical(as.numeric(Sys.time()
- st.time, models = "minutes"), 2), "minuti")))
}
There you will have it. Get pleasure from geeking out!