How to append file names entered through an input box to the rest of the path

rstudio

#1

Hi,

The first script, please see the first script below, works and produces the desired output. After the first script there is an app that calls another script that is identical to the first. Please see app bellow first script and second script called by app. The only difference between the single script and the one called by the application is that in the single script the file names are defined in the script while in the script called by the app, the file names are input by means of a text box. the script run by the application does not produce the expected result. I feel that the reason I am not getting the filename attached to the path has to do with timing. What I see is that the appending of the filename to the path is not happening because that piece of code is executed out of order (before the names are input), but I do not know how to handle time. Could you please take a look and tell me what you think? By the way, I commented the statement that read files so that you don't have to do that.

Thanks

list1 <- list("JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSMs.csv", "JF_160426_Dep2Plas_ctryp_Gpep_SIDtargFULLPSMs.csv",
           "A160824_JF_udep_tryp_Hi_SIDdda_FULL_NewParsePSMs.csv", "A150413_JF_GPeps_SIDtarg_GPstdMix_Tryp_2runs_v3PSMs.csv")
list2 <- list("JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSMs.csv", "JF_160426_Dep2Plas_ctryp_Gpep_SIDtargFULLPSMs.csv",
              "A160824_JF_udep_tryp_Hi_SIDdda_FULL_NewParsePSMs.csv")
list3 <- list("JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSMs.csv", "JF_160426_Dep2Plas_ctryp_Gpep_SIDtargFULLPSMs.csv")

lc <- list(list1, list2, list3)
dataFiles <- NULL
first_path <- NULL
new_path <- NULL
content <- NULL

# Length of list
fileListLength <- length(lc)
new_dataFns <- list("name1", "name2", "name3")
file_content <- NULL


for(i in 1:length(lc)){
  for(j in 1:length(lc[[i]])){
    if(!is.null(lc[[i]])){
     
      first_path[[j]] <- paste(getwd(), "/", lc[[i]][j], sep = "")
    #  file_content[[j]] <- read.csv(file = first_path[[i]], header = TRUE, sep = ",")
      for(k in 1:length(new_dataFns)){
      if(k == i){
         new_path[[j]] <- paste(getwd(),"/", i, new_dataFns[k], ".csv", sep = "")
         print (new_path[[j]])
      }
      }
    #  write.table(file_content[[j]], file = new_path[[j]],  append = TRUE, col.names = FALSE)
    }
  } 
}


OUTPUT OF FIRST SCRIPT
[1] "/home/giuseppa/Development/glycoPipeApp/1name1.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/1name1.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/1name1.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/1name1.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/2name2.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/2name2.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/2name2.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/3name3.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/3name3.csv
library(shiny)
library(shinyjs)
#> 
#> Attaching package: 'shinyjs'
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
#> The following objects are masked from 'package:methods':
#> 
#>     removeClass, show

ui <- fluidPage(
  selectInput("combinefiles", label = h5(strong("Do you wish to combine any data files, and compare the combined data sets? (Y/N) ")),
              choices = c("", "Y", "N"),selected = NULL),
  verbatimTextOutput("combiningFiles"),
  verbatimTextOutput("combineChosen"),
  #verbatimTextOutput("filesToCombine"),
  useShinyjs(),
  conditionalPanel(
    condition = "output.toCombine > '0'",
    selectInput(inputId = "select",label = h5(strong("Please select from the list")), choices = c(Chose = "", list.files("~/Development/glycoPipeApp")), multiple = TRUE, selectize = TRUE)
  ),
  conditionalPanel(
    condition = "output.displayAddButton > '0'",
    actionButton('add','Add')
  ),
  verbatimTextOutput("samelist"),
  conditionalPanel(
    condition = "output.displayAddButton == 1",
    actionButton("sBtn", "Press the save button to end")
  ),
  
  conditionalPanel(
    condition = "output.displayTheSaveButton  > '0'",
    textInput("textbox", h5(strong("Please enter name/s to designate combined set/s separated by comma")))),
  strong(verbatimTextOutput("list")),
  verbatimTextOutput("caption")
  

)

#selections = NULL,

glycoPipe <- function(response = NULL, fOfData = NULL, combineResult = NULL, listContents = NULL, vals = NULL){

enteredValue = NULL
nameList = NULL
answer = NULL
fileChoice = NULL
combination = NULL
combinations = NULL
comb = NULL
nameListSize = NULL
choseDataFiles = NULL

if(!is.null(response)){
  answer = response
}
if(!is.null(fOfData)){
  fileChoice = fOfData
}

if(!is.null(combineResult)){
  combination = combineResult
}

if(!is.null(listContents)){
  enteredValue = listContents
}

if(!is.null(vals)){
  nameList <- vals
}

glyCount1(answer, fileChoice, combination, enteredValue, nameList)
  
}
  
  server <- function(input, output, session){
    listContents = NULL
    
    source("Utilities/utilities.R")
    source("glycoPipeFunctions/glycoPipe_fcns.R")
    source("glyCount1.R")
    
    output$toCombine <- reactive({
      req(input$combinefiles)
      return(length(input$combinefiles))
    })
    
    
    outputOptions(output, "toCombine", suspendWhenHidden = FALSE)
    
        output$displayAddButton <- reactive({
        req(input$combinefiles)
        return(length(input$combinefiles))
     })
    
    outputOptions(output, "displayAddButton", suspendWhenHidden = FALSE)
     
    output$displayTheSaveButton <- reactive({
       req(input$sBtn)
       return(input$sBtn)
     })
    
    outputOptions(output, "displayTheSaveButton", suspendWhenHidden = FALSE)
    
      myValues <- reactiveValues()
      observe({
      if(input$add > 0){
         myValues$dList <- c(isolate(myValues$dList), isolate(list(input$select)))
       }
    })
    
    # #unlist(input$filescombine)
    output$samelist<-renderPrint({
       #listContents  <- list()
      
       listContents <- append(listContents, myValues$dList)
       print(listContents)
      if(input$sBtn > 0){
        numberOfSelectedSets <- glycoPipe(response = NULL, fOfData = NULL, combineResult = NULL , listContents)
        paste("Please enter", numberOfSelectedSets$nameListSize, "names to designate your", numberOfSelectedSets$nameListSize, "sets")
       }
     })
     
    
    VALUES <- list()
     observe({
       isolate({
         req(input$textbox)
         VALUES <- input$textbox
         VALUES <- append(VALUES, list(input$textbox))
         updateTextInput(session, inputId = "textbox", value = VALUES)
       
      })
     })
     
     output$caption <- renderPrint({
       vals <- list()
       vals <- append(vals, input$textbox)
       if(input$sBtn > 0){
         result <- glycoPipe(response = NULL, fOfData = NULL, combineResult = NULL, listContents, vals)
         #unlist(input$filescombine)
      }
     })
    
    session$allowReconnect(TRUE)
    
  }
  
  shinyApp(ui = ui, server = server)
