Add to a list in Shiny

。_饼干妹妹 提交于 2020-01-01 03:15:07

问题


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

))



来源:https://stackoverflow.com/questions/23874674/add-to-a-list-in-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!