0

I wrote a function that takes 2 parameters , but calling the function with specific values raises an error message. Here's my code:

dynamicwilcox <- function(column, datacol) {    
    t = read.table("all.txt")
    #print(column)
    if(column=="Ph") {
        uniphy=unique(c(t$Phylum))
        print(uniphy)
    }               

    if(column=="Cl") {
        uniclass = unique(c(t$Class))
        print(uniclass)
    }   
}

Calling the functiondynamicwilcox("Ph","A") gives me an error. Why?

4
  • @lecodesportif : unexpected symbol in " dynamicwilcox("Ph" but when i try to run it like this : dynamicwilcox(Ph,A) it does not give me error , but the script won't print anything. Commented Apr 2, 2011 at 10:15
  • @lecodesportif: thnx for your help , i found out the problem , i was missing a " inside the function , but i tried to add a varaible that can be read from the keyboard and let the function work based on it , but it's not working , can you help me ?? Commented Apr 2, 2011 at 10:47
  • @coffeetocode: thnx for your help ,i found out the problem , i was missing a " inside the function , but i tried to add a varaible that can be read from the keyboard and let the function work based on it , but it's not working , can you help me ?? Commented Apr 2, 2011 at 10:48
  • @abd see ?readline - I'll edit my answer below with an example - but do note this will only work if you are interacting with R - i.e. you have it running in a console or the R GUI for example, but not in batch mode. Commented Apr 2, 2011 at 10:52

1 Answer 1

1

Something strange is certainly going on - was that all of the error message?

If I take your code back to basics and comment out all the stuff I can't run because I don't have your data file, the following function works without error:

dynamicwilcox <- function(column,datacol) {   
    ##dat <- read.table("all.txt") ## probably not good to call something t
    if(column=="Ph") {
        ##uniphy=unique(c(t$Phylum))
        ##print(uniphy)
        writeLines("column was 'Ph'")
    }
    if(column=="Cl") {
        ##uniclass = unique(c(t$Class))
        ##print(uniclass)
        writeLines("column was 'Cl'")
    }
}

R> dynamicwilcox("Ph", "A")
column was 'Ph'

Perhaps you could start with the above code and see if it works for you, and if it does, build upon it.

As for dynamicwilcox(Ph, A) working, it can't work unless you have already defined objects Ph and A in your current environment. It won't print anything because whatever is stored in Ph is not equal to "Ph" or "Cl". What do you get if you run these two lines ?:

R> Ph
R> A

Hopefully that will explain why that way of calling your function failed.

Update: As for altering the function to use readline() so it accepts user input, here is one version:

dynamicwilcox <- function() {
    ANSWER <- readline("What column do you want to work on? ")
    if(ANSWER=="Ph") {
        writeLines("column was 'Ph'")
    } else if(ANSWER=="Cl") {
        writeLines("column was 'Cl'")
    } else {
        writeLines(paste("Sorry, we don't know what to do with column", ANSWER))
    }
    ANSWER ## return something
}

Here it is in use:

R> dynamicwilcox()
What column do you want to work on? Ph
column was 'Ph'
[1] "Ph"
R> dynamicwilcox()
What column do you want to work on? Cl
column was 'Cl'
[1] "Cl"
R> dynamicwilcox()
What column do you want to work on? FooBar
Sorry, we don't know what to do with column FooBar
[1] "FooBar"

But do read ?readline as it has something just like this in one of the examples that you could learn from.

Sign up to request clarification or add additional context in comments.

5 Comments

@Gavin Simpson: thnx for your help , but i found out the error ,i was missng a ", and this worked :dynamicwilcox("Ph", "A") , but now i'm trying to read from keyboard , and execute the script based on what i read , i asked a new question for this , and you helped me :) but i did not found the complete solution , because the scan did not worked in my case
@abd I've updated to show how to use readline() and I have just left another comment on your earlier Q about reading from the keyboard.
@Gavin Simpson: Hello , i did tried this but it didn't worked, the script is not waiting me to enter the word , it just continue :| , i'm running the script on windows and result appear on a DOS.is this the error ??
@adb How many times do we have to tell you that you can't run in BATCH mode and use readline()!!! Open R GUI on your Windows box. Then run source(file.choose() and select your script in the dialogue that appears. There readline() will work. Please do read the answers we provide - it will save a lot of frustration on both our parts...
@adb sorry, typo in the above comment. It should have been source(file.choose()) there was a missing closing parenthesis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.