Quantcast
Channel: How to get data out of reactive function in Shiny to create new columns on the same DF - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by HubertL for How to get data out of reactive function in Shiny to create new columns on the same DF

$
0
0

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"))  })})

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>