0

I have read this (How do I make the choices in radioButtons reactive in Shiny?) which shows me how to update radioButtons in a reactive way. However, when I try and update two sets of buttons from the same data, only one set renders. Example:

Server:

# Create example data

Wafer <- rep(c(1:3), each=3)
Length <- c(1,1,2,1,1,1,3,5,1)
Width <- c(3,1,6,1,1,1,1,1,6)

dd <- data.frame(Wafer, Length, Width)

shinyServer(function(input, output, session){

# Create reactive dataframe to store data
  values <- reactiveValues()
  values$df <- data.frame()

# Get Lengths and Widths of wafer from user input
 a <- eventReactive(input$do, {
       subset(dd, Wafer %in% input$wafer, select = Length:Width) 
 })

# Update reactive data frame will all Lengths and Widths that have been selected by the user input

 observe({
   if(!is.null(a())) {
     values$df <- rbind(isolate(values$df), a())
   }
  })

  output$wl <- renderDataTable({ a() })

# Update radio buttons with unique Length and Widths stored in values$df
# Which ever "observe" I put first in the code, is the one which updates 
# the radio buttons. Cut and paste the other way round and "width" 
# updates but not "length" radio buttons

  observe({ 
    z <- values$df
    updateRadioButtons(session, "length", choices = unique(z$Length), inline=TRUE)
  })

  observe({ 
    z <- values$df
    updateRadioButtons(session, "width", choices = unique(z$Width), inline=TRUE)
  })

})

ui:

library(markdown)

shinyUI(fluidPage(
  titlePanel("Generic grapher"),
  sidebarLayout(
    sidebarPanel(

      numericInput("wafer", label = h3("Input wafer ID:"), value = NULL),

      actionButton("do", "Search wafer"),
      radioButtons("length", label="Length", choices=""),
      radioButtons("width", label="Width", choices = "")

    ),

    mainPanel(

      dataTableOutput(outputId="wl")
    )
  )
)
)

In the above, radiobuttons do update but only for the first set of buttons in order of code i.e. above "length" updates but "width" doesn't. If I write them in reverse, "width" updates but "length" doesn't. Do I need to define a new session maybe?

Community
  • 1
  • 1
Pete900
  • 2,016
  • 1
  • 21
  • 44
  • Problem was resolved here: http://stackoverflow.com/questions/35056668/update-two-sets-of-radiobuttons-shiny – Pete900 Feb 07 '16 at 10:11

1 Answers1

0

It turns out that:

"it's because a JS error occurs if the choices argument isn't a character vector."

I have posted an issue on Shiny's Github:

https://github.com/rstudio/shiny/issues/1093

This can be resolved by:

To fix your problem, you can either convert your choices to characters using as.character or set selected to a random string such as "".

See:

Update two sets of radiobuttons - shiny

Community
  • 1
  • 1
Pete900
  • 2,016
  • 1
  • 21
  • 44