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

如何使用R合并不同csv文件的选定列,r,shiny-server,shiny,R,Shiny Server,Shiny,用例: 我有10个不同的csv文件。每个文件具有相同的行数&每个文件中共有两列。我想合并这些文件,所以为此我写了一个应用程序。 例如,我从第一个文件中选择了2个公共列和3个其他列。然后我选择第二个文件并选择其他两列(跳过我已经从第一个文件中选择的公共列) 步骤: 1) Select files from list of 10 files 2) This will show columns of that file 3) Dynamic button to add multiple files 4

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

步骤:

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")
                      ))
库(闪亮)

filenames Hi@vijay_Shinde,我想知道你是否能解决这个问题,我也在尝试合并一个目录中多个文件的列并显示它们,但我无法显示这些列。谢谢!我能够根据关键列合并两个csv文件的所有列。你能分享你的解决方案吗,因为我正在努力让它运行!T汉克斯!到目前为止有任何解决方案!!!
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)
  })
  })