Giuseppa Cefalu
2018-Sep-12 21:06 UTC
[R] How to insert a name from a list into a double loop
Hello, The script below works well when I define the elementname <- list("name1", "name2", "name3") . Please see program below. SCRIPT ``` glyCount1 <- function(answer = NULL, fileChoice = NULL, combination = NULL, enteredValue = NULL, nameList) { lc <- enteredValue choseDataFiles = TRUE first_path <- NULL new_path <- NULL new_dataFns <- list() new_dataFns <- as.list(unlist(strsplit(as.character(nameList), ","))) file_content <- NULL elementname <- list("name1", "name2", "name3") for(i in 1:length(lc)){ for(j in 1:length(lc[[i]])){ first_path[j]<- paste(getwd(), "/", lc[[i]][j], sep = "") print(first_path[j]) # file_content[[j]] <- read.csv(file = first_path[[i]], header TRUE, sep = ",") for(k in 1:length(elementname)){ if(k == i){ new_path[j] <- paste(getwd(),"/", i, elementname[k], ".csv", sep = "") print (new_path[j]) } } } } } ``` SCRIPT OUTPUT: [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" However, If instead of using the list "elementname" I call the script through an app and I enter the list of names through a textbox, which then is passed to the glycoPipe1 function. Please see below. The same script does not return the expected output. 'new_dataFns <- list() new_dataFns <- as.list(unlist(strsplit(as.character(nameList), ","))) is used in the script when the value passed to the function by the app's textinput (nameList) is used, in which case I would substitute the elementname list buythe new_dataFns list, please see block code below. BLOCK OF CODE ``` for(i in 1:length(lc)){ for(j in 1:length(lc[[i]])){ first_path[j]<- paste(getwd(), "/", lc[[i]][j], sep = "") print(first_path[j]) # file_content[[j]] <- read.csv(file = first_path[[i]], header TRUE, sep = ",") for(k in 1:length(elementname)){ if(k == i){ new_path[j] <- paste(getwd(),"/", i, elementname[k], ".csv", sep = "") print (new_path[j]) } } } ``` I am sending the app below just in case you need it. but I wonder if it has to do with the 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. Thank you for your help APP PROGRAM ``` library(shiny) library(shinyjs) 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 = NULL, vals) #unlist(input$filescombine) } }) session$allowReconnect(TRUE) } shinyApp(ui = ui, server = server) ``` [[alternative HTML version deleted]]