Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 statements that use/ reference specific variables.

My issue is that when using paste within a function, I am losing my pasted output if there is anything included in the function following the "paste" statement.

Please see the following three functions:

TS<-5

Example 1- everything works fine

T<-function(){

if(exists("TS"))
{paste("TS= ", TS, sep=" ")}

else

if(!exists("TS"))
{print.noquote("No TS Values")}

}

Example 2- My Problem. When I add anything (in this case another print command) following my "if" statement I will lose my pasted output

T<-function(){

if(exists("TS"))
{paste("TS= ", TS, sep=" ")}

else

if(!exists("TS"))
{print.noquote("No TS Values")}

print("my exsistance removes paste output") 

}

Example 3- The same statement placed before the "if" has no negative effect

T<-function(){

print("my exsistance does not remove paste output")

if(exists("TS"))
{paste("TS= ", TS, sep=" ")}

else

if(!exists("TS"))
{print.noquote("No TS Values")}


}

Can someone explain where the conflict is within this function. And better yet how can I work around it so that I can have a paste statement followed by other actions within a function

basically how can I get example #2 to work.

Brownie points- (for sake of visual consistency) when using "print.noquote", is there such a thing as a paste.noquote?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
853 views
Welcome To Ask or Share your Answers For Others

1 Answer

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") 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...