用于向多个文件添加列的闪亮应用程序

时间:2018-02-22 07:44:25

标签: r shiny

我是创建闪亮应用的新手。我正在尝试集成我编写的脚本,用于将列添加到多个csv文件中。请参阅下面的代码。当我运行我的应用程序时,我的文件不变。我知道我的pre.process函数可以独立工作。

library(shiny); library(shinyFiles)

ui <- fluidPage(
  titlePanel("Add Columns"),

  sidebarLayout(
    sidebarPanel(
      shinyDirButton('directory','Select Directory:','Select Directory:'),
      br(),
      br(),
      actionButton('submit','Submit')
    ),
    mainPanel(
      h4("choosen Directory:"),
      verbatimTextOutput("directory"), br()
    )
  )
)

pre.process = function(input_dir){
  library(dplyr)

  setwd(input_dir) # sets up working directory

  file_names = dir(input_dir, pattern = ".csv") # populates file names

  add_col = function(one_file){
    temp = read.csv(one_file, header = T)
    temp = mutate(temp,Instrument = sapply(strsplit(one_file,"_"),`[`,1))
    temp = mutate(temp,Lot = sapply(strsplit(one_file,"_"),`[`,3))
    temp = mutate(temp,Condition = sapply(strsplit(one_file,"_"),`[`,5))
    ifelse(!dir.exists(file.path(input_dir, "archive")), dir.create(file.path(input_dir, "archive")), FALSE)
    write.csv(temp, file = sub("\\.csv","_new\\.csv",one_file), append = FALSE, quote = FALSE,
              sep = ",", row.names = FALSE)
    file.copy(one_file,".//archive")
    file.remove(one_file)

  }
  x=lapply(file_names,add_col)

}

server <- function(input, output, session) {
  shinyDirChoose(input,'directory',roots=c(home = '..'))

  observeEvent(input$submit,{
    directory = parseDirPath(roots = c(home = '..'), input$directory)
    output$directory <- renderPrint(input$directory)
    pre.process(directory)

  })

}

shinyApp(ui = ui, server = server)

0 个答案:

没有答案