【问题标题】:Update two sets of radiobuttons reactively - shiny响应式更新两组单选按钮 - 闪亮
【发布时间】:2016-05-04 14:16:01
【问题描述】:

我已阅读此 (How do I make the choices 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")
    )
  )
)
)

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

【问题讨论】:

标签: r shiny shiny-reactivity


【解决方案1】:

事实证明:

"这是因为如果选择参数不是 字符向量。”

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

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

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

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

见:

Update two sets of radiobuttons - shiny

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 2021-05-01
    • 2021-12-13
    • 2016-07-08
    • 2021-01-26
    • 2019-08-02
    • 2015-09-19
    相关资源
    最近更新 更多