How to conditionally append a list using action buttons

307 views
Skip to first unread message

Andres Fonseca

unread,
Apr 10, 2014, 4:24:27 PM4/10/14
to shiny-...@googlegroups.com
Hey everyone, I am successfully iterating through a list of lists using a 'yes' and 'no' action button. Like so...

inc <- reactive({input$yes + input$no + 1}) ##where input$yes and input$no are the buttons in question

The problem I'm having is that while (with some safety precautions) I can move through and display each elements of my list of lists 
by clicking either button, what I really want is to generate another list of list that represents those elements for which I clicked yes instead of no.

As a simplified example, imagine a page with a side bar that say 'is this letter a vowel?' and a button for yes and no underneath.
In the main panel we see the letters[[inc()]] (or 'a' initially). I want to be able to build a list of those elements I clicked yes for.

I hope that makes sense. Please let me know if you want me to elaborate on anything.

Thanks!

Andres Fonseca

unread,
Apr 11, 2014, 2:50:36 PM4/11/14
to shiny-...@googlegroups.com
Here is some sample code for the alphabet example

##############
SERVER.R

library(shiny)

shinyServer(function(input, output) {


  inc <- reactive({input$yes + input$no + 1}) 
  i <- reactive({ifelse(inc() <= 26, inc(), -1)})

  add <- reactive({
    if (input$yes == 0) return(invisible())
    isolate({ letters[i()] })

  })

  alphabet <- reactive({
    if (i() < 0) return('No more letters') 
    else return(letters[i()] )
  })
  
  output$testing <- renderPrint({ alphabet() })
  output$lastAdd <- renderPrint({ add() })
  
})
###########
UI.R

library(shiny)

shinyUI(fluidPage(

  titlePanel("Iteratively build a list?"),

  sidebarLayout(
    sidebarPanel(
      h3('Is this a vowel?'),
      actionButton("yes", label = "Yes"),
      actionButton("no",  label = "No" )
      
    ),

    mainPanel(
      verbatimTextOutput('lastAdd'),
      verbatimTextOutput('testing')
    )
  )
))

What I want is to have an object that gains a letter every time I click yes. I've been doing some research into reactiveValues but can't 
seem to figure out how to append anything to them.

Hope this clarifies my question.

Joe Cheng

unread,
Apr 11, 2014, 9:05:59 PM4/11/14
to Andres Fonseca, shiny-...@googlegroups.com
In your server function (note, I haven't tested this code but it seems like it should work):

values <- reactiveValues(answers = logical(0))
observe({
  if (input$yes == 0) return()
  isolate({
    values$answers <- c(values$answers, TRUE)
  })
})
observe({
  if (input$no == 0)
    return()
  isolate({
    values$answers <- c(values$answers, FALSE)
  })
})

Then refer to values$answers wherever you want to get at the answers. (Unlike with a reactive expression, you don't need to do values$answers(), just values$answers will do--like an input.)


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages