Error in script run by app does not appear when script is run independently of the application

shiny

#1

Hello,

These are 2 R scripts, 1 app.r, and 1 script (glyCount1.r) called by app.r. Additionally, I am sending a reprex of the files called by the scripts and the app . The 2 scripts (readwrite.r and namestest.r) produce the desired result, while the app does not. The app calls glyCount1.r. glyCount1.app is equal to 1 of the scripts above. It could be either one. The scripts read 3 group of files and write each group to an individual file - 3 groups of files are read and 3 new files are created.. I am sending a reprex of the 2 scripts (readwritenames.r, and readwritefiles.r), a reprex of the app.r, a reprex of the script called by the app (glyCount1.r) and a reprex of the files read by the scripts. When I run the app that calls glyCount1.r I get the error: cannot open the connection. glyCount1.r can read the files, but cannot write them. It seems as it cannot read the paths to write to. The only difference between readwrite.r, namestest.r and glyCount1.r is that in readwrite.r and namestest.r, the file names are defined in the script, while in glyCount1.r the file names are sent by a function call in the app.r. The app uses an inputSelect widget to make 3 selections (each selection is 1 group) and it uses an inputText widget to select 3 names, one for each file. Could you please run the programs and look at the error? I do not know why the same script does not produce the same result when is run independently from the application as when is run by the application. {;ease le tme know if you need any clarifications.

Thanks

library(reprex)
# file directorylists
lc <- 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")

 


# file directory list of lists
lc <- list(list1, list2, list3)
#> Error in eval(expr, envir, enclos): object 'list1' not found
dataFiles <- NULL

#The purpose of the program is to read the files in lc: first list1, second list2 , third list3 and merge the files in each lists separately
#to make 3 sepate files.  The first file will contain the contents of list1, the second file will contents the contents of list2 and the third file
#will contain the contents of list three.

#I am saving the files - 20 lines of each - in the working directory.install

# Length of list
fileListLength <- length(lc)


# These are the names that will be given to the 3 resulting files
new_dataFns <- list("name1", "name2", "name3")
file_dir <- getwd()
dataList = NULL
new_filenames = NULL
finalList <- NULL
filenames <- NULL

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


 for(i in 1:length(lc)) {
      
        first_path[i] <- file.path(getwd(), lc[i])
        # print(first_path[i])
 }

for(j in 1:length(new_dataFns)){
    new_path[j] <- file.path(getwd(), new_dataFns[j])
 #   print(new_path[j])
}

file_content <- NULL
for(i in 1:length(lc)){
  for(j in 1:length(lc[[i]])){
       first_path[j]<- unique(file.path(getwd(), lc[[i]][j]))
       file_content[[j]] <- read.csv(file = first_path[[i]], header = TRUE, sep = ",")
       new_path[j] <- file.path(getwd(), new_dataFns[i])
       write.table(file_content, file = new_path[j],  append = TRUE, col.names = FALSE)
  }
} 
#> Warning in file(file, "rt"): cannot open file '/tmp/RtmplQSt7s/
#> JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSMs.csv': No such file or directory
#> Error in file(file, "rt"): cannot open the connection

Created on 2018-08-31 by the reprex package (v0.2.0).

namestest.r


lc <- 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")
               
elementname <- list("name1, name2, name3")  
content <- NULL
file_content <- NULL
dataFnsDir <- getwd()

for(i in 1:length(lc)) {
                 
   first_path[i] <- file.path(getwd(), lc[i])
          # print(first_path[i])
}
#> Error in eval(expr, envir, enclos): object 'first_path' not found
               
  for(j in 1:length(new_dataFns)){
       new_path[j] <- file.path(getwd(), new_dataFns[j])
                 #   print(new_path[j])
  }
#> 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]])){
    first_path[j]<- unique(file.path(getwd(), lc[[i]][j]))
    file_content[[j]] <- read.csv(file = first_path[[i]], header = TRUE, sep = ",")
    # new_path[j] <- file.path(getwd(), new_dataFns[i])
    # write.table(file_content, file = new_path[j],  append = TRUE, col.names = FALSE)
  } 
  content <- append(content, file_content[i])

}
#> Error in eval(expr, envir, enclos): object 'first_path' not found

for(i in 1:length(new_dataFns)){
  for(j in 1:length(content[j])){
    
    new_dataFns <- unlist(strsplit(as.character(elementname), ","))
    new_path <- paste(dataFnsDir, "/", new_dataFns[i], sep = "")
    new_path <- gsub('\\s+', '', new_path)
  }
  write.table(content[j], file= new_path, append = TRUE, col.names = FALSE)
}
#> Error in eval(expr, envir, enclos): object 'new_dataFns' not found

Created on 2018-08-31 by the reprex package (v0.2.0).

App.r

library(shiny)

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")
  

)
#> Error in useShinyjs(): could not find function "useShinyjs"

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

enteredValue = NULL
nameList = NULL
answer = NULL
fileChoice = NULL
combination = NULL
combinations = NULL

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

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

if(!is.null(selections)){
  combinations = selections
  #print(combinations)
}

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

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


  
  choseDataFiles <- glyCount(answer, fileChoice, combination, enteredValue, nameList)[[1]]
  combine <- glyCount(answer, fileChoice, combination, enteredValue, nameList)[[2]]
  fileLength <- glyCount(answer, fileChoice, combination, enteredValue, nameList)[[3]]


if(!is.null(choseDataFiles)){
  choseDataFiles = choseDataFiles
}
if(!is.null(combine)){
  comb = combine
}

if(!is.null(fileLength)){
  nameListSize <- fileLength
}




list(choseDataFiles = choseDataFiles, comb = comb, nameListSize = nameListSize)



}
  
  server <- function(input, output, session){
    
    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)))
      }
    })
    
    
    output$samelist<-renderPrint({
      
      listContents  <- list()
      listContents <- append(listContents, myValues$dList)
      print(listContents)
      if(input$sBtn > 0){
        numberOfSelectedSets <- glycoPipe(response = NULL, fOfData = NULL, combineResult = NULL, unlist(input$filescombine), 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, unlist(input$filescombine), input$x, vals)
        
      }
    })
    

    
    
    
    session$allowReconnect(TRUE)
    
  }
  
  shinyApp(ui = ui, server = server)
#> Error in force(ui): object 'ui' not found

Created on 2018-08-31 by the reprex package (v0.2.0).

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

#The purpose of the program is to read the files in lc: first list1, second list2 , third list3 and merge the files in each lists separately
#to make 3 sepate files.  The first file will contain the contents of list1, the second file will contents the contents of list2 and the third file
#will contain the contents of list three.

#I am saving the files - 20 lines of each - in the working directory.install

# 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
content <- NULL
combine <- FALSE
#add the file names to the path and read and merge the contents of each list in the list of lists
print(length(new_dataFns))

first_path <- NULL
new_path <- NULL
content <- NULL
#add the file names to the path and read and merge the contents of each list in the list of lists


for(i in 1:length(lc)) {
  
  first_path[i] <- file.path(getwd(), lc[i])
  # print(first_path[i])
}

for(j in 1:length(elementname)){
  new_path[j] <- file.path(getwd(), elementname[j])
  #   print(new_path[j])
}

file_content <- NULL
for(i in 1:length(lc)){
  
  for(j in 1:length(lc[[i]])){
    first_path[j]<- paste(getwd(), "/", lc[[i]][j], sep = "")
    file_content[[j]] <- tryCatch(read.csv(file = first_path[[i]], header = TRUE, sep = ","), error=function(e) NULL)
  }
  content <- append(content, file_content[i])
  
} 
for(i in j:length(new_dataFns)){
for(i in 1:length(new_dataFns)){
  new_dataFns <- unlist(strsplit(as.character(elementname), ","))
  new_path <- paste(getwd(), "/", new_dataFns[i], sep = "")
  new_path <- gsub('\\s+', '', new_path)
}
  write.table(content[j], file= new_path, append = TRUE, col.names = FALSE)
}
 list(choseDataFiles = choseDataFiles,
     combine = combine, fileLength = fileLength)
}

