我已经阅读了这篇文章(How do I make the selections in radioButtons reactive in Shiny?),它向我展示了如何以反应方式更新radioButton
s。但是,当我尝试从相同的数据更新两组按钮时,只有一组呈现。例子:
服务器:
# 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)
})
})
用户界面:
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")
)
)
)
)
在上面,单选按钮确实会更新,但仅按代码顺序更新第一组按钮,即“长度”更新但“宽度”不更新。如果我把它们反过来写,“宽度”会更新,但“长度”不会。我是否需要定义一个新会话?