New Column Containing Row Number in Reactive Data Frame

shiny

#1

Say I have a data frame called df. I can create a new column called ID in df that lists the row number for the data frame using R.

df$ID <- seq.int(nrow(df))

Now let’s say I have a reactive data frame called df().

How can I accomplish the same thing?


#2

I think you will either have to do that within the reactive definition for df, add the ID in the client of df(), or create a new reactive based on df, e.g.

df_with_ID = reactive({
  df_new = df()
  df_new$ID = seq.int(nrow(df_new))
  df_new
})

#3

Yes, this does seem to work. I didn’t pick up that this was the answer to problems like these after watching all the tutorials on the internet. Seems to be a very important concept. Thanks for sharing.