Created on 2018-08-31 by the reprex package (v0.2.0).

library(magrittr)
library(reprex)

JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSM<- NULL

JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSM %>%        # table that should become a file
  head(5) %>%    # 5 lines might be plenty?
  dput()
#> NULL

JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSM <-    
  # Everything below here is the output from above. I used
  #  fake data for this.
  structure(list(
    
    confidence = c("High", "High", "High", "High", "High", "High", "Medium", "High", "High", "High", "High", "High", "High", "High", "High", "Medium", "High", "High", "Medium","High"
    ),
    deltaScore = c("0.1667", "0.5789", "1", "0.56", "0.52", "1", "0.56", "0.5882", "1", "0.52", "1", "0.56", "0.5", "0.2857", "0.5556", "0.55", "0", "0.2857", "0.5789", "0.5"
    ),
    
    PSM_ambiguity = c("Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Unambiguous", "Ambiguous", "Unambiguous", "Unambiguous", "Medium"
    ),
    
    Annotated_Sequence = c("InHcR", "InHcR", "InHcR", "InHcR", "InHcR", "InHcR", "InHcR", "EnGTVSR", "InHcR", "InHcR", "InHcR",  "InHcR", "InHcR", "InHcR", "InHcR", "InHcR", "InHcR", "InHcR", "InHcR", "InHcR"
    ),
    
    Modifications = c("N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)","N2(HexNAc) C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)", "N2(HexNAc); C4(Carbamidomethyl)","N2(HexNAc); C4(Carbamidomethyl", "N2(HexNAc); C4(Carbamidomethyl", "N2(HexNAc); C4(Carbamidomethyl)"
    )
    
  ), .Names = c("confidence", "deltaScore","PMS_Ambiguity", "Annotated_Sequence", "Modifications"), row.names = c(
    NA,
    -20L
  ), class = c("tbl_df", "tbl", "data.frame"))

write.csv(JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSM, "F_160426_Dep2Plas_tryp_Gpep_SIDtargPSM.csv")
rm(JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSM)

Created on 2018-08-31 by the reprex package (v0.2.0).


#2

First, this is way too much to expect help in debugging. You should try to recreate the problem using a minimal amount of code. Strip it down bit by bit until you have the least amount necessary to get the same error.

Second, this sounds like a classic case of not considering your environment. In the namestest.r file, the errors you get makes sense. new_dataFns and first_path aren't defined in the script. They might exist in your environment when you interactively run the script, which would explain how it works then. When writing scripts, make sure to run them in a clean session. Also, don't use the "load environment on start" feature (a file named .RData, with no prefix to the name, in the working directory).


#3

Hello,

I am sorry, I sent you the wrong program. This is called testnames.r . I did not mean for you to have to go through all the lines I thought the scripts readwite and testnames were working fine and knowing that I sent all the programs so you could try them and compare. The only thing that does not work is the write.table in the statement of the script called by the App (glyCount1.R) . glyCount1.R works when run independently of the app, but it throws an error if it is called by the APP, "cannot open connection", cannot open file '/home/giuseppa/Development/glycoPipeApp, Is a directory.Please see below immediately after this paragraph. There is the statement that throws the error, exactly the write.table() statement. These are the minimal examples, since I want to show that the only difference between the app and one of the scripts is that in one case the filenames are defined in the script while in the application they are input through the GUI. For some reason, the name of the file does not get attached to the path.

Could you please explain how to go about doing this? " Also, don't use the "load environment on start" feature (a file named .RData , with no prefix to the name, in the working directory)." When I start R in RStudio I get this message in the console: [Workspace loaded from ~/.RData]


