简体   繁体   English

R Shiny-保存来自单选按钮和复选框的组合输入

[英]R Shiny - Saving Combined Inputs from Radio Buttons and Checkboxes

I'm new to shiny (but not R) and am working to build an app where the user chooses specific rows from a table to export into a csv file. 我是新手(但不是R),并且正在构建一个应用程序,其中用户从表中选择特定行以导出到csv文件。 Reduced to its essentials, the table has two columns of factors, and one column of responses. 简而言之,该表包括两列因素和一列响应。

For example, 例如,

在此处输入图片说明

Originally, I had the app set to where the user had checkboxes on the sidebar based on the values in the dataFactor2 column that they could use to select which rows to export. 最初,我将应用程序设置为用户根据dataFactor2列中的值在边栏上具有复选框的位置,他们可以用来选择要导出的行。 (For example, they could select for export the table rows where dataFactor2 == A, G, and J.) However, that got unwieldy for large numbers of dataFactor1 and dataFactor2 levels. (例如,他们可以选择要导出的表行,其中dataFactor2 == A,G和J。)但是,这对于大量的dataFactor1和dataFactor2级别来说很难。

My idea was to add radio buttons for the levels of dataFactor1. 我的想法是为dataFactor1的级别添加单选按钮。 Then, if you selected the radio button for dataFactor1 == X, you would get checkboxes for A, B, and C, and the table would only show the rows where dataFactor1 == X. You'd select row A, B, or C, then you'd move on to Y and Z. At the end of the process, you'd still export the rows that corresponded to the selected levels of dataFactor2. 然后,如果您为dataFactor1 == X选择了单选按钮,则将获得A,B和C的复选框,并且该表仅显示dataFactor1 == X的行。您将选择A,B或B行C,然后转到Y和Z。在该过程结束时,您仍将导出与所选dataFactor2级别相对应的行。

However, when I try to implement things this way, everything resets when I move from one level of dataFactor1 to the next. 但是,当我尝试以这种方式实现事物时,当我从一个级别的dataFactor1移到下一级时,所有设置都会重置。 Any suggestions for how to capture all the checked boxes? 关于如何捕获所有复选框的任何建议?

Sample code below for ui.R: 以下ui.R的示例代码:

#ui.R

library(shiny)

myFactor1<-c("X", "Y", "Z")
myFactor2<-c() #initialize

shinyUI(fluidPage(

  titlePanel("Sample"),

  # Sidebar with radio buttons and checkboxes
  sidebarLayout(
    sidebarPanel(width=2,

      #display radio button list of factor1 values
      radioButtons("factor1", 
                   label = "dataFactor1",
                   choices=myFactor1), #dynamically generate factor list

      #display checkboxes of factor2 values
      checkboxGroupInput("factor2",
                         label="dataFactor2",
                         choices=myFactor2) #dynamically generate list

    ),

    # Show factor2 selections and table
    mainPanel(

      textOutput("text2"),
      tableOutput("table2")


  )

)

))

and server.R: 和server.R:

#server.R
library(shiny)

shinyServer(function(input, output, session) {

  mydata<-data.frame(dataFactor1=c(rep("X", 3), rep("Y", 4), rep("Z", 5)),
                     dataFactor2=c("A", "B", "C", "D", "E", "F", "G", "H", 
                                   "I", "J", "K", "L"),
                     dataResponse=rnorm(12))

  observe({
        factor1Choice<-input$factor1

        myFactor2_list<-mydata$dataFactor2[mydata$dataFactor1==factor1Choice]

        updateCheckboxGroupInput(session, "factor2",
                                 choices = myFactor2_list)

        mydata2<-mydata[mydata$dataFactor1==factor1Choice,]

        output$table2<-renderTable(mydata2)

        observe({
          factor2Choice<-input$factor2
          output$text2<-renderText(factor2Choice)

        })

  })

})

Any suggestions for how to capture all the checked boxes? 关于如何捕获所有复选框的任何建议?

With regards to both answers here , you could do (quick&dirty): 至于两个答案在这里 ,你可以做(快速和肮脏的):

myFactor1<-c("X", "Y", "Z")
myFactor2<-c() #initialize

