如何使用R shiny合并不同csv文件的选定列

时间:2016-05-11 09:26:13

标签: r shiny-server shiny

用例: 我有10个不同的csv文件。每个文件具有相同的行数和数量。每个文件中共有两列。我想合并这些文件,所以为此我写了R闪亮的应用程序。 例如我选择了2个常见的列&第一个文件中的其他3列。然后我选择第二个文件&选择其他2列(跳过我从第一个文件中选择的公共列。

步骤:

1) Select files from list of 10 files
2) This will show columns of that file
3) Dynamic button to add multiple files
4) Then repeat step 1 to 3 till you finish your files.

我的代码正在努力从第一个文件中选择列然后添加新文件,但后来它没有显示下一个文件列。

这是我的源代码:

ui.R

library(shiny)

filenames <<- c("file one","file2")

shinyUI(fluidPage(
sidebarLayout(
                      sidebarPanel(
                       uiOutput("choose_dataset"),
                        tags$hr(),
                        uiOutput("choose_columns"),
                        actionButton("add_btn", "Add"),
                        actionButton("rm_btn", "Remove"),
                        textOutput("counter"),
                        actionButton("merge_btn","Merge")
                      ),
                      # Show a plot of the generated distribution
                      mainPanel(
                        uiOutput("textbox_ui")
                      ))

Server.R

library(shiny)
library(stringr)
library(dplyr)
    shinyServer(function(input, output) {

  timingalign <- read.csv("<Path>file1.csv",header = T,sep = ",")
  mobility <- read.csv("<path>file2.csv",sep = ",")
  data_sets1 <- c("timingalign","mobility")

  output$filetable <- renderTable({
    filedata()
  })
  # Track the number of input boxes to render
  counter <- reactiveValues(n = 0)
  observeEvent(input$add_btn, {counter$n <- counter$n + 1})
  observeEvent(input$rm_btn, {
    if (counter$n > 0) counter$n <- counter$n - 1
  })

  output$counter <- renderPrint(print(counter$n))
  textboxes <- reactive({
    n <- counter$n
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()

    # Get the data set with the appropriate name
    dat <- get(input$dataset)
    colnames <- names(dat)

    if (n > 0) {
      lapply(seq_len(n), function(i) {
        sidebarPanel(
          selectInput("dataset", "Select a file", as.list(data_sets1)),
          tags$hr(),
          # Create the checkboxes and select them all by default
          checkboxGroupInput("columns", "Choose columns", 
                             choices  = colnames)
        )

      })
    }

  })

  output$textbox_ui <- renderUI({ textboxes() })
  # Output the data
  output$data_table <- renderTable({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()

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

    # Make sure columns are correct for data set (when data set changes, the
    # columns will initially be for the previous data set)
    if (is.null(input$columns) || !(input$columns %in% names(dat)))
      return()

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

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


  # Drop-down selection box for which data set
  output$choose_dataset <- renderUI({
    selectInput("dataset", "Select a file", as.list(data_sets1))
  })

  # Check boxes
  output$choose_columns <- renderUI({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()

    # Get the data set with the appropriate name
    dat <- get(input$dataset)
    colnames <- names(dat)

    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames)
  })
  })

指导我如何解决这个问题。

0 个答案:

没有答案