[R] Multi-line paste function

Lapointe, Pierre Pierre.Lapointe at nbf.ca
Tue Mar 14 19:13:55 CET 2006


Here's my contribution to R.

When R interacts with external programs (MySQL, cURL, etc.), it often
requires a pasted string that is sent to these programs. For readability
reasons, it is often preferable to have complex commands (SQL for example)
spread on several lines. However, the normal paste function requires to add
additional ' ", ' at the end of each line and another ' " ' at the beginning
of each new line.  It becomes fastidious for long commands.

Multi-line paste function:

multi.line.paste <-function (..., sep = " ", collapse = NULL) 
{
    args <- list(...)
    if (length(args) == 0) 
        if (length(collapse) == 0) 
            character(0)
        else ""
    else {
        for (i in seq(along = args)) args[[i]] <-
gsub("\n","",as.character(args[[i]]))
        .Internal(paste(args, sep, collapse))
    }
}

Example with a SQL command on multiple lines:

multi.line.paste("
SELECT * 
FROM estimates a, newtable b 
WHERE a.Ticker=b.Ticker 
AND a.Fiscal_Year=b.Fiscal_Year 
AND a.EPS<>b.EPS 
AND a.Date_Last_change-1.9>b.Date_Last_change
")

Regards,

Pierre


**************************************************
AVIS DE NON-RESPONSABILITE: Ce document transmis par courrie...{{dropped}}




More information about the R-help mailing list