繁体   English   中英

R shiny:基于checkboxgroupinput的子集数据

[英]R shiny: Subset data based on checkboxgroupinput

我想根据复选框输入动态选择的列来设置我的数据。 有什么方法可以让我的输入文件在我的代码中全局可用,以便可以轻松执行进一步的操作。

以下是我的代码:

Server.R

    library(shiny)

    shinyServer(function(input, output) {

    dInput <- reactive({
       inFile <- input$file1
       if (is.null(inFile))
       return(NULL)

    finput <- read.csv(inFile$datapath, header=TRUE, sep=',',quote="'")
    fheaders <- names(finput) 
    return(fheaders)
    })


    output$choose_columns <- renderUI({
                       checkboxGroupInput("columns", "Choose columns", 
                       choices  = dInput(),
                       selected = NULL)
    })


  # to observe in environment which columns are selected
  observe({ print(input$columns) })


  output$data_table <- renderTable({
    # If missing input, return to avoid error later in function
    if(is.null(input$file1))
      return()

    # Get the data set
    dat <- get(input$file1)

    # Keep the selected columns
    dat <- dat[, input$columns, drop = FALSE]

    # Return first 20 rows
    head(dat, 20)
  })


})

ui.R

library(shiny)

# Define UI for application 
shinyUI(fluidPage(

  # Application title
  titlePanel("Subset a table"),


  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      uiOutput("choose_columns")
      ),

    mainPanel(
      tableOutput("data_table")
    )
  )
))

我在显示tableOutput(“data_table”)时遇到以下错误

Error : invalid first argument    

我认为您需要对dInput进行reactive ,并在过滤后的数据上再添加一个。 然后,您可以使用data_table() (使用() )进行进一步操作。 下面的(单个文件)代码在我的机器上正常工作。 我也删除了observe (无用)并更改了dInput返回的内容(实际文件而不是colnames)。

    library(shiny)

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

  dInput <- reactive({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
    else
      return(read.csv(inFile$datapath, header=TRUE, sep=',',quote="'"))
  })


  output$choose_columns <- renderUI({
    cn <- colnames(dInput())
    selectInput("columns", "Choose columns", 
           choices  = cn,
           selected = cn,
           size=10,
           multiple=TRUE, selectize=FALSE)
  })

  data_table <- reactive({
    # If missing input, return to avoid error later in function
    if(is.null(input$file1))
      return(NULL)

    # Get the data set
    dat <- dInput()

    # Keep the selected columns
    dat[, input$columns, drop = FALSE]
  })

  output$data_table <- renderTable(data_table())

})


# Define UI for application 
ui <- shinyUI(fluidPage(

  # Application title
  titlePanel("Subset a table"),


  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),

      uiOutput("choose_columns")
    ),

    mainPanel(
      h3("Filtered table"),
      tableOutput("data_table")
    )
  )
))

shinyApp(ui, server)

那是你要的吗?

暂无
暂无

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

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