How to check if object (variable) is defined in R?












219















I'd like to check if some variable is defined in R - without getting an error. How can I do this?



My attempts (not successful):



> is.na(ooxx)
Error: object 'ooxx' not found
> is.finite(ooxx)
Error: object 'ooxx' not found


Thanks!










share|improve this question





























    219















    I'd like to check if some variable is defined in R - without getting an error. How can I do this?



    My attempts (not successful):



    > is.na(ooxx)
    Error: object 'ooxx' not found
    > is.finite(ooxx)
    Error: object 'ooxx' not found


    Thanks!










    share|improve this question



























      219












      219








      219


      28






      I'd like to check if some variable is defined in R - without getting an error. How can I do this?



      My attempts (not successful):



      > is.na(ooxx)
      Error: object 'ooxx' not found
      > is.finite(ooxx)
      Error: object 'ooxx' not found


      Thanks!










      share|improve this question
















      I'd like to check if some variable is defined in R - without getting an error. How can I do this?



      My attempts (not successful):



      > is.na(ooxx)
      Error: object 'ooxx' not found
      > is.finite(ooxx)
      Error: object 'ooxx' not found


      Thanks!







      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 1 '16 at 14:16







      TMS

















      asked Feb 20 '12 at 21:46









      TMSTMS

      33.5k37170303




      33.5k37170303
























          5 Answers
          5






          active

          oldest

          votes


















          360














          You want exists():



          R> exists("somethingUnknown")
          [1] FALSE
          R> somethingUnknown <- 42
          R> exists("somethingUnknown")
          [1] TRUE
          R>





          share|improve this answer



















          • 1





            @Gavin & Dirk, you're so nice to each other :) Only solution is that you toss the coin (Bernoulli with p=0.5 :-)) who will get the accept! :-)

            – TMS
            Feb 20 '12 at 22:00






          • 24





            @tim if you are inside a function, missing() is what you want.

            – CousinCocaine
            Jan 27 '14 at 14:31






          • 2





            Might get a bit trickier if checking for list elements: stackoverflow.com/q/7719741

            – TMS
            Sep 20 '14 at 11:58






          • 2





            what about for what the op wanted - using the variable name, not in quotes?

            – tim
            Jun 13 '15 at 13:46



















          93














          See ?exists, for some definition of "...is defined". E.g.



          > exists("foo")
          [1] FALSE
          > foo <- 1:10
          > exists("foo")
          [1] TRUE





          share|improve this answer



















          • 7





            You win by 52 seconds :)

            – Dirk Eddelbuettel
            Feb 20 '12 at 21:51






          • 6





            @DirkEddelbuettel Well, if you will use ridiculously long object names ;-)

            – Gavin Simpson
            Feb 20 '12 at 21:54






          • 2





            heh. Happens to me all the time when I am testing out examples before posting, Gavin or Josh have already answered it.

            – Maiasaura
            Feb 20 '12 at 22:17



















          50














          if you are inside a function, missing() is what you want.



          exchequer = function(x) {
          if(missing(x)){
          message("x is missing… :-(")
          }
          }

          exchequer()
          x is missing… :-(





          share|improve this answer































            37














            As others have pointed out, you're looking for exists. Keep in mind that using exists with names used by R's base packages would return true regardless of whether you defined the variable:



            > exists("data")
            [1] TRUE


            To get around this (as pointed out by Bazz; see ?exists), use the inherits argument:



            > exists("data", inherits = FALSE)
            [1] FALSE

            foo <- TRUE
            > exists("foo", inherits = FALSE)
            [1] TRUE


            Of course, if you wanted to search the name spaces of attached packages, this would also fall short:



            > exists("data.table")
            [1] FALSE
            require(data.table)
            > exists("data.table", inherits = FALSE)
            [1] FALSE
            > exists("data.table")
            [1] TRUE


            The only thing I can think of to get around this -- to search in attached packages but not in base packages -- is the following:



            any(sapply(1:(which(search() == "tools:rstudio") - 1L),
            function(pp) exists(_object_name_, where = pp, inherits = FALSE)))


            Compare replacing _object_name_ with "data.table" (TRUE) vs. "var" (FALSE)



            (of course, if you're not on RStudio, I think the first automatically attached environment is "package:stats")






            share|improve this answer





















            • 1





              Playing around, using argument inherits = FALSE seems to isolate things in the global environment. Does that sound right?

              – CJB
              Jan 7 '16 at 12:49











            • @Bazz you're correct; I've edited this into the answer.

              – MichaelChirico
              Feb 3 '16 at 3:21






            • 1





              This comment should be higher up, since I use variable name "data", just using exist gave me some trouble initially.

              – mzm
              May 11 '16 at 14:44



















            14














            If you don't want to use quotes, you can use deparse(substitute()) trick which I found in example section of ?substitute:



            is.defined <- function(sym) {
            sym <- deparse(substitute(sym))
            env <- parent.frame()
            exists(sym, env)
            }

            is.defined(a)
            # FALSE
            a <- 10
            is.defined(a)
            # TRUE





            share|improve this answer


























            • I wish people knew how sick this was. Good work.

              – Carl Boneri
              Jun 27 '17 at 18:36











            • you can also force or evaluate it in the function like this: is.defined <- function(sym) class(try(sym, TRUE))!='try-error'

              – chinsoon12
              Oct 4 '17 at 0:49











            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',
            autoActivateHeartbeat: false,
            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%2f9368900%2fhow-to-check-if-object-variable-is-defined-in-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            360














            You want exists():



            R> exists("somethingUnknown")
            [1] FALSE
            R> somethingUnknown <- 42
            R> exists("somethingUnknown")
            [1] TRUE
            R>





            share|improve this answer



















            • 1





              @Gavin & Dirk, you're so nice to each other :) Only solution is that you toss the coin (Bernoulli with p=0.5 :-)) who will get the accept! :-)

              – TMS
              Feb 20 '12 at 22:00






            • 24





              @tim if you are inside a function, missing() is what you want.

              – CousinCocaine
              Jan 27 '14 at 14:31






            • 2





              Might get a bit trickier if checking for list elements: stackoverflow.com/q/7719741

              – TMS
              Sep 20 '14 at 11:58






            • 2





              what about for what the op wanted - using the variable name, not in quotes?

              – tim
              Jun 13 '15 at 13:46
















            360














            You want exists():



            R> exists("somethingUnknown")
            [1] FALSE
            R> somethingUnknown <- 42
            R> exists("somethingUnknown")
            [1] TRUE
            R>





            share|improve this answer



















            • 1





              @Gavin & Dirk, you're so nice to each other :) Only solution is that you toss the coin (Bernoulli with p=0.5 :-)) who will get the accept! :-)

              – TMS
              Feb 20 '12 at 22:00






            • 24





              @tim if you are inside a function, missing() is what you want.

              – CousinCocaine
              Jan 27 '14 at 14:31






            • 2





              Might get a bit trickier if checking for list elements: stackoverflow.com/q/7719741

              – TMS
              Sep 20 '14 at 11:58






            • 2





              what about for what the op wanted - using the variable name, not in quotes?

              – tim
              Jun 13 '15 at 13:46














            360












            360








            360







            You want exists():



            R> exists("somethingUnknown")
            [1] FALSE
            R> somethingUnknown <- 42
            R> exists("somethingUnknown")
            [1] TRUE
            R>





            share|improve this answer













            You want exists():



            R> exists("somethingUnknown")
            [1] FALSE
            R> somethingUnknown <- 42
            R> exists("somethingUnknown")
            [1] TRUE
            R>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 20 '12 at 21:51









            Dirk EddelbuettelDirk Eddelbuettel

            277k37513602




            277k37513602








            • 1





              @Gavin & Dirk, you're so nice to each other :) Only solution is that you toss the coin (Bernoulli with p=0.5 :-)) who will get the accept! :-)

              – TMS
              Feb 20 '12 at 22:00






            • 24





              @tim if you are inside a function, missing() is what you want.

              – CousinCocaine
              Jan 27 '14 at 14:31






            • 2





              Might get a bit trickier if checking for list elements: stackoverflow.com/q/7719741

              – TMS
              Sep 20 '14 at 11:58






            • 2





              what about for what the op wanted - using the variable name, not in quotes?

              – tim
              Jun 13 '15 at 13:46














            • 1





              @Gavin & Dirk, you're so nice to each other :) Only solution is that you toss the coin (Bernoulli with p=0.5 :-)) who will get the accept! :-)

              – TMS
              Feb 20 '12 at 22:00






            • 24





              @tim if you are inside a function, missing() is what you want.

              – CousinCocaine
              Jan 27 '14 at 14:31






            • 2





              Might get a bit trickier if checking for list elements: stackoverflow.com/q/7719741

              – TMS
              Sep 20 '14 at 11:58






            • 2





              what about for what the op wanted - using the variable name, not in quotes?

              – tim
              Jun 13 '15 at 13:46








            1




            1





            @Gavin & Dirk, you're so nice to each other :) Only solution is that you toss the coin (Bernoulli with p=0.5 :-)) who will get the accept! :-)

            – TMS
            Feb 20 '12 at 22:00





            @Gavin & Dirk, you're so nice to each other :) Only solution is that you toss the coin (Bernoulli with p=0.5 :-)) who will get the accept! :-)

            – TMS
            Feb 20 '12 at 22:00




            24




            24





            @tim if you are inside a function, missing() is what you want.

            – CousinCocaine
            Jan 27 '14 at 14:31





            @tim if you are inside a function, missing() is what you want.

            – CousinCocaine
            Jan 27 '14 at 14:31




            2




            2





            Might get a bit trickier if checking for list elements: stackoverflow.com/q/7719741

            – TMS
            Sep 20 '14 at 11:58





            Might get a bit trickier if checking for list elements: stackoverflow.com/q/7719741

            – TMS
            Sep 20 '14 at 11:58




            2




            2





            what about for what the op wanted - using the variable name, not in quotes?

            – tim
            Jun 13 '15 at 13:46





            what about for what the op wanted - using the variable name, not in quotes?

            – tim
            Jun 13 '15 at 13:46













            93














            See ?exists, for some definition of "...is defined". E.g.



            > exists("foo")
            [1] FALSE
            > foo <- 1:10
            > exists("foo")
            [1] TRUE





            share|improve this answer



















            • 7





              You win by 52 seconds :)

              – Dirk Eddelbuettel
              Feb 20 '12 at 21:51






            • 6





              @DirkEddelbuettel Well, if you will use ridiculously long object names ;-)

              – Gavin Simpson
              Feb 20 '12 at 21:54






            • 2





              heh. Happens to me all the time when I am testing out examples before posting, Gavin or Josh have already answered it.

              – Maiasaura
              Feb 20 '12 at 22:17
















            93














            See ?exists, for some definition of "...is defined". E.g.



            > exists("foo")
            [1] FALSE
            > foo <- 1:10
            > exists("foo")
            [1] TRUE





            share|improve this answer



















            • 7





              You win by 52 seconds :)

              – Dirk Eddelbuettel
              Feb 20 '12 at 21:51






            • 6





              @DirkEddelbuettel Well, if you will use ridiculously long object names ;-)

              – Gavin Simpson
              Feb 20 '12 at 21:54






            • 2





              heh. Happens to me all the time when I am testing out examples before posting, Gavin or Josh have already answered it.

              – Maiasaura
              Feb 20 '12 at 22:17














            93












            93








            93







            See ?exists, for some definition of "...is defined". E.g.



            > exists("foo")
            [1] FALSE
            > foo <- 1:10
            > exists("foo")
            [1] TRUE





            share|improve this answer













            See ?exists, for some definition of "...is defined". E.g.



            > exists("foo")
            [1] FALSE
            > foo <- 1:10
            > exists("foo")
            [1] TRUE






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 20 '12 at 21:50









            Gavin SimpsonGavin Simpson

            135k19306383




            135k19306383








            • 7





              You win by 52 seconds :)

              – Dirk Eddelbuettel
              Feb 20 '12 at 21:51






            • 6





              @DirkEddelbuettel Well, if you will use ridiculously long object names ;-)

              – Gavin Simpson
              Feb 20 '12 at 21:54






            • 2





              heh. Happens to me all the time when I am testing out examples before posting, Gavin or Josh have already answered it.

              – Maiasaura
              Feb 20 '12 at 22:17














            • 7





              You win by 52 seconds :)

              – Dirk Eddelbuettel
              Feb 20 '12 at 21:51






            • 6





              @DirkEddelbuettel Well, if you will use ridiculously long object names ;-)

              – Gavin Simpson
              Feb 20 '12 at 21:54






            • 2





              heh. Happens to me all the time when I am testing out examples before posting, Gavin or Josh have already answered it.

              – Maiasaura
              Feb 20 '12 at 22:17








            7




            7





            You win by 52 seconds :)

            – Dirk Eddelbuettel
            Feb 20 '12 at 21:51





            You win by 52 seconds :)

            – Dirk Eddelbuettel
            Feb 20 '12 at 21:51




            6




            6





            @DirkEddelbuettel Well, if you will use ridiculously long object names ;-)

            – Gavin Simpson
            Feb 20 '12 at 21:54





            @DirkEddelbuettel Well, if you will use ridiculously long object names ;-)

            – Gavin Simpson
            Feb 20 '12 at 21:54




            2




            2





            heh. Happens to me all the time when I am testing out examples before posting, Gavin or Josh have already answered it.

            – Maiasaura
            Feb 20 '12 at 22:17





            heh. Happens to me all the time when I am testing out examples before posting, Gavin or Josh have already answered it.

            – Maiasaura
            Feb 20 '12 at 22:17











            50














            if you are inside a function, missing() is what you want.



            exchequer = function(x) {
            if(missing(x)){
            message("x is missing… :-(")
            }
            }

            exchequer()
            x is missing… :-(





            share|improve this answer




























              50














              if you are inside a function, missing() is what you want.



              exchequer = function(x) {
              if(missing(x)){
              message("x is missing… :-(")
              }
              }

              exchequer()
              x is missing… :-(





              share|improve this answer


























                50












                50








                50







                if you are inside a function, missing() is what you want.



                exchequer = function(x) {
                if(missing(x)){
                message("x is missing… :-(")
                }
                }

                exchequer()
                x is missing… :-(





                share|improve this answer













                if you are inside a function, missing() is what you want.



                exchequer = function(x) {
                if(missing(x)){
                message("x is missing… :-(")
                }
                }

                exchequer()
                x is missing… :-(






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 9 '13 at 21:56









                timtim

                1,8651733




                1,8651733























                    37














                    As others have pointed out, you're looking for exists. Keep in mind that using exists with names used by R's base packages would return true regardless of whether you defined the variable:



                    > exists("data")
                    [1] TRUE


                    To get around this (as pointed out by Bazz; see ?exists), use the inherits argument:



                    > exists("data", inherits = FALSE)
                    [1] FALSE

                    foo <- TRUE
                    > exists("foo", inherits = FALSE)
                    [1] TRUE


                    Of course, if you wanted to search the name spaces of attached packages, this would also fall short:



                    > exists("data.table")
                    [1] FALSE
                    require(data.table)
                    > exists("data.table", inherits = FALSE)
                    [1] FALSE
                    > exists("data.table")
                    [1] TRUE


                    The only thing I can think of to get around this -- to search in attached packages but not in base packages -- is the following:



                    any(sapply(1:(which(search() == "tools:rstudio") - 1L),
                    function(pp) exists(_object_name_, where = pp, inherits = FALSE)))


                    Compare replacing _object_name_ with "data.table" (TRUE) vs. "var" (FALSE)



                    (of course, if you're not on RStudio, I think the first automatically attached environment is "package:stats")






                    share|improve this answer





















                    • 1





                      Playing around, using argument inherits = FALSE seems to isolate things in the global environment. Does that sound right?

                      – CJB
                      Jan 7 '16 at 12:49











                    • @Bazz you're correct; I've edited this into the answer.

                      – MichaelChirico
                      Feb 3 '16 at 3:21






                    • 1





                      This comment should be higher up, since I use variable name "data", just using exist gave me some trouble initially.

                      – mzm
                      May 11 '16 at 14:44
















                    37














                    As others have pointed out, you're looking for exists. Keep in mind that using exists with names used by R's base packages would return true regardless of whether you defined the variable:



                    > exists("data")
                    [1] TRUE


                    To get around this (as pointed out by Bazz; see ?exists), use the inherits argument:



                    > exists("data", inherits = FALSE)
                    [1] FALSE

                    foo <- TRUE
                    > exists("foo", inherits = FALSE)
                    [1] TRUE


                    Of course, if you wanted to search the name spaces of attached packages, this would also fall short:



                    > exists("data.table")
                    [1] FALSE
                    require(data.table)
                    > exists("data.table", inherits = FALSE)
                    [1] FALSE
                    > exists("data.table")
                    [1] TRUE


                    The only thing I can think of to get around this -- to search in attached packages but not in base packages -- is the following:



                    any(sapply(1:(which(search() == "tools:rstudio") - 1L),
                    function(pp) exists(_object_name_, where = pp, inherits = FALSE)))


                    Compare replacing _object_name_ with "data.table" (TRUE) vs. "var" (FALSE)



                    (of course, if you're not on RStudio, I think the first automatically attached environment is "package:stats")






                    share|improve this answer





















                    • 1





                      Playing around, using argument inherits = FALSE seems to isolate things in the global environment. Does that sound right?

                      – CJB
                      Jan 7 '16 at 12:49











                    • @Bazz you're correct; I've edited this into the answer.

                      – MichaelChirico
                      Feb 3 '16 at 3:21






                    • 1





                      This comment should be higher up, since I use variable name "data", just using exist gave me some trouble initially.

                      – mzm
                      May 11 '16 at 14:44














                    37












                    37








                    37







                    As others have pointed out, you're looking for exists. Keep in mind that using exists with names used by R's base packages would return true regardless of whether you defined the variable:



                    > exists("data")
                    [1] TRUE


                    To get around this (as pointed out by Bazz; see ?exists), use the inherits argument:



                    > exists("data", inherits = FALSE)
                    [1] FALSE

                    foo <- TRUE
                    > exists("foo", inherits = FALSE)
                    [1] TRUE


                    Of course, if you wanted to search the name spaces of attached packages, this would also fall short:



                    > exists("data.table")
                    [1] FALSE
                    require(data.table)
                    > exists("data.table", inherits = FALSE)
                    [1] FALSE
                    > exists("data.table")
                    [1] TRUE


                    The only thing I can think of to get around this -- to search in attached packages but not in base packages -- is the following:



                    any(sapply(1:(which(search() == "tools:rstudio") - 1L),
                    function(pp) exists(_object_name_, where = pp, inherits = FALSE)))


                    Compare replacing _object_name_ with "data.table" (TRUE) vs. "var" (FALSE)



                    (of course, if you're not on RStudio, I think the first automatically attached environment is "package:stats")






                    share|improve this answer















                    As others have pointed out, you're looking for exists. Keep in mind that using exists with names used by R's base packages would return true regardless of whether you defined the variable:



                    > exists("data")
                    [1] TRUE


                    To get around this (as pointed out by Bazz; see ?exists), use the inherits argument:



                    > exists("data", inherits = FALSE)
                    [1] FALSE

                    foo <- TRUE
                    > exists("foo", inherits = FALSE)
                    [1] TRUE


                    Of course, if you wanted to search the name spaces of attached packages, this would also fall short:



                    > exists("data.table")
                    [1] FALSE
                    require(data.table)
                    > exists("data.table", inherits = FALSE)
                    [1] FALSE
                    > exists("data.table")
                    [1] TRUE


                    The only thing I can think of to get around this -- to search in attached packages but not in base packages -- is the following:



                    any(sapply(1:(which(search() == "tools:rstudio") - 1L),
                    function(pp) exists(_object_name_, where = pp, inherits = FALSE)))


                    Compare replacing _object_name_ with "data.table" (TRUE) vs. "var" (FALSE)



                    (of course, if you're not on RStudio, I think the first automatically attached environment is "package:stats")







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 3 '16 at 3:37









                    MichaelChirico

                    20k861111




                    20k861111










                    answered Sep 24 '15 at 16:19









                    Santiago BaldrichSantiago Baldrich

                    538510




                    538510








                    • 1





                      Playing around, using argument inherits = FALSE seems to isolate things in the global environment. Does that sound right?

                      – CJB
                      Jan 7 '16 at 12:49











                    • @Bazz you're correct; I've edited this into the answer.

                      – MichaelChirico
                      Feb 3 '16 at 3:21






                    • 1





                      This comment should be higher up, since I use variable name "data", just using exist gave me some trouble initially.

                      – mzm
                      May 11 '16 at 14:44














                    • 1





                      Playing around, using argument inherits = FALSE seems to isolate things in the global environment. Does that sound right?

                      – CJB
                      Jan 7 '16 at 12:49











                    • @Bazz you're correct; I've edited this into the answer.

                      – MichaelChirico
                      Feb 3 '16 at 3:21






                    • 1





                      This comment should be higher up, since I use variable name "data", just using exist gave me some trouble initially.

                      – mzm
                      May 11 '16 at 14:44








                    1




                    1





                    Playing around, using argument inherits = FALSE seems to isolate things in the global environment. Does that sound right?

                    – CJB
                    Jan 7 '16 at 12:49





                    Playing around, using argument inherits = FALSE seems to isolate things in the global environment. Does that sound right?

                    – CJB
                    Jan 7 '16 at 12:49













                    @Bazz you're correct; I've edited this into the answer.

                    – MichaelChirico
                    Feb 3 '16 at 3:21





                    @Bazz you're correct; I've edited this into the answer.

                    – MichaelChirico
                    Feb 3 '16 at 3:21




                    1




                    1





                    This comment should be higher up, since I use variable name "data", just using exist gave me some trouble initially.

                    – mzm
                    May 11 '16 at 14:44





                    This comment should be higher up, since I use variable name "data", just using exist gave me some trouble initially.

                    – mzm
                    May 11 '16 at 14:44











                    14














                    If you don't want to use quotes, you can use deparse(substitute()) trick which I found in example section of ?substitute:



                    is.defined <- function(sym) {
                    sym <- deparse(substitute(sym))
                    env <- parent.frame()
                    exists(sym, env)
                    }

                    is.defined(a)
                    # FALSE
                    a <- 10
                    is.defined(a)
                    # TRUE





                    share|improve this answer


























                    • I wish people knew how sick this was. Good work.

                      – Carl Boneri
                      Jun 27 '17 at 18:36











                    • you can also force or evaluate it in the function like this: is.defined <- function(sym) class(try(sym, TRUE))!='try-error'

                      – chinsoon12
                      Oct 4 '17 at 0:49
















                    14














                    If you don't want to use quotes, you can use deparse(substitute()) trick which I found in example section of ?substitute:



                    is.defined <- function(sym) {
                    sym <- deparse(substitute(sym))
                    env <- parent.frame()
                    exists(sym, env)
                    }

                    is.defined(a)
                    # FALSE
                    a <- 10
                    is.defined(a)
                    # TRUE





                    share|improve this answer


























                    • I wish people knew how sick this was. Good work.

                      – Carl Boneri
                      Jun 27 '17 at 18:36











                    • you can also force or evaluate it in the function like this: is.defined <- function(sym) class(try(sym, TRUE))!='try-error'

                      – chinsoon12
                      Oct 4 '17 at 0:49














                    14












                    14








                    14







                    If you don't want to use quotes, you can use deparse(substitute()) trick which I found in example section of ?substitute:



                    is.defined <- function(sym) {
                    sym <- deparse(substitute(sym))
                    env <- parent.frame()
                    exists(sym, env)
                    }

                    is.defined(a)
                    # FALSE
                    a <- 10
                    is.defined(a)
                    # TRUE





                    share|improve this answer















                    If you don't want to use quotes, you can use deparse(substitute()) trick which I found in example section of ?substitute:



                    is.defined <- function(sym) {
                    sym <- deparse(substitute(sym))
                    env <- parent.frame()
                    exists(sym, env)
                    }

                    is.defined(a)
                    # FALSE
                    a <- 10
                    is.defined(a)
                    # TRUE






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Apr 17 '17 at 8:40

























                    answered Apr 17 '17 at 6:32









                    NirmalNirmal

                    19115




                    19115













                    • I wish people knew how sick this was. Good work.

                      – Carl Boneri
                      Jun 27 '17 at 18:36











                    • you can also force or evaluate it in the function like this: is.defined <- function(sym) class(try(sym, TRUE))!='try-error'

                      – chinsoon12
                      Oct 4 '17 at 0:49



















                    • I wish people knew how sick this was. Good work.

                      – Carl Boneri
                      Jun 27 '17 at 18:36











                    • you can also force or evaluate it in the function like this: is.defined <- function(sym) class(try(sym, TRUE))!='try-error'

                      – chinsoon12
                      Oct 4 '17 at 0:49

















                    I wish people knew how sick this was. Good work.

                    – Carl Boneri
                    Jun 27 '17 at 18:36





                    I wish people knew how sick this was. Good work.

                    – Carl Boneri
                    Jun 27 '17 at 18:36













                    you can also force or evaluate it in the function like this: is.defined <- function(sym) class(try(sym, TRUE))!='try-error'

                    – chinsoon12
                    Oct 4 '17 at 0:49





                    you can also force or evaluate it in the function like this: is.defined <- function(sym) class(try(sym, TRUE))!='try-error'

                    – chinsoon12
                    Oct 4 '17 at 0:49


















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9368900%2fhow-to-check-if-object-variable-is-defined-in-r%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

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga