Calculate Value using JavaScript and Return it to Shiny

53 views
Skip to first unread message

Dario Strbenac

unread,
Jun 21, 2016, 9:00:08 PM6/21/16
to Shiny - Web Framework for R
The example in the insertUI documentation page depends on a single action button incrementing its value each time it is clicked to generate a sequential ID for a new row containing a textInput field. What if each added row of inputs has + and - buttons at the end of it ? No single add or remove button will have the total of added rows. Is there a way to call a JavaScript function which, for example, uses document.querySelectorAll to find all of the dynamically added rows and total how many there are, then pass this value back to R, enabling a reactive function to generate a new row of inputs associated with a unique ID ?

Joe Cheng

unread,
Jun 21, 2016, 10:05:02 PM6/21/16
to Dario Strbenac, Shiny - Web Framework for R
I think it'd be better to use reactiveValues (or its cousin, makeReactiveBinding) and have the + and - buttons mutate it. Something like this?

rv <- reactiveValues(count = 0)

observeEvent(input$plus, {
  rv$count <- rv$count + 1
  insertUI(
    selector = "#add",
    where = "afterEnd",
    ui = div(id=paste0("container", rv$count),
      textInput(paste0("txt", rv$count),
        "Insert some text")
    )
  )
})
observeEvent(input$minus, {
  removeUI(paste0("#container", rv$count))
  rv$count <- rv$count - 1
})

On Tue, Jun 21, 2016 at 6:00 PM, Dario Strbenac <dario....@gmail.com> wrote:
The example in the insertUI documentation page depends on a single action button incrementing its value each time it is clicked to generate a sequential ID for a new row containing a textInput field. What if each added row of inputs has + and - buttons at the end of it ? No single add or remove button will have the total of added rows. Is there a way to call a JavaScript function which, for example, uses document.querySelectorAll to find all of the dynamically added rows and total how many there are, then pass this value back to R, enabling a reactive function to generate a new row of inputs associated with a unique ID ?

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/d1f4e31d-2e37-440c-9b0b-422e7785399b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dario Strbenac

unread,
Jun 22, 2016, 9:00:17 PM6/22/16
to Shiny - Web Framework for R
That's a convenient idea for keeping track of the number of buttons. However, there is still the problem that any plus button generates a new filter row, which also has a plus button at its end (as commonly done on web forms for booking sites). The typical example of dynamic UIs on the RStudio examples website is where the dynamically generated element does not have the capability to generate other dynamic elements from it.

This problem could be conveniently avoided if it were possible to create observers that listened to a class of inputs rather than a single element ID. For example, a pseudocode representation is:

ui <- (textInput, selectInput, plusButton1(class = "insert-ui-button"), minusButton1(class = "remove-ui-button")) # first row of filters.

server <- function{
  reactivesTotal <- reactiveValues(total = 1)
}

observeEvent(input[[".insert-ui-button"]] {  # Catch all events triggered by a particular class of HTML element.

  triggerID <- getElementIDofTrigger()
 
  insertUI(selector = triggerID,
              ui = div(textInput, selectInput, plusButton2(class = "insert-ui-button"), minusButton2(class = "remove-ui-button")))

  reactivesTotal[["total"]] = reactivesTotal[["total"]] + 1
})

observeEvent(input[[".insert-remove-button"]] {

  triggerID <- getElementIDofTrigger()
  removeUI(selector = triggerID)

  reactivesTotal[["total"]] = reactivesTotal[["total"]] - 1
})


This would only require one listener for the plus buttons and one for the minus buttons, regardless of how many such buttons there are in the user interface at any time.

Reply all
Reply to author
Forward
0 new messages