Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add to a list in Shiny

Tags:

list

r

global

shiny

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?

like image 334
asosnovsky Avatar asked May 26 '14 16:05

asosnovsky


1 Answers

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

))

shiny app

like image 187
jdharrison Avatar answered Sep 29 '22 16:09

jdharrison