Multi-line paste function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Multi-line paste function

Lapointe, Pierre
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}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Multi-line paste function

Philippe Grosjean
I am not sure I clearly understand what you want, but getting the string
returned by your multi.line.paste() function is straightforward using
gsub():

gsub("\n", "", "
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
")

If you really want a custom function for that, then, define:

multi.line.paste <- function(str) gsub("\n", "", str)

Best,

Philippe Grosjean

Lapointe, Pierre wrote:

> 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}}
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Multi-line paste function

Lapointe, Pierre
In reply to this post by Lapointe, Pierre
Hello Phillipe

1-You are right that for a simple example as the one I provided, paste and
gsub give the same result.
2-For a more complex case, where let's say, I'd like to include a variable
in my SQL statement, the multi.line.paste command is essential.

variable <-1.9
multi.line.paste("
SELECT *
FROM estimates3 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_or_conf-",variable,">b.Date_Last_change_or_conf
",sep="")

Regards,

Pierre

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Multi-line paste function

Philippe Grosjean
Lapointe, Pierre wrote:

> Hello Phillipe
>
> 1-You are right that for a simple example as the one I provided, paste and
> gsub give the same result.
> 2-For a more complex case, where let's say, I'd like to include a variable
> in my SQL statement, the multi.line.paste command is essential.
>
> variable <-1.9
> multi.line.paste("
> SELECT *
> FROM estimates3 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_or_conf-",variable,">b.Date_Last_change_or_conf
> ",sep="")
>
> Regards,
>
> Pierre

Still simpler than your function:

variable <- 1.9
gsub("\"", "", paste("
SELECT *
FROM estimates3 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_or_conf-",variable,">b.Date_Last_change_or_conf
", sep=""))

Philippe Grosjean

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html