glyCount1 <- function(answer = NULL, fileChoice = NULL, combination = NULL, enteredValue = NULL, nameList) {

# Length of list
lc <- enteredValue 
elementname <- nameList
choseDataFiles = TRUE
 fileLength <- length(lc)
# # These are the names that will be given to the 3 resulting files
first_path <- NULL
new_path <- NULL
#add the file names to the path and read and merge the contents of each list in the list of lists
new_dataFns = NULL
listOfNames = NULL



new_dataFns <- unlist(strsplit(as.character(elementname), ","))
print(length(new_dataFns))


content <- NULL
file_content <- NULL
dataFnsDir <- getwd()
first_path <- NULL
new_path = NULL

#> Error in eval(expr, envir, enclos): object 'new_dataFns' not found
file_content <- NULL
for(i in 1:length(lc)){
  for(j in 1:length(lc[[i]])){
    if(!is.null(lc[[i]])){
      
      first_path[[j]] <- paste(getwd(), "/", lc[[i]][j], sep = "")
    #  file_content[[j]] <- read.csv(file = first_path[[i]], header = TRUE, sep = ",")
      for(k in 1:length(new_dataFns)){
        if(k == i){
          new_path[[j]] <- paste(getwd(),"/", i, new_dataFns[k], ".csv", sep = "")
          print (new_path[[j]])
        }
      }
      #  write.table(file_content[[j]], file = new_path[[j]],  append = TRUE, col.names = FALSE)
    }
  } 
}

}

OUTPUT OF SCRIPT CALLED BY APPLICATION

[1] "/home/giuseppa/Development/glycoPipeApp/1.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/1.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/1.csv"
[1] "/home/giuseppa/Development/glycoPipeApp/1.csv"
[1] "Please enter names to designate your sets"


#2

Just to add some more debugging information, I have written some print statements; please see below. The list new_dataFns is a global variable, but is only seen by the first and second loop. After that, under the third and forth loop, the list new_dataFns is NULL. I made sure that I and did not save the environment. Also under global Options, Always save history even when not saving .RData is unchecked and restore .RData workspace at start time is unchecked. Under session. I clear the workspace and I start anew session without saving the workspace to the .RData file.

glyCount1 <- function(answer = NULL, fileChoice = NULL, combination = NULL, enteredValue = NULL, nameList) {

# Length of list
lc <- enteredValue 
elementname <- nameList
choseDataFiles = TRUE
fileLength <- length(lc)
# # These are the names that will be given to the 3 resulting files
first_path <- NULL
new_path <- NULL
#add the file names to the path and read and merge the contents of each list in the list of lists
new_dataFns = NULL


new_dataFns <- unlist(strsplit(as.character(nameList), ","))


content <- NULL
file_content <- NULL
dataFnsDir <- getwd()
first_path <- NULL
new_path = NULL

for(i in 1:length(lc)){
  #print(new_dataFns)
  for(j in 1:length(lc[[i]])){
    #print(new_dataFns)
    if(!is.null(lc[[i]])){
      print(new_dataFns)
      first_path[[j]] <- paste(getwd(), "/", lc[[i]][j], sep = "")
    #  file_content[[j]] <- read.csv(file = first_path[[i]], header = TRUE, sep = ",")
      for(k in 1:length(new_dataFns)){
        print(new_dataFns[k])
        if(k == i){
          print(new_dataFns[k])
          new_path[[j]] <- paste(getwd(),"/", i, new_dataFns[k], ".csv", sep = "")
          print(new_path[[j]])
          
        }
      }
      #  write.table(file_content[[j]], file = new_path[[j]],  append = TRUE, col.names = FALSE)
    }
  } 
}

}

#3

I tried to define the variable as global (new_dataFns <<- unlist(strsplit(as.character(nameList), ","))), but with no result.

assign("new_dataFns", nameList, envir=globalenv()) does not make the variable global either.


#4

I have also noticed that if I define the list of names in the script (new_dataFns <- list("name1", "name2", "name3"), the list of names is visible inside the ```
if(!is.null(lc[[i]])) condition. What it cannot be seen under that condition is the result of the input box which is stored in nameList function parameter. I cannot understand why. Should I convert nameList to something else?


#5

I am beginning to think that the problem is in the new_dataFns list.

new_dataFns = as.list(unlist(strsplit(as.character(nameList), ","))) . It seams that elementname a new_dataFns do not work in the same way in the script.

Am I am building the new_dataFns list incorrectly?. It seems as if the values could not be accessed. Would you recommend that I take the input in the app in a different way?

Thanks,