R shiny - 基于另一个 CheckboxGroupInput 更新 CheckboxGroupInput

R shiny - updating CheckboxGroupInput based on another CheckboxGroupInput

我只想在用户取消选中固定变量中的变量时,将 'Fixed Variables' 的变量包含在 'Random Variables' 中。这是图像 http://imgur.com/zUXEJwg。没有错误消息,只是每当我尝试取消选中任何变量时,它都不让我这样做。 谁能帮我解决这个问题?

shinyServer(function(input, output,clientData, session) {
   observe({

     data<-reactive({
        file1<-input$file
        if(is.null(file1)){return()}
        read.table(file=file1$datapath,    sep=input$sep,header=input$header,stringsAsFactors=input$stringsAsFactors)
                  })

        colnames<-names(data())
        updateCheckboxGroupInput(session, "Fixed", choices = colnames, selected=colnames) 

        inputVar<-reactive({
              Fixedvar<-input$Fixed
              if(is.null(Fixedvar)){return()}
              subset<-data()[,!(colnames %in% Fixedvar)]
               col<-names(subset)
                col
                             })

       updateCheckboxGroupInput(session, "Random", choices = inputVar())
               })})



shinyUI(fluidPage(
        sidebarLayout(
              sidebarPanel( 

       fileInput("file","Please upload your file"),
       checkboxInput(inputId='header', label='header', value=TRUE),
       checkboxInput(inputId="stringsAsFactors", label="stringsAsFactors", value=FALSE),
        radioButtons(inputId='sep',label='Separator', choices=c(Comma=',',  Semicolon=';',Tab='\t',Space=''), selected=','),
        fluidRow(column(6, wellPanel(
        checkboxGroupInput("Fixed", 
                       label = "Fixed Variables","") 
           )),

        column(6, wellPanel(
        checkboxGroupInput("Random", label = "Random Variables","") 
         ) ))),


             mainPanel()
            )))

您可能需要观察员。每当你想评估反应式表达式,但不需要 return 值时,你必须使用观察者。一些类似观察者的例子是 renderXXX 函数。更多信息 here.

试试这个

observe({
    colnames<-names(data())
    updateCheckboxGroupInput(session, "Fixed", choices = colnames, selected=colnames)
})

# ...

observe({
    iv <- inputVar()
    updateCheckboxGroupInput(session, "Random", choices = iv)
})

问题是您只使用了一个 observe。只要其中的任何反应元素发生变化,它就会运行 - 在您的情况下就是任何东西。具体来说,当您更改 "Fixed" 复选框组时,这部分观察也会再次运行:

colnames<-names(data())
updateCheckboxGroupInput(session, "Fixed", choices = colnames, selected=colnames)

由于您的数据没有改变,它始终只是将您的选择和选择的选项设置为 colnames。为了解决这个问题,你需要让你的服务器更加模块化,使用独立的观察者和反应元素。这是适合您的 server.R:

的布局
library(shiny)

shinyServer(function(input, output, session) {

  data <- reactive({

    file1 <- input$file
    if(is.null(file1)){ return(mtcars) } # Default data for testing

    read.table(file = file1$datapath, sep = input$sep, header = input$header,
               stringsAsFactors = input$stringsAsFactors)

  })

  colnames <- reactive({ names(data()) })

  observeEvent(data(), {

    updateCheckboxGroupInput(session, "Fixed",
      choices = colnames(), selected = colnames()) 

  })

  inputVar <- reactive({

    Fixedvar <- input$Fixed

    if (setequal(colnames(), Fixedvar)) {
      # If sets are equal, return an empty string
      return("")
    } else {
      # Return elements that are only in colnames
      setdiff(colnames(), Fixedvar)
    }

  })

  observeEvent(inputVar(), {

    updateCheckboxGroupInput(session, "Random", choices = inputVar())

  })

})

我也稍微更改了 inputVar。当 none 个框在 checkboxGroupInput 中被选中时,它 returns NULL。因此,当 input$FixedNULL 时,您实际上想要将 "Random" 中的选项更新为 all 列。

但仍然存在一个问题:每当您选中或取消选中第一个输入中的某个框时,第二个输入框就会更新。这意味着它的选择也会更新——所有未选中的内容。不过,这是一个不同的问题;我认为您可以使用 reactiveValues 来存储 "Random" 的先前值并在更新输入选项时将这些值用作初始值。