How to use isolate()

Hi,

I read the article at "https://shiny.rstudio.com/articles/isolation.html"
Does it mean to say that with isolate() an input value will not override a previous input value, but will add to a list? Is the list being created because isolate() is being used otherwise the value of myValues$list would be overwritten and a list would not be created?

Thanks

Here is an examle:

 ui <- fluidPage(
   headerPanel("Test"),
 
 textInput('txt','','Text'),
 actionButton('add','add'),
 verbatimTextOutput("list")
)
  
 server <- function(input, output, session){
   myValues <- reactiveValues()
   observe({
     if(input$add > 0){
       myValues$List <- c(isolate(myValues$List), isolate(input$txt))
    }
  })
   output$list<-renderPrint({
     myValues$List
     result <- receivelist(myValues$List)
     value <- paste(result$myList, result$L)
     value
   })
   
 }
 
 receivelist <- function(myList){
   x <- list()
   myList <- append(x, myList)
   L <- length(myList)
   print(myList)
   list(myList = myList, L = L)
   
   
 }
 
 shinyApp(ui = ui, server = server)

In your example what you are doing is preventing a dependency on either :

myValues$List

or

input$txt

...removing isolate from around myValues$List would cause your app not to not do anything.

This next one makes the role of isolate more obvious:
...removing isolate from around input$txt would cause your app to add to the list anytime text is entered after the first time "add" is pressed by causing the following observe to reevaluate every time input$text is changed :

observe({
     if(input$add > 0){
       myValues$List <- c(isolate(myValues$List), isolate(input$txt))
    }
  })

In this case, however, it would be simpler to use observeEvent:

myValues <- reactiveValues()
 observeEvent(input$add, {
      myValues$List <- c(myValues$List, input$txt)
      
  })
  

Thanks very much. I guess i do not understand the meaning of dependency. Could you please elaborate a little bit more on that?

By dependency I mean that a function that includes input$txt without being wrapped with isolate() will
be invalidated anytime input$txt is updated.

https://shiny.rstudio.com/images/reactivity_diagrams/isolate_process_5.png

This should help with understanding dependencies:

And this for a walk-through of "isolate":

and

1 Like

Thank you for all the info.