Add to a list in Shiny

2019-03-20 13:00发布

问题:

I want to define a list that a user may update through doing certain actions. I did this:

runApp(list(
  ui=fluidPage(
    h1('Example')
    ,textInput('txt','','Text')
    ,actionButton('add','add')
    ,verbatimTextOutput('list')
    )#ui

  ,server=function(input,output,session) {
    s.list<-reactive(isolate(d.list()))
    d.list<-reactive({if (input$add == 0)   return()
                      isolate({
      list(input$txt,unlist(s.list()))
                      })#iso
    })#rea
    output$list<-renderPrint({
      list(unlist(d.list()))
    })#list
  }#server

  ))#ruanApp

But the list updates infinitely many times, does anyone know a way to make this work?

回答1:

You can use reactiveValues

require(shiny)
runApp(list(
  ui=fluidPage(
    h1('Example')
    ,textInput('txt','','Text')
    ,actionButton('add','add')
    ,verbatimTextOutput('list')
  )

  ,server=function(input,output,session) {
    myValues <- reactiveValues()
    observe({
      if(input$add > 0){
        myValues$dList <- c(isolate(myValues$dList), isolate(input$txt))
      }
    })
    output$list<-renderPrint({
      myValues$dList
    })
  }

))