What is the correct way to put quoted strings in rlang functions?









up vote
0
down vote

favorite












I am writing a fuction that I will use regularly to filter for dates and names in our database (and then to perform some monthly counting/calculations on them).
I would like to know what is the correct way to insert and evaluate strings within rlang functions in this case?



Am I doing right by using quo to put strings into the fuction?



Example:



 business_flights = tibble(passanger_name=rep(c(rep("John RED",3),rep("Mary ORANGE",3)),4),
dep_date=seq(from = lubridate::ymd('2005-04-07'),
to = lubridate::ymd('2025-03-22'), length.out = 24),
flight_num = sample(seq(from = 99, to = 1999, by = 30), size = 24, replace = TRUE))

filter_flights = function(mytibble, name, date0, date1)
require(tidyverse); require(lubridate)
flights_filtered = mytibble %>%
filter(dep_date >= !!date0, dep_date < !!date1,
grepl(!!name, passanger_name))
View(flights_filtered)


filter_flights(mytibble = business_flights,
name = quo("RED"),
date0 = quo("2005-10-13"),
date1 = quo(today()))









share|improve this question



















  • 1




    Why do you want to use quo ? it works fine without it
    – Moody_Mudskipper
    Nov 10 at 13:20










  • To go one step further from @Moody_Mudskipper, I dont think you need any quasiquotaton in your example. if you take out both quo and !!, you will get the same result. If you passed a variable that you would want to filter instead of dep_date then you would need QQ or use the NSE escape hatches
    – Jrakru56
    Nov 10 at 16:19















up vote
0
down vote

favorite












I am writing a fuction that I will use regularly to filter for dates and names in our database (and then to perform some monthly counting/calculations on them).
I would like to know what is the correct way to insert and evaluate strings within rlang functions in this case?



Am I doing right by using quo to put strings into the fuction?



Example:



 business_flights = tibble(passanger_name=rep(c(rep("John RED",3),rep("Mary ORANGE",3)),4),
dep_date=seq(from = lubridate::ymd('2005-04-07'),
to = lubridate::ymd('2025-03-22'), length.out = 24),
flight_num = sample(seq(from = 99, to = 1999, by = 30), size = 24, replace = TRUE))

filter_flights = function(mytibble, name, date0, date1)
require(tidyverse); require(lubridate)
flights_filtered = mytibble %>%
filter(dep_date >= !!date0, dep_date < !!date1,
grepl(!!name, passanger_name))
View(flights_filtered)


filter_flights(mytibble = business_flights,
name = quo("RED"),
date0 = quo("2005-10-13"),
date1 = quo(today()))









share|improve this question



















  • 1




    Why do you want to use quo ? it works fine without it
    – Moody_Mudskipper
    Nov 10 at 13:20










  • To go one step further from @Moody_Mudskipper, I dont think you need any quasiquotaton in your example. if you take out both quo and !!, you will get the same result. If you passed a variable that you would want to filter instead of dep_date then you would need QQ or use the NSE escape hatches
    – Jrakru56
    Nov 10 at 16:19













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am writing a fuction that I will use regularly to filter for dates and names in our database (and then to perform some monthly counting/calculations on them).
I would like to know what is the correct way to insert and evaluate strings within rlang functions in this case?



Am I doing right by using quo to put strings into the fuction?



Example:



 business_flights = tibble(passanger_name=rep(c(rep("John RED",3),rep("Mary ORANGE",3)),4),
dep_date=seq(from = lubridate::ymd('2005-04-07'),
to = lubridate::ymd('2025-03-22'), length.out = 24),
flight_num = sample(seq(from = 99, to = 1999, by = 30), size = 24, replace = TRUE))

filter_flights = function(mytibble, name, date0, date1)
require(tidyverse); require(lubridate)
flights_filtered = mytibble %>%
filter(dep_date >= !!date0, dep_date < !!date1,
grepl(!!name, passanger_name))
View(flights_filtered)


