反应性更新两组radiobuttons - 有光泽

时间:2016-01-27 14:53:01

标签: r shiny shiny-reactivity

我已经阅读了这篇文章(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")
    )
  )
)
)

在上面,radiobutton确实更新,但仅按代码的顺序更新第一组按钮,即&#34;长度&#34;更新,但&#34;宽度&#34;没有按&#39;吨。如果我反向写它们,&#34;宽度&#34;更新,但&#34;长度&#34;没有按&#39;吨。我是否需要定义新会话?

1 个答案:

答案 0 :(得分:0)

事实证明:

  

&#34;它是因为如果choice参数不是一个JS错误就会发生   字符向量。&#34;

我在Shiny&#39; Github上发布了一个问题:

https://github.com/rstudio/shiny/issues/1093

这可以通过以下方式解决:

  

要解决您的问题,您可以将选择转换为字符   使用as.character或set选择随机字符串,例如&#34;&#34;。

请参阅:

Update two sets of radiobuttons - shiny