0

我已经阅读了这篇文章(How do I make the selections in radioButtons reactive in Shiny?),它向我展示了如何以反应方式更新radioButtons。但是,当我尝试从相同的数据更新两组按钮时,只有一组呈现。例子:

服务器:

# 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")
    )
  )
)
)

在上面,单选按钮确实会更新,但仅按代码顺序更新第一组按钮,即“长度”更新但“宽度”不更新。如果我把它们反过来写,“宽度”会更新,但“长度”不会。我是否需要定义一个新会话?

4

1 回答 1

0

事实证明:

“这是因为如果选择参数不是字符向量,则会发生 JS 错误。”

我在 Shiny 的 Github 上发布了一个问题:

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

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

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

看:

更新两组单选按钮 - 闪亮

于 2016-02-07T10:16:21.367 回答