Cannot extract parameter from file

One of the parameters in the fiel is PARAMETER. To extract PARAMETER I am using template$PARAMETER, but it does not work. I get the error $ operator is invalid for atomic vectors. It does not work if I convert the read file to a data frame "data.frame(read.delim)" . it does not work if I use template[, "PARAMETER"] or template[[PARAMETER]]. I cannot find any other explanation in Google. What seems to be not working?

 template<- try(read.delim(parmsDefFn, as.is=T), silent=TRUE)
  if (inherits(template, "try-error")) { # if error,
   params <- stop("Defaults params file ",basename(parmsDefFn),"either doesn't exist or is not a valid tab-separated PARAMS file. \n")
 }
  
  #PARAMS FILE:
  prm<- try(read.delim(PARAMSfullFile, as.is=T), silent=TRUE)
   if (inherits(prm, "try-error")) { # if error,
    stop(basename(PARAMSfullFile)," either doesn't exist or is not a valid tab-separated PARAMS file. \n")
  }

It is difficult to understand without the reprex, but if I understand your question correctly, I think you might want to take a look at yaml package (https://cran.r-project.org/web/packages/yaml/index.html). It allows you to read in files in yaml format that does exactly what you want. It then saves it as a list that you can work on using the $ or any other way you've described above.
The error you are seeing (operator is invalid for atomic vectors) means that whatever you read in is not what you think it is. Namely, it has no PARAMETER value in it. You can use str (i.e., str(template)) to see what is the structure of the object you've read in.

1 Like

Thank you very much. How about package dplyr? Is there any way to eliminate empty rows and comments from the file?

dplyr is the package that works with dataframes in one way or another. It can definitely do those things, but keep in mind that R works with all objects in memory, so if you want to, for example, read in something from the file, clean it up with dplyr and then save it back to the disk than your first question is not so relevant (or, indeed, yaml package).
So I would recommend to create a reproducible example of what you are trying to do and then it'll be easier to recommend possible solution.

Thanks. I am working on it. I will send it soon.

In this piece of code I ma trying to read a tab delimited file and eliminate the blank lines, but it does not eliminate the NAs or empty Lines

  templateFn<- "0.tsv"
  
  parmsDefFn<- file.path("~", templateFn)
  
  template<- try(read.delim(parmsDefFn, as.is=T, sep = '\t', na.strings = c("", "NA")), silent=TRUE)
  if (inherits(template, "try-error")) { # if error,
    stop("Defaults params file ",basename(parmsDefFn),"either doesn't exist or is not a valid tab-separated PARAMS file. \n")
  }
  template = template %>% na.omit()

The code is expected to store the result of reading the file in a dataframe, however I not get a data frame, but each row is a vector. Why is this happening?

Try to use readr::read_tsv function and see if it helps. I'm not sure why something like this can happen, but since I don't know what is 0.tsv then I can't really help you more.

Is readr a package? 0.tsv is a tab delimited file

Yes, it's a package.
Well, I don't have this file, so I have no idea what is inside of it. For that reason I'm not sure why your code doesn't work. From the explanation you've provided I don't see the problem.

I know what happens. I eliminated manually the first empty line of the file and the statement template$PARAMETER worked very well. I think I have to eliminate empty lines and #comments before I use template$PARAM. How can I do that so that without changing the content of the file from adataframe to a list of vector?

Another problem is that in my Shiny server function I save the file to the server home directory using file.copy(input$file$datapath, "/home/giuseppa"). and that copy is the one that creates the vectors instead of a data frame. How can Is save the information to the server without using copy?

I would like to delete tis edit

I think I figured it out. the first time I run the program file.copy(input$file$datapath, “/home/giuseppa”) , when I try to read the copied/ saved file, the statement read.delim("~/0.tsv) does not exit. When I run the program again evrything works fine because now when I run read.delim("~0.tsv") the file exists.

Additionally I have added recursive = TRUE.
file.copy(input$file$datapath, “/home/giuseppa”, recursive = TRURE)