I want to create a dataframe running a code every 30 seconds.
get_lokasyon() gives me a data frame. I want to query with get_lokasyon() every 30 seconds and add the results one after another but df_total gives me only last running get_lokasyon()
library(tcltk)
df_total = data.frame()
run <- function () {
.id <<- tcl("after", 30000, run) # after 30000 ms execute run() again
df <- get_lokasyon()
df_total <- rbind(df_total,df)
print(df_total)
}
run()
Example result of get_lokasyon():
Operator Garaj KapiNo Saat Boylam Enlem
1 OHO AA 16:24:51 29.037663 41.12744
After 90 seconds I want:
Operator Garaj KapiNo Saat Boylam Enlem
1 OHO AA 16:24:51 29.037663 41.12744
2 OHO AA 16:25:40 29.037663 41.12744
3 OHO AA 16:26:10 29.037663 41.12744
The reason this is not working is because there is no loop in your function. If you like to get the results multiple times you need to specify a for-loop or while-loop.
The function tcl() might trigger ever x seconds, but this would not trigger the rest of the function around it again unless you are in a reactive environment like Shiny.
Here is an example how you could implement this
myData = data.frame()
for(x in 1:3){
print(paste("Start round", x))
newData = data.frame(x = x , time = Sys.time(), val = runif(1))
myData = rbind(myData, newData)
Sys.sleep(3)
}
#> [1] "Start round 1"
#> [1] "Start round 2"
#> [1] "Start round 3"
myData
#> x time val
#> 1 1 2021-02-07 09:19:25 0.8187054
#> 2 2 2021-02-07 09:19:28 0.3396553
#> 3 3 2021-02-07 09:19:31 0.1789086