2016-01-27 49 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

回答