Difference between paste and print (affecting result of function)

前端 未结 2 396
离开以前
离开以前 2020-12-19 14:10

To start off, I\'m not really sure what the difference between paste and print is. But I am using \"print\" to spit out generic statements and \"paste\" to spit out statemen

2条回答
  •  一向
    一向 (楼主)
    2020-12-19 15:01

    paste concatenates (pastes) strings and returns a character vector, so you can do thing like

    paste('a','b', sep = '-')
    
    ## [1] "a-b"
    

    print prints values. From ?print

    print prints its argument and returns it invisibly (via invisible(x)). It is a generic function which means that new printing methods can be easily added for new classes.

    Most classes will have a defined print method (or will use print.default)

    You can see the available print methods by typing

    methods('print')
    

    In your case paste("TS= ", TS, sep=" ") returns a character vector, so when this is the result of the function, print.character is used to display the results

    In fact, I think you want message not print or print.noquote.

    T <- function() {
        if (exists("TS"))
        {
            message(paste("TS= ", TS, sep=" "))
        } else if (!exists("TS")) {
            message("No TS Values")
        }
        message("my exsistance removes paste output") 
    }
    

提交回复
热议问题