r语言 - 更新两组单选按钮 - 闪亮



我昨天问了这个问题(被动更新两组单选按钮 - 闪亮),但也许它太混乱而无法得到回应。我已经精简了这个问题:为什么我不能让两组单选按钮被动更新:

服务器。R:

# 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){
# Get Lengths from user input   
 a <- eventReactive(input$do, {
       subset(dd, Wafer %in% input$wafer, select = Length) 
 })
# Get Widths from user input   
 b <- eventReactive(input$do, {
   subset(dd, Wafer %in% input$wafer, select = Width) 
 })
#Observe and update first set of radiobuttons based on a(). Does
#render   
  observe({ 
    z <- a()
    updateRadioButtons(session, "length", choices = unique(z$Length), inline=TRUE)
  })
#Observe and update second set of radiobuttons based on b(). Does 
#not render     
  observe({ 
    z <- b()
    updateRadioButtons(session, "width", choices = unique(z$Width), inline=TRUE)
  })
  output$l <- renderDataTable({ a() })
  output$w <- renderDataTable({ b() })  
})

用户界面。R:

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="l"),
      dataTableOutput(outputId="w")
    )))
)

在上面,我只能得到一组单选按钮来响应("长度")。但是,如果我注释掉长度observe,宽度可以工作,所以我的代码必须单独正常。也许我错过了一些简单的东西?

这可能是updateRadioButtons函数的错误。如果未设置selected,则将其替换为第一个选项。我想如果选择列表是数字,这会导致错误。

要解决此问题,您可以使用as.character将选择转换为字符,也可以将selected设置为随机字符串,例如 ""

使用 as.character 可能会更好,因为您可以自动选择第一个选择。

最新更新