Update two sets of radiobuttons reactively - shiny

坚强是说给别人听的谎言 提交于 2019-12-12 03:54:52

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/35040579/update-two-sets-of-radiobuttons-reactively-shiny

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