反应性地更新两组单选按钮 - 闪亮
Update two sets of radiobuttons reactively - shiny
我已阅读此 (How do I make the choices 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)
})
})
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")
)
)
)
)
在上面,单选按钮会更新,但仅针对代码顺序的第一组按钮,即上面 "length" 更新,但 "width" 不更新。如果我反过来写,"width" 会更新,但 "length" 不会。我是否需要定义一个新会话?
事实证明:
"it's because a JS error occurs if the choices argument isn't a
character vector."
我在 Shiny 的 Github 上发布了一个问题:
https://github.com/rstudio/shiny/issues/1093
这可以通过以下方式解决:
To fix your problem, you can either convert your choices to characters
using as.character or set selected to a random string such as "".
参见:
我已阅读此 (How do I make the choices 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)
})
})
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")
)
)
)
)
在上面,单选按钮会更新,但仅针对代码顺序的第一组按钮,即上面 "length" 更新,但 "width" 不更新。如果我反过来写,"width" 会更新,但 "length" 不会。我是否需要定义一个新会话?
事实证明:
"it's because a JS error occurs if the choices argument isn't a character vector."
我在 Shiny 的 Github 上发布了一个问题:
https://github.com/rstudio/shiny/issues/1093
这可以通过以下方式解决:
To fix your problem, you can either convert your choices to characters using as.character or set selected to a random string such as "".
参见: