Shiny R reactivevalues memory leak -
i'm trying understand why cycling use of shiny's reactivevalues
causes use more memory. context user interface option automate given strategy. example below based on st. petersburg paradox. appreciate may better practice place entire automation process in seperate function file understand why reactive object increases in size , if there more immediate work around.
the example below print out amount of memory values
using on disk. there no increase in size list saved it.
library(shiny) library(pryr) server=shinyserver(function(input, output) { values=reactivevalues(win=0, total=1000, gamble=0, stick=0, outcome=0, auto.counter=0 ) ############################################# # update action buttons observe({ if(!is.null(input$stick)){ isolate({ values$stick=values$stick+1 }) } }) observe({ if(!is.null(input$gamble)){ isolate({ values$gamble=values$gamble+1 }) } }) ############################################# ## perform stick action observe({ if(values$stick>0){ isolate({ values$total=values$total+2^values$win values$auto.counter=max(values$auto.counter-1,0) values$win=0 cat("\nstick:",2^values$win) cat("\nreactive values size:",object_size(values)) }) } }) # perform gamble action observe({ if(values$gamble>0){ isolate({ values$outcome=sample(1:2,1) if(values$win==0){ values$total=values$total-input$entry } if(values$outcome==1){ output$print=rendertext("win") values$win=values$win+1 } else { output$print=rendertext("lose") values$win=0 } cat("\ngamble:",2^values$win) cat("\nreactive values size:",object_size(values)) values$auto.counter=max(values$auto.counter-1,0) }) } }) ############################################# # automation input observe({ if(input$automate>0){ isolate({ values$auto.counter=input$rep }) } }) # if automation on run until auto.counter==0 observe({ if(!is.null(values$auto.counter)){ if(values$auto.counter>0){ isolate({ if(2^values$win>input$target){ values$stick=values$stick+1 } else { values$gamble=values$gamble+1 } }) } } }) ############################################# # render views output$total=rendertext({paste("total winnings:",values$total)}) output$currentvalue=rendertext({paste("current jackpot:",2^values$win)}) }) ui=shinyui(fluidpage( br(), fluidrow( column( 4, numericinput(inputid="entry",label="entry fee",value=10), actionbutton(inputid="gamble",label='gamble'), actionbutton(inputid="stick",label='stick'), h4("automate"), numericinput(inputid="target",label="target value",value=20), numericinput(inputid="rep",label="turns",value=10), actionbutton(inputid="automate",label='automate') ), column( 8, textoutput("currentvalue"), textoutput("total"), textoutput("print") ) ) )) runapp(list(ui=ui,server=server))
Comments
Post a Comment