So the problem you seem to be running into is related to delayed evaluation. The commands in renderPlot()
are not executed immediately. They are captured and run when the plot is ready to be drawn. The problem is that the value of name
is changing each iteration. By the time the plots are drawn (which is long after the for loop has completed), the name
variable only has the last value it had in the loop.
The easiest workaround would probably be to switch from a for
loop to using Map
. Since Map
calls functions, those functions create closures which capture the current value of name
for each iteration. Try this
# sample data
set.seed(15)
cname <- letters[1:3]
data <- data.frame(
vals = rpois(10*length(cname),15),
customer = rep(cname,each=10)
)
runApp(list(server=
shinyServer(function(input, output){
Map(function(name) {
output[[name]] <- renderPlot({
barplot(data[data$customer==name,1])
})
},
cname)
})
,ui=
shinyUI(fluidPage(
plotOutput("a"),
plotOutput("b"),
plotOutput("c")
))
))