filter_flights(mytibble = business_flights,
name = quo("RED"),
date0 = quo("2005-10-13"),
date1 = quo(today()))









share|improve this question















I am writing a fuction that I will use regularly to filter for dates and names in our database (and then to perform some monthly counting/calculations on them).
I would like to know what is the correct way to insert and evaluate strings within rlang functions in this case?



Am I doing right by using quo to put strings into the fuction?



Example:



 business_flights = tibble(passanger_name=rep(c(rep("John RED",3),rep("Mary ORANGE",3)),4),
dep_date=seq(from = lubridate::ymd('2005-04-07'),
to = lubridate::ymd('2025-03-22'), length.out = 24),
flight_num = sample(seq(from = 99, to = 1999, by = 30), size = 24, replace = TRUE))

filter_flights = function(mytibble, name, date0, date1)
require(tidyverse); require(lubridate)
flights_filtered = mytibble %>%
filter(dep_date >= !!date0, dep_date < !!date1,
grepl(!!name, passanger_name))
View(flights_filtered)


filter_flights(mytibble = business_flights,
name = quo("RED"),
date0 = quo("2005-10-13"),
date1 = quo(today()))






r dplyr tidyverse lubridate rlang






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 12:13

























asked Nov 10 at 12:08









lim-lim

123




123







  • 1




    Why do you want to use quo ? it works fine without it
    – Moody_Mudskipper
    Nov 10 at 13:20










  • To go one step further from @Moody_Mudskipper, I dont think you need any quasiquotaton in your example. if you take out both quo and !!, you will get the same result. If you passed a variable that you would want to filter instead of dep_date then you would need QQ or use the NSE escape hatches
    – Jrakru56
    Nov 10 at 16:19













  • 1




    Why do you want to use quo ? it works fine without it
    – Moody_Mudskipper
    Nov 10 at 13:20










  • To go one step further from @Moody_Mudskipper, I dont think you need any quasiquotaton in your example. if you take out both quo and !!, you will get the same result. If you passed a variable that you would want to filter instead of dep_date then you would need QQ or use the NSE escape hatches
    – Jrakru56
    Nov 10 at 16:19








1




1




Why do you want to use quo ? it works fine without it
– Moody_Mudskipper
Nov 10 at 13:20




Why do you want to use quo ? it works fine without it
– Moody_Mudskipper
Nov 10 at 13:20












To go one step further from @Moody_Mudskipper, I dont think you need any quasiquotaton in your example. if you take out both quo and !!, you will get the same result. If you passed a variable that you would want to filter instead of dep_date then you would need QQ or use the NSE escape hatches
– Jrakru56
Nov 10 at 16:19





To go one step further from @Moody_Mudskipper, I dont think you need any quasiquotaton in your example. if you take out both quo and !!, you will get the same result. If you passed a variable that you would want to filter instead of dep_date then you would need QQ or use the NSE escape hatches
– Jrakru56
Nov 10 at 16:19













1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Non-standard evaluation allows you to capture and manipulate expressions. In base R, this is primarily accomplished through the use of quote:



quote(x)
# x

quote( a*log10(b+5) )
# a * log10(b + 5)


However, any captured expression that consists of a single literal is itself that literal:



identical( quote(5), 5 ) # TRUE
identical( quote("a"), "a" ) # TRUE
identical( quote(FALSE), FALSE ) # TRUE
identical( quote(5+5), 10 ) # FALSE, expression is not a single literal


rlang::quo from tidyverse builds on this functionality by capturing an expression AND the environment that gave rise to that expression. Together, these define a quosure:



quo(x)
# <quosure>
# expr: ^x
# env: global

f <- function() quo(x)
f()
# <quosure>
# expr: ^x
# env: 0x55b7e61d5c80


Keeping expressions next to their environments allows you to ensure that they are always evaluated in a consistent fashion, as they make their way through your code:



