Can you confirm if the next break cancels the inner for loop?
for (out in 1:n_old){
id_velho <- old_table_df$id[out]
for (in in 1:n)
{
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}else
if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
}
Well, your code is not reproducible so we will never know for sure, but this is what help('break')says:
break breaks out of a for, while or
repeat loop; control is transferred to
the first statement outside the
inner-most loop.
So yes, break only breaks the current loop. You can also see it in action with e.g.:
for (i in 1:10)
{
for (j in 1:10)
{
for (k in 1:10)
{
cat(i," ",j," ",k,"\n")
if (k ==5) break
}
}
}
your break statement should break out of the for (in in 1:n).
Personally I am always wary with break statements and double check it by printing to the console to double check that I am in fact breaking out of the right loop. So before you test add the following statement, which will let you know if you break before it reaches the end. However, I have no idea how you are handling the variable n so I don't know if it would be helpful to you. Make a n some test value where you know before hand if it is supposed to break out or not before reaching n.
for (in in 1:n)
{
if (in == n) #add this statement
{
"sorry but the loop did not break"
}
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}
else if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
Related
In R, is it possible to create a function that sets a default answer after some defined amount of time for menu (or to create something that works like that)?
My best not-working idea is to try using {future}.
Example
library(future)
plan(multisession(workers = 2))
zero_after_s <- function(s) Sys.sleep(s)
zero_after_t_menu <- function(
choices, graphics = FALSE, title = NULL, t = 0
) {
if (t == 0) {
menu(choices, graphics, title)
} else {
time_passed <- future(zero_after_s(t))
while (!resolved(time_passed)) {
return(menu(choices, graphics, title))
}
0
}
}
switch(
zero_after_t_menu(c("List letters", "List LETTERS"), t = 5) + 1,
cat("Nothing done (maybe t seconds passed without answers)\n"),
letters,
LETTERS
)
This cannon work because while will cycle and check its argument only when its body finishes the cycle, i.e., the user has answered to menu. I put this just as a tentative idea of a solution.
I try to call menu from the future (it seems more promising), but I cannot interact with it anymore (obviously), and anyway, it throws an error because menu cannot be used non-interactively :-)
zero_after_t_menu <- function(
choices, graphics = FALSE, title = NULL, t = 0
) {
if (t == 0) {
menu(choices, graphics, title)
} else {
res %<-% future(menu(choices, graphics, title), earlySignal = TRUE)
Sys.sleep(t)
if (resolved(res)) res else 0
}
}
Any ideas or suggestions?
Thank you,
Corrado.
PS: my actual use case is a loop across some (many) files to be (slowly) preprocessed. Under some conditions, I would like to have the opportunity to select what to do. Still, given that it is a very long execution, during nights or not-monitoring time, I would like a safe default selection (e.g., "skip that iteration for the future") to be made automatically to permit the loop to go on without my supervision.
When i execute a crowd simulation, all dots start going to the suggested metting point (reffer to the code). However the dots (people leaving the room) start to step over the other dots, something that shouldnt be happening according to my excersice.
Here is the code:
dimensionX=10
dimensionY=10
numberPeople=20
velocity=0.001
varianzavelocidad=runif(1, min=0, max=5)
x<-dimensionX*runif(numberPeople)
y<-dimensionY*runif(numberPeople)
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
for(i in 1:10000) {
for(j in 1:numberPeople) {
ang <- atan((y[j]-5)/(10-x[j]))
x[j]<-x[j]+velocity*varianzavelocidad*cos(ang)
y[j]<-y[j]-velocity*varianzavelocidad*sin(ang)
}
x[x>10]=10
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
}
My thoughts were that i should be working with an If()/Else() condition inside the J array, however am not sure how to read each J object and set a condition that if the dot/point tries to step over another dot/point it would compare a strength value between the dots trying to step over.
A clue of how i would like to make it work:
dimensionX=10
dimensionY=10
numberPeople=20
velocity=0.001
strength=runit(1) *******
varianzavelocidad=runif(1, min=0, max=5)
x<-dimensionX*runif(numberPeople)
y<-dimensionY*runif(numberPeople)
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
for(i in 1:10000) {
for(j in 1:numberPeople) {
ang <- atan((y[j]-5)/(10-x[j]))
x[j]<-x[j]+velocity*varianzavelocidad*cos(ang)
y[j]<-y[j]-velocity*varianzavelocidad*sin(ang)
}
if(X[j1] = X[J2] && Y[J1] = Y[J2]) {
//MAKE THE DOTS CHOOSE BETWEEN THE STRONGEST //using strength value
} else() { they keep on going }
x[x>10]=10
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
}
I have tried downloading foreach function but honestly havent been able to find a way to use it correctly. Any thoughts?
I am using RStudio, I have a programm and I want to see the value of each parameter in each iteration. I am seeing only value of parameters on the last iteration.
Here is my R code:
k<-read.csv("D:\\Testc.csv")[,1:5]
window<-64
stelle<-""
spalte<-1
co<-0
r<-1
l<-(2/sqrt(window))
while(spalte < 3)
{
datalist<-matrix(k[,spalte])
while(r+window<=length(datalist[,1]))
{
m<-acf(as.numeric(datalist[r:(r+window),1]),lag.max=32,plot=FALSE)$acf[-1]
for(i in (1:32))
{
if(m[i]>l)
{
co<-co+1
}
}
if(co>5)
{
row<-as.character(r)
spalte<-as.character(spalte)
pos<-rbind(row,spalte)
stelle<-c(stelle,"-",pos)
}
r<-r+30
co<-0
}
spalte<-spalte+1
}
stelle
I want to see the value of co ,spalte, stelle on each iteration. On the left side of RStudio on Workspace tab I see values only for last iteration.
place the following code into your loop at the end:
print(paste("co=", co))
print(paste("spalte=", spalte))
print(paste("stelle=", stelle))
Here is what I have so far, this is returning two columns, but each counter is stopping and then duplicating the same value over and over...
if(lLogisticsControlTable.APMJobTypeId)
select count (RecID) from jobTypeCheck where jobTypeCheck.APMJobTypeId == lLogisticsControlTable.APMJobTypeId;
{
counter = jobTypeCheck.RecId;
}
while select jobTypeCheck where jobTypeCheck.APMJobTypeId == lLogisticsControlTable.APMJobTypeId
{
counter1 = counter / 2;
halfCount1 = counter - counter1;
if(halfcount <= counter1)
{
halfCount++;
jobListCheck1 = jobTypeCheck.Name;
}
if (halfCount1 > halfCount)
{
halfCount1++;
jobListCheck2 = jobTypeCheck.Name;
}
element.execute(2);
}
}
As Michael Brown indicated, it's difficult to understand the problem with half of the code ;)
However, I would suggest that you call the element.execute(2) method on every second pass through the loop? That way jobListCheck1 would be on the left, and jobListCheck2 would be on the right hand side. Finally you would then need to check immediately outside of your loop if you had an odd number of jobTypeCheck elements, and call the element.execute(2) method one last time remembering to set the jobListCheck2 variable as empty beforehand.
Regards
Can you confirm if the next break cancels the inner for loop?
for (out in 1:n_old){
id_velho <- old_table_df$id[out]
for (in in 1:n)
{
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}else
if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
}
Well, your code is not reproducible so we will never know for sure, but this is what help('break')says:
break breaks out of a for, while or
repeat loop; control is transferred to
the first statement outside the
inner-most loop.
So yes, break only breaks the current loop. You can also see it in action with e.g.:
for (i in 1:10)
{
for (j in 1:10)
{
for (k in 1:10)
{
cat(i," ",j," ",k,"\n")
if (k ==5) break
}
}
}
your break statement should break out of the for (in in 1:n).
Personally I am always wary with break statements and double check it by printing to the console to double check that I am in fact breaking out of the right loop. So before you test add the following statement, which will let you know if you break before it reaches the end. However, I have no idea how you are handling the variable n so I don't know if it would be helpful to you. Make a n some test value where you know before hand if it is supposed to break out or not before reaching n.
for (in in 1:n)
{
if (in == n) #add this statement
{
"sorry but the loop did not break"
}
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}
else if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}