r - Saving multiple data sets and deleting using Shiny action buttons -
having trouble linking 2 different action buttons rendered table. anytime "save cohort" action button enabled user, data set gets saved, , output table "cohort_names" updates fine. but, when "reset cohorts", "saved cohort" names table not empty. in example code below, referencing same fake data set.
shinyserver(function(input, output, session) { populations = reactivevalues(a = 0) values = reactivevalues(extracted_data = null) #this finds dataframe saved observeevent(input$run_query, { values$extracted_data = data.frame(id = c(153, 343, 996), sex = c(2,1,1)) #just example. behind scenes running sql query }) #this action button saves data frame reactive list observeevent(input$save_cohort, { if(!is.null(values$extracted_data) & input$name_cohort != "a") { populations$a = populations$a + 1 cname = ifelse(input$name_cohort == "", paste("population", populations$a), input$name_cohort) populations[[cname]] = values$extracted_data #this object comes "run query" action , works fine print(populations$a) } }) #this action button suppose reset reactive object "populations" null , resets counter (a) observeevent(input$reset_cohorts, { populations = null populations$a = 0 print(populations$a) }) #population info output$populations = rendertext(populations$a) updated_names <- reactive({ tmpnames = cbind(names(populations)[-which(names(populations) == "a")]) colnames(tmpnames) = "populations" print(tmpnames) tmpnames }) #this not updating. need cohort_names reset nothing when reset_cohorts enabled. updates fine when save_cohorts enabled. output$cohort_names = rendertable({updated_names()}, align = 'c', width = "100%") }
here simple ui.r in case wants recreate:
shinyui(fluidpage( sidebarlayout( sidebarpanel(tableoutput("cohort_names")), mainpanel(actionbutton("run_query", "run query"), actionbutton("save_cohort", "save cohort"), actionbutton("reset_cohorts", "reset cohorts"), textinputrow("name_cohort",label= null, placeholder = "enter cohort name")) ) )
my current running theory treating reactivevalues incorrectly, cannot life of me figure out appropriate solution. advice appreciated
although can achieve want, code has bug. if press reset cohorts button first time, reset in background (see console prints) ui not show updated value. second click onward on reset cohort button, works intended. not figure out why happening though :(
here code in case can live bug.
library(shiny) server <- function(input, output, session) { populations <<- list() pop_counter <- reactivevalues(a = 0) values <- reactivevalues(extracted_data = null) #this finds dataframe saved observeevent(input$run_query, { values$extracted_data = data.frame(id = c(153, 343, 996), sex = c(2,1,1)) #just example. behind scenes running sql query }) #this action button saves data frame reactive list observeevent(input$save_cohort, { if(!is.null(values$extracted_data) & input$name_cohort != "a") { pop_counter$a = pop_counter$a + 1 cname = ifelse(input$name_cohort == "", paste("population", pop_counter$a), input$name_cohort) populations[[cname]] <<- values$extracted_data #this object comes "run query" action , works fine print('inside saving cohort....') print(populations) print(class(populations)) print(pop_counter$a) } }) #this action button suppose reset reactive object "populations" null , resets counter (a) observeevent(input$reset_cohorts, { print('inside resetting of populations list') populations <<- list() pop_counter$a <- 0 print(populations) print(pop_counter$a) }) updated_names <- eventreactive(c(input$reset_cohorts, input$save_cohort),{ print('inside updated_names() ...') if(length(populations) == 0) return(data.frame()) tmpnames <- cbind(names(populations))#[-which(names(populations) == "a")] colnames(tmpnames) = "populations" print(tmpnames) tmpnames }) #this not updating. need cohort_names reset nothing when reset_cohorts enabled. updates fine when save_cohorts enabled. output$cohort_names = rendertable({updated_names()}, align = 'c', width = "100%") } ui <- shinyui(fluidpage( sidebarlayout( sidebarpanel(tableoutput("cohort_names")), mainpanel(actionbutton("run_query", "run query"), actionbutton("save_cohort", "save cohort"), actionbutton("reset_cohorts", "reset cohorts"), textinput("name_cohort",label= null, placeholder = "enter cohort name")) ) ) ) shinyapp(ui = ui, server = server)
Comments
Post a Comment