x <- 5
g <- function( myexpr )
x <- 10
eval_tidy( myexpr )


x # Evaluate expression directly in its environment
# 5

g( quote(x) ) # Evaluate expression inside g()
# 10

g( quo(x) ) # Evaluate expression in its env, while inside g()
# 5


However, when capturing a literal inside a quosure, quo assigns an empty environment to it:



quo("x")
# <quosure>
# expr: ^"x"
# env: empty


That is because the string "x" will always evaluate to "x", regardless of what environment it's evaluated in. Because of this, there is almost never a good reason to quo a string object (or any literal for that matter). It doesn't do anything and, as pointed out in the comments, your code will work fine without it.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238792%2fwhat-is-the-correct-way-to-put-quoted-strings-in-rlang-functions%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Non-standard evaluation allows you to capture and manipulate expressions. In base R, this is primarily accomplished through the use of quote:



    quote(x)
    # x

    quote( a*log10(b+5) )
    # a * log10(b + 5)


    However, any captured expression that consists of a single literal is itself that literal:



    identical( quote(5), 5 ) # TRUE
    identical( quote("a"), "a" ) # TRUE
    identical( quote(FALSE), FALSE ) # TRUE
    identical( quote(5+5), 10 ) # FALSE, expression is not a single literal


    rlang::quo from tidyverse builds on this functionality by capturing an expression AND the environment that gave rise to that expression. Together, these define a quosure:



    quo(x)
    # <quosure>
    # expr: ^x
    # env: global

    f <- function() quo(x)
    f()
    # <quosure>
    # expr: ^x
    # env: 0x55b7e61d5c80


    Keeping expressions next to their environments allows you to ensure that they are always evaluated in a consistent fashion, as they make their way through your code:



    x <- 5
    g <- function( myexpr )
    x <- 10
    eval_tidy( myexpr )


    x # Evaluate expression directly in its environment
    # 5

    g( quote(x) ) # Evaluate expression inside g()
    # 10

    g( quo(x) ) # Evaluate expression in its env, while inside g()
    # 5


    However, when capturing a literal inside a quosure, quo assigns an empty environment to it:



    quo("x")
    # <quosure>
    # expr: ^"x"
    # env: empty


    That is because the string "x" will always evaluate to "x", regardless of what environment it's evaluated in. Because of this, there is almost never a good reason to quo a string object (or any literal for that matter). It doesn't do anything and, as pointed out in the comments, your code will work fine without it.






    share|improve this answer
























      up vote
      0
      down vote



      accepted










      Non-standard evaluation allows you to capture and manipulate expressions. In base R, this is primarily accomplished through the use of quote:



      quote(x)
      # x

      quote( a*log10(b+5) )
      # a * log10(b + 5)


      However, any captured expression that consists of a single literal is itself that literal:



      identical( quote(5), 5 ) # TRUE
      identical( quote("a"), "a" ) # TRUE
      identical( quote(FALSE), FALSE ) # TRUE
      identical( quote(5+5), 10 ) # FALSE, expression is not a single literal


      rlang::quo from tidyverse builds on this functionality by capturing an expression AND the environment that gave rise to that expression. Together, these define a quosure:



      quo(x)
      # <quosure>
      # expr: ^x
      # env: global

      f <- function() quo(x)
      f()
      # <quosure>
      # expr: ^x
      # env: 0x55b7e61d5c80


      Keeping expressions next to their environments allows you to ensure that they are always evaluated in a consistent fashion, as they make their way through your code:



      x <- 5
      g <- function( myexpr )
      x <- 10
      eval_tidy( myexpr )


      x # Evaluate expression directly in its environment
      # 5

      g( quote(x) ) # Evaluate expression inside g()
      # 10

      g( quo(x) ) # Evaluate expression in its env, while inside g()
      # 5


      However, when capturing a literal inside a quosure, quo assigns an empty environment to it:



      quo("x")
      # <quosure>
      # expr: ^"x"
      # env: empty


      That is because the string "x" will always evaluate to "x", regardless of what environment it's evaluated in. Because of this, there is almost never a good reason to quo a string object (or any literal for that matter). It doesn't do anything and, as pointed out in the comments, your code will work fine without it.






      share|improve this answer






















        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Non-standard evaluation allows you to capture and manipulate expressions. In base R, this is primarily accomplished through the use of quote:



        quote(x)
        # x

        quote( a*log10(b+5) )
        # a * log10(b + 5)


        However, any captured expression that consists of a single literal is itself that literal:



        identical( quote(5), 5 ) # TRUE
        identical( quote("a"), "a" ) # TRUE
        identical( quote(FALSE), FALSE ) # TRUE
        identical( quote(5+5), 10 ) # FALSE, expression is not a single literal


        rlang::quo from tidyverse builds on this functionality by capturing an expression AND the environment that gave rise to that expression. Together, these define a quosure:



        quo(x)
        # <quosure>
        # expr: ^x
        # env: global

        f <- function() quo(x)
        f()
        # <quosure>
        # expr: ^x
        # env: 0x55b7e61d5c80


        Keeping expressions next to their environments allows you to ensure that they are always evaluated in a consistent fashion, as they make their way through your code:



        x <- 5
        g <- function( myexpr )
        x <- 10
        eval_tidy( myexpr )


        x # Evaluate expression directly in its environment
        # 5

        g( quote(x) ) # Evaluate expression inside g()
        # 10

        g( quo(x) ) # Evaluate expression in its env, while inside g()
        # 5


        However, when capturing a literal inside a quosure, quo assigns an empty environment to it:



        quo("x")
        # <quosure>
        # expr: ^"x"
        # env: empty


        That is because the string "x" will always evaluate to "x", regardless of what environment it's evaluated in. Because of this, there is almost never a good reason to quo a string object (or any literal for that matter). It doesn't do anything and, as pointed out in the comments, your code will work fine without it.






        share|improve this answer












        Non-standard evaluation allows you to capture and manipulate expressions. In base R, this is primarily accomplished through the use of quote:



        quote(x)
        # x

        quote( a*log10(b+5) )
        # a * log10(b + 5)


        However, any captured expression that consists of a single literal is itself that literal:



        identical( quote(5), 5 ) # TRUE
        identical( quote("a"), "a" ) # TRUE
        identical( quote(FALSE), FALSE ) # TRUE
        identical( quote(5+5), 10 ) # FALSE, expression is not a single literal


        rlang::quo from tidyverse builds on this functionality by capturing an expression AND the environment that gave rise to that expression. Together, these define a quosure:



        quo(x)
        # <quosure>
        # expr: ^x
        # env: global

        f <- function() quo(x)
        f()
        # <quosure>
        # expr: ^x
        # env: 0x55b7e61d5c80


        Keeping expressions next to their environments allows you to ensure that they are always evaluated in a consistent fashion, as they make their way through your code:



        x <- 5
        g <- function( myexpr )
        x <- 10
        eval_tidy( myexpr )


        x # Evaluate expression directly in its environment
        # 5

        g( quote(x) ) # Evaluate expression inside g()
        # 10

        g( quo(x) ) # Evaluate expression in its env, while inside g()
        # 5


        However, when capturing a literal inside a quosure, quo assigns an empty environment to it:



        quo("x")
        # <quosure>
        # expr: ^"x"
        # env: empty


        That is because the string "x" will always evaluate to "x", regardless of what environment it's evaluated in. Because of this, there is almost never a good reason to quo a string object (or any literal for that matter). It doesn't do anything and, as pointed out in the comments, your code will work fine without it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 21:13









        Artem Sokolov

        4,69221936




        4,69221936



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238792%2fwhat-is-the-correct-way-to-put-quoted-strings-in-rlang-functions%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20