简体   繁体   中英

R shiny select variable based on checkboxGroupInput

I am using R shiny to develop a interactive analysis tool. Now I want to do classification tree based on variables check in checkboxGroupInput. How can I select that subset of data? THX!

UI:

       dateInput("date","Enter date:",value = date),
       checkboxGroupInput("variable", "Variable:",
                         choices = names ,selected = names
        )

server I tried, but doesn't work:

 dataall <-  reactive({
     filename <- paste0("dataall_'", input$date, "'.RData")
     load(filename)
     feature.test[,names(feature.test) %in% input$variable]
   })

feature.test is data in loaded file.

Hard to understand what you want since you don't subset the file you load. What is feature.test ?

Here is a simple example to how to subset a data frame using an input and shiny reactivity :

shiny::runApp(list(
  ui = basicPage(
    selectInput("specy", "Specy", choices = levels(iris$Species)),
    tableOutput("content")
  ),
  server = function(input, output, session) {
    output$content <- renderTable({
      iris[iris$Species == input$specy, ]
    })
  }
))

EDIT ## : Subset by column :

shiny::runApp(list(
  ui = pageWithSidebar(
    headerPanel("Example"),
    sidebarPanel(
      checkboxGroupInput("variable", "Variable:", choices = names(iris))
    ),
    mainPanel(
     tableOutput("content")
    )
  ),
  server = function(input, output, session) {
    output$content <- renderTable({
      if(is.null(input$variable))
        return()

      iris[input$variable]
    })
  }
))

"variable" is supposed to be "date" since this is the control that you are referencing in the UI part, as in:

checkboxGroupInput( "date", "Variable:",
                 choices = names ,selected = names
)

For data.table you need to add a ,with=FALSE or use a temporary variable in the server code:

# Load libraries
library(shiny)
library(data.table)

# Copy dataset
irisDT=copy(iris)
setDT(irisDT)

# Shiny app
shiny::runApp(list(
    # UI
    ui = pageWithSidebar(
        headerPanel("Example"),
        sidebarPanel(
            checkboxGroupInput("variable", "Variable:", choices = names(iris))
        ),
        mainPanel(
            tableOutput("content")
        )
    ),

    # Server
    server = function(input, output, session) {
        output$content <- renderTable({
            if(is.null(input$variable))
                return()

            # iris[input$variable]               # data.frame
            irisDT[, input$variable, with=FALSE] # data.table
            # Alternatively:
            # tmp <- input$variable
            # irisDT[, ..tmp]
        })
    }
))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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