Trycatch in for loop- continue to next r dataRetrieval










1














I have a list containing the following site id numbers:
sitelist <- c("02074500", "02077200", "208111310", "02081500", "02082950")



I want to use the dataRetrieval package to collect additional information about these sites and save it into individual .csv files. Site number "208111310" does not exist, so it returns an error and stops the code.



I want the code to ignore site numbers that do not return data and continue to the next number in sitelist.
I've tried trycatch in several ways but can't get the correct syntax. Here is my for loop without trycatch.



for (i in sitelist)
test_gage <- readNWISdv(siteNumbers = i,
parameterCd = pCode)

df = test_gage
df = subset(df, select= c(site_no, Date, X_00060_00003))
names(df)[3] <- c("flow in m3/s")
df$Year <- as.character(year(df$Date))

write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
rm(list=setdiff(ls(),c("sitelist", "pCode")))










share|improve this question




























    1














    I have a list containing the following site id numbers:
    sitelist <- c("02074500", "02077200", "208111310", "02081500", "02082950")



    I want to use the dataRetrieval package to collect additional information about these sites and save it into individual .csv files. Site number "208111310" does not exist, so it returns an error and stops the code.



    I want the code to ignore site numbers that do not return data and continue to the next number in sitelist.
    I've tried trycatch in several ways but can't get the correct syntax. Here is my for loop without trycatch.



    for (i in sitelist)
    test_gage <- readNWISdv(siteNumbers = i,
    parameterCd = pCode)

    df = test_gage
    df = subset(df, select= c(site_no, Date, X_00060_00003))
    names(df)[3] <- c("flow in m3/s")
    df$Year <- as.character(year(df$Date))

    write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
    rm(list=setdiff(ls(),c("sitelist", "pCode")))










    share|improve this question


























      1












      1








      1







      I have a list containing the following site id numbers:
      sitelist <- c("02074500", "02077200", "208111310", "02081500", "02082950")



      I want to use the dataRetrieval package to collect additional information about these sites and save it into individual .csv files. Site number "208111310" does not exist, so it returns an error and stops the code.



      I want the code to ignore site numbers that do not return data and continue to the next number in sitelist.
      I've tried trycatch in several ways but can't get the correct syntax. Here is my for loop without trycatch.



      for (i in sitelist)
      test_gage <- readNWISdv(siteNumbers = i,
      parameterCd = pCode)

      df = test_gage
      df = subset(df, select= c(site_no, Date, X_00060_00003))
      names(df)[3] <- c("flow in m3/s")
      df$Year <- as.character(year(df$Date))

      write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
      rm(list=setdiff(ls(),c("sitelist", "pCode")))










      share|improve this question















      I have a list containing the following site id numbers:
      sitelist <- c("02074500", "02077200", "208111310", "02081500", "02082950")



      I want to use the dataRetrieval package to collect additional information about these sites and save it into individual .csv files. Site number "208111310" does not exist, so it returns an error and stops the code.



      I want the code to ignore site numbers that do not return data and continue to the next number in sitelist.
      I've tried trycatch in several ways but can't get the correct syntax. Here is my for loop without trycatch.



      for (i in sitelist)
      test_gage <- readNWISdv(siteNumbers = i,
      parameterCd = pCode)

      df = test_gage
      df = subset(df, select= c(site_no, Date, X_00060_00003))
      names(df)[3] <- c("flow in m3/s")
      df$Year <- as.character(year(df$Date))

      write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
      rm(list=setdiff(ls(),c("sitelist", "pCode")))







      r try-catch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 3:37









      prosoitos

      935219




      935219










      asked Nov 13 '18 at 2:35









      HeyBales

      84




      84






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use the variable error in the function trycatch to specify what happened when an error occurs and store the return value using operator <<-.



          for (i in sitelist) 
          test_gage <- NULL
          trycatch(error=function(message)
          test_gage <<- readNWISdv(siteNumbers = i,parameterCd = pCode)

          df = test_gage
          df = subset(df, select= c(site_no, Date, X_00060_00003))
          names(df)[3] <- c("flow in m3/s")
          df$Year <- as.character(year(df$Date)) write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
          rm(list=setdiff(ls(),c("sitelist", "pCode")))



          If you want to catch the warnings also just give a second argument to trycatch.



          trycatch(error=function(),warning=function())





          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',
            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%2f53272956%2ftrycatch-in-for-loop-continue-to-next-r-dataretrieval%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









            1














            You can use the variable error in the function trycatch to specify what happened when an error occurs and store the return value using operator <<-.



            for (i in sitelist) 
            test_gage <- NULL
            trycatch(error=function(message)
            test_gage <<- readNWISdv(siteNumbers = i,parameterCd = pCode)

            df = test_gage
            df = subset(df, select= c(site_no, Date, X_00060_00003))
            names(df)[3] <- c("flow in m3/s")
            df$Year <- as.character(year(df$Date)) write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
            rm(list=setdiff(ls(),c("sitelist", "pCode")))



            If you want to catch the warnings also just give a second argument to trycatch.



            trycatch(error=function(),warning=function())





            share|improve this answer

























              1














              You can use the variable error in the function trycatch to specify what happened when an error occurs and store the return value using operator <<-.



              for (i in sitelist) 
              test_gage <- NULL
              trycatch(error=function(message)
              test_gage <<- readNWISdv(siteNumbers = i,parameterCd = pCode)

              df = test_gage
              df = subset(df, select= c(site_no, Date, X_00060_00003))
              names(df)[3] <- c("flow in m3/s")
              df$Year <- as.character(year(df$Date)) write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
              rm(list=setdiff(ls(),c("sitelist", "pCode")))



              If you want to catch the warnings also just give a second argument to trycatch.



              trycatch(error=function(),warning=function())





              share|improve this answer























                1












                1








                1






                You can use the variable error in the function trycatch to specify what happened when an error occurs and store the return value using operator <<-.



                for (i in sitelist) 
                test_gage <- NULL
                trycatch(error=function(message)
                test_gage <<- readNWISdv(siteNumbers = i,parameterCd = pCode)

                df = test_gage
                df = subset(df, select= c(site_no, Date, X_00060_00003))
                names(df)[3] <- c("flow in m3/s")
                df$Year <- as.character(year(df$Date)) write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
                rm(list=setdiff(ls(),c("sitelist", "pCode")))



                If you want to catch the warnings also just give a second argument to trycatch.



                trycatch(error=function(),warning=function())





                share|improve this answer












                You can use the variable error in the function trycatch to specify what happened when an error occurs and store the return value using operator <<-.



                for (i in sitelist) 
                test_gage <- NULL
                trycatch(error=function(message)
                test_gage <<- readNWISdv(siteNumbers = i,parameterCd = pCode)

                df = test_gage
                df = subset(df, select= c(site_no, Date, X_00060_00003))
                names(df)[3] <- c("flow in m3/s")
                df$Year <- as.character(year(df$Date)) write.csv(df, paste0("./gage_flow/",i,".csv"), row.names = F)
                rm(list=setdiff(ls(),c("sitelist", "pCode")))



                If you want to catch the warnings also just give a second argument to trycatch.



                trycatch(error=function(),warning=function())






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 3:39









                Csd

                29819




                29819



























                    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%2f53272956%2ftrycatch-in-for-loop-continue-to-next-r-dataretrieval%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

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党