jeudi 4 août 2016

R: IF object is TRUE then assign object NOT WORKING

I am trying to write a very basic IF statement in R and am stuck. I thought I'd find someone with the same problem, but I cant. Im sorry if this has been solved before.

I want to check if a variable/object has been assigned, IF TRUE I want to execute a function that is part of a R-package. First I wrote

FileAssignment <- function(x){
  if(exists("x")==TRUE){
    print("yes!")
    x <- parse.vdjtools(x)
  } else { print("Nope!")}
}

I assign a filename as x

FILENAME <- "FILENAME.txt"

I run the function

FileAssignment(FILENAME)

I use print("yes!") and print("Nope!") to check if the IF-Statement works, and it does. However, the parse.vdjtools(x) part is not assigned. Now I tested the same IF-statement outside of the function:

if(exists("FILENAME1")==TRUE){
  FILENAME1 <- parse.vdjtools(FILENAME1)
}

This works. I read here that it might be because the function uses {} and the if-statement does too. So I should remove the brackets from the if-statement.

FileAssignment <- function(x){
  if(exists("x")==TRUE)
    x <- parse.vdjtools(x)
   else { print("Nope!")
}

Did not work either.

I thought it might be related to the specific parse.vdjtools(x) function, so I just tried assigning a normal value to x with x <- 20. Also did not work inside the function, however, it does outside.

Aucun commentaire:

Enregistrer un commentaire