2016-01-27 47 views
0

我已阅读此(How do I make the choices in radioButtons reactive in Shiny?),它向我展示了如何以被动方式更新radioButton。但是,当我尝试更新来自相同数据的两组按钮时,只有一组呈现。例如:反应式更新两套单选按钮 - 闪亮

服务器:

# 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") 
    ) 
) 
) 
) 

在上文中,单选按钮做更新,但是仅针对第一组按钮中的上述的“长度”的更新,即代码顺序,但“宽度”不。如果我反向写入它们,“宽度”更新但“长度”不更新。我是否需要定义一个新的会话?

+0

问题在这里得到解决:http://stackoverflow.com/questions/35056668/update-two-sets-of-radiobuttons-shiny – Pete900

回答