ui <- fluidPage(

  titlePanel("Sample"),

  # Sidebar with radio buttons and checkboxes
  sidebarLayout(
    sidebarPanel(width=2,

      #display radio button list of factor1 values
      radioButtons("factor1", 
                   label = "dataFactor1",
                   choices=myFactor1), #dynamically generate factor list

      #display checkboxes of factor2 values
      checkboxGroupInput("factor2",
                         label="dataFactor2",
                         choices=myFactor2) #dynamically generate list

    ),

    # Show factor2 selections and table
    mainPanel(

      textOutput("text2"),
      tableOutput("table2")


  )

)

)


server <- function(input, output, session) {

  mydata<-data.frame(dataFactor1=c(rep("X", 3), rep("Y", 4), rep("Z", 5)),
                     dataFactor2=c("A", "B", "C", "D", "E", "F", "G", "H", 
                                   "I", "J", "K", "L"),
                     dataResponse=rnorm(12))

  values <- reactiveValues(cb = with(mydata, setNames(rep(FALSE, nlevels(dataFactor2)), levels(dataFactor2))))  

  observeEvent(input$factor2, {
    myFactor2_list<-mydata$dataFactor2[mydata$dataFactor1==input$factor1]
    values$cb[myFactor2_list] <- myFactor2_list %in% input$factor2
  })

  observe({
        factor1Choice<-input$factor1

        myFactor2_list<-mydata$dataFactor2[mydata$dataFactor1==factor1Choice]

        updateCheckboxGroupInput(session, "factor2",
                                 choices = myFactor2_list,
                                 selected = levels(mydata$dataFactor2)[values$cb])

        mydata2<-mydata[mydata$dataFactor1==factor1Choice,]

        output$table2<-renderTable(mydata2)

        observe({
          factor2Choice<-input$factor2
          output$text2<-renderText(factor2Choice)

        })

  })

}
shinyApp(ui, server)

The general setup is another issue - I found these shiny sessions quite interesting and helpful. 常规设置是另一个问题-我发现这些闪亮的会话非常有趣并且很有帮助。

Here a possible solution using a list to store the selections. 这是使用列表存储选择的可能解决方案。 All the selected checkboxes are recovered using unlist . 使用unlist恢复所有选中的复选框。

library(shiny)

myFactor1<-c("X", "Y", "Z")

ui <- shinyUI(fluidPage(
  titlePanel("Sample"),
  # Sidebar with radio buttons and checkboxes
  sidebarLayout(
    sidebarPanel(width=2,
      #display radio button list of factor1 values
      radioButtons("factor1", 
                   label = "dataFactor1",
                   choices=myFactor1), #dynamically generate factor list
      #display checkboxes of factor2 values
      checkboxGroupInput("factor2", label="dataFactor2")
    ),
    # Show factor2 selections and table
    mainPanel(
      textOutput("text2"),
      tableOutput("table2"),
      p("All selections:"),
      textOutput("text3")
    )
  )
))

server <- shinyServer(function(input, output, session) {

  mydata<-data.frame(dataFactor1=c(rep("X", 3), rep("Y", 4), rep("Z", 5)),
                     dataFactor2=c("A", "B", "C", "D", "E", "F", "G", "H", 
                                   "I", "J", "K", "L"),
                     dataResponse=rnorm(12))

  # to store selections
  mySelection <- list()

  observeEvent(input$factor1, {
    factor1Choice<-input$factor1
    myFactor2_list<-mydata$dataFactor2[mydata$dataFactor1==factor1Choice]

    updateCheckboxGroupInput(session, "factor2",
                             choices = myFactor2_list, 
                             # update with stored selections
                             selected = mySelection[[input$factor1]])

    mydata2<-mydata[mydata$dataFactor1==factor1Choice,]

    output$table2<-renderTable(mydata2)

  })

  observeEvent(input$factor2, {
    # backup the selection
    mySelection[[input$factor1]] <<- input$factor2
    output$text2 <- renderText(mySelection[[input$factor1]])    
    # get all selections
    output$text3 <- renderText(unlist(mySelection))    
  })

})

shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM