I am receiving new data every 3 seconds, the data contains enough points to plot a new point 10x per second. I am wanting to create a plot that appears to be live stream.
My current code is this:
output$plot1 <- renderPlot({
autoInvalidate()
chuck1 <- read.csv("logfile1.txt",header=FALSE) #read data coming into logfile1.txt
autoInvalidate()
chuck1 <- na.omit(chuck1)
row_value1 <<- tail(chuck1$V1, 30) # Get most recent entries in the logfile1.txt for column 1
row_value2 <<- tail(chuck1$V2, 30) # Get most recent entries in the logfile1.txt for column 2
for(i in 1:30) #Create a for loop to cycle through the 30 entries and plot each new entry
{
row_value3 <<- row_value1[i] # set a value equal to each iteration of the for loop to be plotted later.
row_value4 <<- row_value2[i] # set a value equal to each iteration of the for loop to be plotted later.
plot(row_value3,row_value3, type="o", xlab="Time", ylab="Movement", main="Without", ylim=c(0.5,4), xlim = c(row_value1[1],row_value1[30]))
Sys.sleep(0.1) # Sleep for 0.1 second
i <- i + 1 # cycle through the loop (Unsure if i needed this just added it in just incase and tried it without it)
}
})
Currently this code results in nothing being plotted, I can see that even if it did work it would likely re-plot each time and the final thing I see is just 1 point on the screen but I don't even seem to be getting that.
I have found a few example online but none that quite match what I am looking for.
Any help would be greatly appreciated.
can you post a few example lines from the text file? If the data's proprietary, put in dummy values, but it would be helpful for us to be able to reproduce what you're experiencing.
There is a Python script that keeps the file within 100 rows for the incoming data, it writes over the last line. This keeps the last row number always 100.
I have made another attempt with comments in the code that describe what I am trying to do and my line of thinking. For some reason data_main only has 91 obs. at the end of the for loop, my expectation was for each iteration of i to be added to data_main, therefore returning it to 100 while plotting it. It plots all values until the last 10 values, it skips all those in the loop except the very last value.
output$plot5 <- renderPlot({
autoInvalidate()
data1 <- read.csv("logfile.txt",header=FALSE) #Read data
autoInvalidate()
data1 <<- na.omit(data1) #remove n/a's
data_tail <<- tail(data1$V1, 10) #prepare the recent 10 entries of V1
data_tail2 <<- tail(data1$V2, 10) #prepare the recent 10 entries of V2
i <<- 1 #declare starting value for i loop
data3 <<- head(data1,-10) #create new dataframe with that is trimmed of the new entries
for(i in 1:10) #Create a for loop to add those removed entries back in 1 by 1
{
data2 <<- data.frame(V1 = data_tail[i], V2 = data_tail2[i]) # data2 gets set to each iteration of i
data_main <<- rbind(data3, data2) #each iteration of i gets appended to the end of data3 (adding entries back in)
plot(data_main$V1,data_main$V2, type="o", xlab="Time", ylab="Movement", main="With", ylim=c(0.5,4), xlim =c(11992,11995)) #Plot (to current xlim for non-live data test)
i <- i + 1 #Cycle through i
}
})
The data set is the same as above, except I made sure to use one that had exactly 100 rows for this trial
Solved part of it, but it just plots everything at once still, is we use the sys.sleep, it just waits the full cycle (if it was 1 second wait for 10 iterations it waits 10 seconds and plots everything). So that makes me believe it needs a different approach or something? I feel like what I am trying to do is a reasonably simple task.
output$plot5 <- renderPlot({
autoInvalidate()
data1 <- read.csv("logfile.txt",header=FALSE) #Read data
autoInvalidate()
data1 <<- na.omit(data1) #remove n/a's
data_tail <<- tail(data1$V1, 10) #prepare the recent 10 entries of V1
data_tail2 <<- tail(data1$V2, 10) #prepare the recent 10 entries of V2
#i <<- 1 #declare starting value for i loop
data3 <<- head(data1,-10) #create new dataframe with that is trimmed of the new entries
for(i in 1:10) #Create a for loop to add those removed entries back in 1 by 1
{
data2 <- data.frame(V1 = data_tail[i], V2 = data_tail2[i]) # data2 gets set to each iteration of i
if (i==1){data_main <- rbind(data3, data2)}else{data_main <- rbind(data_main,data2)}
data4 <<- data_main
#each iteration of i gets appended to the end of data3 (adding entries back in)
plot(data_main$V1,data_main$V2, type="o", xlab="Time", ylab="Movement", main="With", ylim=c(0.5,4), xlim =c(11992,11995)) #Plot (to current xlim for non-live data test)
#Cycle through i
}
})
you mention Sys.sleep in your narrative, but it's not in your code. If you add Sys.sleep(1) to the end of your loop, what's missing? It seems like that results in what you were after.
I've tried that, I've tried putting Sys.sleep(1) in all kinds of places in that code. When I put Sys.sleep(1) anywhere in that loop it will take 10 seconds before the plot appear, note this plot has all points rather than 1 at a time. No idea why it seems to complete the entire 1:10 of the loop before displaying anything but that is whats happening.
Basically,
What I expect:
Collect data from file
Remove last 10 rows from dataframe to be plotted
Add the 10 rows back into the data frame 1 by 1 and plot it
Wait 1 second, repeat
What happens:
Collect data from file
Remove last 10 rows from dataframe to be plotted
Wait 10 seconds and plot entire dataframe (with the 10 rows already added back in)
Here's a couple of thoughts... you're junk is all wrapped up in a function which is fine later, but sucks for debugging. Pull it out so you can interact easier and test things. You have this renderPlot thing which I have no idea what it is. same with autoInvalidate(). I killed that stuff so I could run your code, then added in simulated data. I put a Sys.sleep(1) at the end of the loop and it seems to do exactly what you say you want:
data1 <- data.frame(V1=1:20, V2=rnorm(20)) #Read data
data1 <<- na.omit(data1) #remove n/a's
data_tail <<- tail(data1$V1, 10) #prepare the recent 10 entries of V1
data_tail2 <<- tail(data1$V2, 10) #prepare the recent 10 entries of V2
#declare starting value for i loop
data3 <<- head(data1,-10) #create new dataframe with that is trimmed of the new entries
for(i in 1:10) #Create a for loop to add those removed entries back in 1 by 1
{
data2 <- data.frame(V1 = data_tail[i], V2 = data_tail2[i]) # data2 gets set to each iteration of i
if (i==1){
data_main <- rbind(data3, data2)
}else{
data_main <- rbind(data_main,data2)
}
data4 <<- data_main
plot(data_main$V1,data_main$V2, type="o", xlab="Time", ylab="Movement", main="With") #Plot (to current xlim for non-live data test)
Sys.sleep(1)
}
Thats given me a few ideas and made me realise the need to explain more of the situation, cheers for sticking around and helping.
The reason for the renderplot was because we are having several plots in rows down a page. We have been using fluidrow() to allow for this, but I am starting to feel like we may need to take a different approach.
I have some live streaming code where the external data file gets updated row by row which makes my current approach actually work. Unfortunately this new data isn't coming in 1 at a time :(.
Essentially I need to be able to place to plot in a box so that I can arrange my display to fit in with other things that are happening.
I hope I have explained myself a bit better, really appreciate the help.
Ohhh.. I see now that this is tagged shiny and I had not paid attention to that initially! My mistake. That really changes things. I have very limited Shiny experience so I'll make a few comments and then maybe someone will come in and correct me if I'm wrong
Shiny expects your R code to produce a static graph and then it renders the graph. So what you're seeing is that Shiny waits until R is totally done running everything then it renders the result. Thus the wait for 10 seconds before plotting.
The first link above may be only for animations where the data does not change. I think the reactivetimer example is more what you need. But I am out of my experience base here...