Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 反应式单选按钮取决于其他单选按钮的选择_R_Shiny - Fatal编程技术网

R 反应式单选按钮取决于其他单选按钮的选择

R 反应式单选按钮取决于其他单选按钮的选择,r,shiny,R,Shiny,我有一个闪亮的应用程序,我想用一组无线电广播按钮来确定另一组无线电广播按钮的选择向量。我尝试过使用conditionalPanel,但我不知道如何将不同的radioBuottons绑定到单个输出条目(如果可能的话)。 因此,我为第二组radioBuottons定义了一个列表,并尝试根据第一组radioBuottons的选择来选择它们 下面是一个代码示例: ui.r: library(shiny) secondInput <- list( "a" = c("one", "two", "

我有一个闪亮的
应用程序,我想用一组
无线电广播按钮
来确定另一组
无线电广播按钮
的选择向量。我尝试过使用
conditionalPanel
,但我不知道如何将不同的
radioBuottons
绑定到单个输出条目(如果可能的话)。 因此,我为第二组
radioBuottons
定义了一个列表,并尝试根据第一组
radioBuottons
的选择来选择它们

下面是一个代码示例:

ui.r:

library(shiny)

secondInput <- list(
  "a" = c("one", "two", "three"),
  "b" = c("four", "five")
)

shinyUI(fluidPage(
  titlePanel("Test reactive RadioButtons"),
    column(4,
      radioButtons("input1", label = "1st input", choices = c("a","b"))),
    column(4,
      radioButtons("input2", label = "2nd input depend on 1st input", choices = secondInput[[input$input1]])),
    column(4,
      textOutput("IN1"))
))
library(shiny)

shinyServer(function(input, output) {
  out <- reactive(input$input1)
  output$IN1 <- renderText(out())
})
库(闪亮)

secondInput您生成的
单选按钮
不是被动的。如果要使用一个输入使另一个输入或输出依赖于它,则必须在服务器中的反应式表达式中为其构建逻辑。您可以为此使用
uiOutput
renderUI
。下面给出了一个工作示例,希望对您有所帮助

library(shiny)

secondInput <- list(
  "a" = c("one", "two", "three"),
  "b" = c("four", "five")
)

ui <- fluidPage(
  radioButtons("input1", label = "1st input", choices = c("a","b")),
  uiOutput('radiobuttons2')
)

server <- function(input, output, session) {
  output$radiobuttons2 <- renderUI({
    radioButtons('input2',label='2nd input', choices = secondInput[[input$input1]])
  })

}

shinyApp(ui, server)
库(闪亮)

secondInput我认为
updateradiobutions
会更好,因为每次
input1
更改时都不需要重新创建小部件

library(shiny)

secondInput <- list(
  "a" = c("one", "two", "three"),
  "b" = c("four", "five")
)

ui <- fluidPage(
  titlePanel("Test reactive RadioButtons"),
  column(4,radioButtons("input1", label = "1st input", choices = c("a","b"))),
  column(4,radioButtons("input2", label = "2nd input depend on 1st input", choices = "")),
  column(4,textOutput("IN1"))
)

# Define server logic
server <- function(input, output, session) {

  observeEvent(input$input1,{
    updateRadioButtons(session,"input2",choices = secondInput[[input$input1]])
  })

  out <- reactive(input$input1)
  output$IN1 <- renderText(out())
}

# Run the application 
shinyApp(ui = ui, server = server)
库(闪亮)

第二次输入工作完美!非常感谢。这真是太好了!