R 仅更新闪亮应用程序中数据框中的特定行

R 仅更新闪亮应用程序中数据框中的特定行,r,dataframe,shiny,R,Dataframe,Shiny,因此,我试图构建一个闪亮的应用程序来实时计算特定旅行的距离。我将每隔15秒实时获取数据,并在数据帧df1中更新。为此,我需要在第一行的另一个数据帧cal_distdf中存储我在前15秒内获得的值 随后,在接下来的15秒内,当我在df1中获得新更新的数据时,应该替换cal\u distdf的第二行注意:cal\U distdf的第一行在整个应用程序中保持不变,每15秒只更新第二行 下面是我的R脚本 cal_distdf <- df1$Odovalue distcal <- react

因此,我试图构建一个闪亮的应用程序来实时计算特定旅行的距离。我将每隔15秒实时获取数据,并在数据帧
df1
中更新。为此,我需要在第一行的另一个数据帧
cal_distdf
中存储我在前15秒内获得的值

随后,在接下来的15秒内,当我在
df1
中获得新更新的数据时,应该替换
cal\u distdf
的第二行注意:cal\U distdf的第一行在整个应用程序中保持不变,每15秒只更新第二行

下面是我的R脚本

cal_distdf <- df1$Odovalue

distcal <- reactive({

       cal_distdf[2] <- df1$Odovalue
       disttravelled <- cal_distdf[2] - cal_distdf[1]

      return(disttravelled)
})

output$Distance <- renderText({distcal()})

cal\u distdf您可以使用
reactiveValues()
最初创建数据帧。此外,在将新行追加到
cal_distdf
时,您可以使用
isolate()
,而不是仅更改特定的行值,您可以使用新值追加行,并获取第一个值和最新追加值之间的差值

#Create New dataframe
values <- reactiveValues()
values$cal_distdf <- data.frame(Odovalue = numeric(0))

 #Function to calculate distance

distcal <- reactive({

   newdistvalue <- df1$Odovalue #Store the new value to a variable
   newrow <- isolate(c(newdistvalue)) 
   isolate( values$cal_distdf [nrow( values$cal_distdf ) + 1,] <- c(newLine)) #append new row to the existing dataframe. NOTE using rbind() changes the column name.
   newdistvalue1 <-  values$cal_distdf #Copying appended rows dataframe to new data frame. 
   cal_dist <- as.numeric(tripd$Odovalue[nrow(newdistvalue1)]) - as.numeric(newdistvalue1$Odovalue[1]) # Calculating difference between new appended row with the first row value to Calculate distance
  return(cal_dist)

})

 output$Distance <- renderText({distcal()})
#创建新的数据帧

值更新数据帧中特定单元格的方法是
df[row,col]@Rodrigo,我希望它是一个数据帧计算距离不是我将执行的唯一操作。
df