简体   繁体   English

如何从 input$.. 或 output$.. 获取路径并将其用于 list.files 然后复制/剪切文件

[英]How to get the path from input$.. or output$.. and use it to list.files and then copy/cut the files

I am learing Shiny.我正在学习 Shiny。 I want to make a simple app that allows for dynamic paths that the user enters.我想制作一个简单的应用程序,允许用户输入动态路径。 The app should then list csv files in folder A and then copy them from folder A to folder B (the working directory).然后应用程序应列出文件夹 A 中的 csv 文件,然后将它们从文件夹 A 复制到文件夹 B(工作目录)。 Then the app does some operations in folder B using an external exe program.然后应用程序使用外部 exe 程序在文件夹 B 中执行一些操作。 Afterwards the folder will cut the results files (.txt) from B and copies them into A.之后,该文件夹将从 B 中剪切结果文件 (.txt) 并将它们复制到 A。

The structure of my app is as follows ( I have also attached a picture).我的应用程序的结构如下(我还附上了一张图片)。 the problem is explained in the comments in the code.该问题在代码中的注释中进行了解释。 在此处输入图像描述

library(shiny)   

ui<-fluidPage(
  textInput("prg","Program",getwd()),
  verbatimTextOutput("prg"),
  textInput("prj","Project","Project"),
  verbatimTextOutput("prj")
)

server<-function(input, output,session) {
  output$prg=renderText(input$prg)
  renderPrint(output$prg)

  output$prj=renderText(paste0(input$prg,"/",input$prj))

  #This is where my challenge is
  #I want to 
  #list.files(path=path-shown-in-text-box-Project,pattern=".csv")
  #Then i want to copy csv files from A to B as described above and run the following program

  #This works
observeEvent(input$run,
                   {
                     system("my.exe") #exe not shared
                   })
#Finally I want to cut and paste the results (.txt) from B back into A

}

shinyApp(ui,server)

I want to list.files(path=path-shown-in-text-box-Project,pattern=".csv")我想列出.files(path=path-shown-in-text-box-Project,pattern=".csv")

Here's code you can use to browse any directory for a particular CSV file, read that file and display its contents.以下代码可用于浏览特定 CSV 文件的任何目录,读取该文件并显示其内容。

library(shiny)

# Define UI
ui <- pageWithSidebar(

  # App title ----
  headerPanel("Open a File and Show Contents"),

  # Sidebar panel for inputs ----
  sidebarPanel(
    label="Data Source",fileInput("fileName", "File Name",accept=c(".csv"))),

  # Main panel for displaying outputs ----
  mainPanel(
    tableOutput(outputId = "table")
  )
)

# Define server logic
server <- function(input, output) {

  inputData <- reactive ({
    if (is.null(input$fileName)) return(NULL)
    inFile <- input$fileName
    conInFile <- file(inFile$datapath,open='read')
    inData <- read.csv(conInFile,stringsAsFactors = FALSE)
    close (conInFile)
  return (inData)
  })

  output$table <- renderTable ({
    inData <- inputData()
    if (length(inData) > 0) inData
  })
}

shinyApp(ui, server)

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

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