简体   繁体   中英

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. 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). Then the app does some operations in folder B using an external exe program. Afterwards the folder will cut the results files (.txt) from B and copies them into 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")

Here's code you can use to browse any directory for a particular CSV file, read that file and display its contents.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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