Stella Biderman Stella Biderman - 1 month ago 7
R Question

Displaying data from a list in R without dynamically changing variable names

I'm writing some code in R that builds a list of data frames. While it runs, it needs to display each of the data frames it creates in a separate tab. The data frames and the list are both created by several nested for loops, along the lines of:

df.list <- vector("list", length(e))
i <- 1
for (...){
data <- as.data.frame(stuff)
j <- 1
for (...){
for (...){
[loop stuff]
data[j,] <- [more stuff]
}
}
df.list[[i]] <- data
i <- i + 1
}


The question is where to put the "View" function. If I add a second loop at the end that runs through the list and displays the data frames, then they all get named "df.list". If I put View(data) right before df.list[[i]] <- data then they all get named "data". Having them all have the same name is not an acceptable situation for this context. Ideally, I would be able to name them whatever string I want, but I would settle for anything that is reasonably understandable and distinguishable from the other data frames.

I know I can solve this by dynamically changing the variable name to be datai where i is the list index, but that's almost always the wrong way to do things.

Answer

Posting the solution from the comments so I can close:

The View() function takes names as optional arguments! View(data, name) will display data and call the tab name