for(i in 1:length(elementname)){
  new_dataFns <- unlist(strsplit(as.character(elementname), ","))
  new_path[i] <- paste(getwd(), "/", new_dataFns[i], sep = "")
  new_path[i] <- gsub('\\s+', '', new_path[i])
  print(new_path[i])
  write.table(content[j], file= new_path[i], append = TRUE, col.names = FALSE)
}
list1 <- list(a = "JF_160426_Dep2Plas_tryp_Gpep_SIDtarg-(06)_PSMs.txt", b = "JF_160426_Dep2Plas_ctryp_Gpep_SIDtargFULL_PSMs.txt",
              c = "160824_JF_udep_tryp_Hi_SIDdda_FULL_NewParse-(05)_PSMs.txt", d = "150413_JF_GPeps_SIDtarg_GPstdMix_Tryp_2runs_v3_PSMs.txt")
list2 <- list(a = "JF_160426_Dep2Plas_tryp_Gpep_SIDtarg-(06)_PSMs.txt", b = "JF_160426_Dep2Plas_ctryp_Gpep_SIDtargFULL_PSMs.txt",
              c = "160824_JF_udep_tryp_Hi_SIDdda_FULL_NewParse-(05)_PSMs.txt")
list3 <- list(a = "JF_160426_Dep2Plas_tryp_Gpep_SIDtarg-(06)_PSMs.txt", b = "JF_160426_Dep2Plas_ctryp_Gpep_SIDtargFULL_PSMs.txt")
lc <- list(list1, list2, list3)
dataFiles <- NULL

#The purpose of the program is to read the files in lc: first list1, second list2 , third list3 and merge the files in each lists separately
#to make 3 sepate files.  The first file will contain the contents of list1, the second file will contents the contents of list2 and the third file
#will contain the contents of list three.

#I am saving the files - 20 lines of each - in the working directory.install

# Length of list
fileListLength <- length(lc)


# These are the names that will be given to the 3 resulting files
new_dataFns <- list("name1", "name2", "name3")

first_path <- NULL
new_path <- NULL
content <- NULL
#add the file names to the path and read and merge the contents of each list in the list of lists


for(i in 1:length(lc)) {
  
  first_path[i] <- file.path(getwd(), lc[i])
  # print(first_path[i])
}

for(j in 1:length(new_dataFns)){
  new_path[j] <- file.path(getwd(), new_dataFns[j])
  #   print(new_path[j])
}

file_content <- NULL
for(i in 1:length(lc)){
  for(j in 1:length(lc[[i]])){
    first_path[j]<- paste(getwd(),"/", lc[[i]][j], sep = "")
    file_content[[j]] <- read.csv(file = first_path[[i]], header = TRUE, sep = ",")
  }
  content <- append(content, file_content[i])
  
} 
#> Warning in file(file, "rt"): cannot open file '/tmp/RtmpypcxGn/
#> JF_160426_Dep2Plas_tryp_Gpep_SIDtarg-(06)_PSMs.txt': No such file or
#> directory
#> Error in file(file, "rt"): cannot open the connection

for(i in 1:length(new_dataFns)){
  for(j in 1:length(content[j])){
    
    new_dataFns <- unlist(strsplit(as.character(new_dataFns), ","))
    new_path <- paste(getwd(), "/", new_dataFns[i], sep = "")
    new_path <- gsub('\\s+', '', new_path)
  }
  write.table(content[j], file= new_path, append = TRUE, col.names = FALSE)
}

Created on 2018-08-31 by the reprex package (v0.2.0).


#4

Thanks for your help. yes you are write. Once I disabled loading of .RData, I started to see errors in the scripts that has not seen before.


#5

Thanks again. The script below works fine when it is called by the application.


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

#The purpose of the program is to read the files in lc: first list1, second list2 , third list3 and merge the files in each lists separately
#to make 3 sepate files.  The first file will contain the contents of list1, the second file will contents the contents of list2 and the third file
#will contain the contents of list three.

#I am saving the files - 20 lines of each - in the working directory.install

# 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(elementname), ","))

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]]<- file.path(getwd(), lc[[i]][j])
   file_content[[j]] <- read.csv(file = first_path[[i]], header = TRUE, sep = ",")
   new_path[[j]] <- paste(getwd(), "/", i, ".csv", sep = "")
   write.table(file_content[[j]], file = new_path[[j]],  append = TRUE, col.names = FALSE)
  }
  } 
}
}