2016-03-26 97 views
3

我想根据通过复选框输入动态选择的列来设置我的数据。 有没有什么方法可以让我的输入文件在我的代码 全球可用,以便进一步的操作可以很容易地执行。R shiny:基于复选框组输入的子集数据

以下是我的代码:

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  
+0

在你的'head(dat,20)'声明前加一个'print(input $ columns)'来查看是否选择了正确的列 – SymbolixAU

回答

1

我想哟你需要在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) 

是你想要的吗?

+0

试过这个,但输出没有改变。 –

+0

正是我需要的。非常感谢 –

相关问题