更新两组radiobuttons - 闪亮

时间:2016-01-28 08:47:22

标签: r shiny

昨天我问过这个问题(Update two sets of radiobuttons reactively - shiny),但也许它太乱了,无法得到答复。我已经删除了这个问题:为什么我无法获得两套无线电按钮以进行反应性更新:

server.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() })  
})

ui.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")

    )))
)

在上面,我只能得到一组无线电按钮(&#34;长度&#34;)。但是,如果我注释掉长度observe,则宽度可以正常工作,因此我的代码必须单独处理。也许我错过了一些简单的东西?

1 个答案:

答案 0 :(得分:3)

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

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

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

相关问题