You have to put the reactive
out of the function, for example you can call the function from the reactive
:
shinyServer(function(input, output, session) { generate <- function(dates){ listofdfs <- list() #Create a list in which you intend to save your df's. for (i in 1:length(dates)){ data <- dbGetQuery(con, sprintf("select Column A, CAST (date AS date), id from My_Table where id =", input$user_id," and date <= '%s' and date >= '%s'- INTERVAL '7 days'", dates[i], dates[i])) data$Column_A_mean <- mean(data[,1]) #creating a new column like we do in a data frame (DF) listofdfs[[i]] <- data() # save your dataframes into the list } return(listofdfs) #Return the list of dataframes. } df <- reactive({do.call("rbind", generate(dates))}) output$Coolplot <- renderPlot({ ggplot(data = df(), aes(date)) + geom_line(aes(y = Column A, colour = "Column A"))+ geom_line(aes(y = Column_A_mean, colour = "Column_A_mean")) })})