tabPanels not showing correct data

I have a shiny page that displays information about cities. Some of the cities are in multiple counties. In that instance, I want to show data for each county in a separate tab. I have code to produce the data table I want to display for each county, and a list that contains each data table. However, when I render this information, tabs for each county are displayed, but the data displayed is for the last county in the series.

The relevant part of my code:
selpop <- subset(popdata, placenumber %in% plnum)
if(nrow(selpop) == 1) { # This part, for cities in a single county, works
sdotab <- pop_data(selpop)
outlist <- list(tabPanel(ctymat[1,2],renderTable(sdotab, digits=0,include.rownames=FALSE)))
return(outlist)
} else { # multicounty cities
ctynames <- matrix(nrow=nrow(selpop))
#Generating total
selpop2 <- subset(selpop, counumber == 999)
ctynames[1] = "Total"
sdotab <- pop_data(selpop2)
outlist <- list(sdotab)
for(i in 1:nrow(ctymat)) {
ctynames[i+1] = ctymat[i,2]
selpop2 <- subset(selpop, counumber == as.numeric(ctymat[i,1]))
sdotab <- pop_data(selpop2)
xlist <- list(sdotab)
outlist <- append(outlist,xlist) # at this point, "outlist" is a list with one data table for each county
} # for

tablist <- list()
for(nm in 1:nrow(ctynames)){
tablist[[nm]] <- tabPanel(ctynames[nm],renderTable(outlist[[nm]], digits=0,include.rownames=FALSE))
}

return(tablist)
#tablist should display the rendered data table for each county, but the data displayed is from the last county in the list

} # multicounty cities

}
Here is the output:

Any ideas about how to display the correct data in each tabPanel?

TIA

I found the solution,
replace

tablist <- list()
for(nm in 1:nrow(ctynames)){
tablist[[nm]] <- tabPanel(ctynames[nm],renderTable(outlist[[nm]], digits=0,include.rownames=FALSE))
}

with

tablist <- lapply(1:nrow(ctynames), function(i) {
  tabPanel(ctynames[i],renderTable(outlist[[i]], digits=0,include.rownames=FALSE))
})

All is well.
Cheers