{"id":3018,"date":"2023-07-19T15:05:42","date_gmt":"2023-07-19T15:05:42","guid":{"rendered":"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/"},"modified":"2023-07-19T15:05:42","modified_gmt":"2023-07-19T15:05:42","slug":"probeer-het-nog-eens","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/","title":{"rendered":"Hoe u uw eerste trycatch()-functie schrijft in r"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><span style=\"color: #000000;\">U kunt een <strong>tryCatch()<\/strong> -functie in R gebruiken om de waarde van een expressie te retourneren of een aangepast bericht te genereren als er een waarschuwing of fout wordt aangetroffen.<\/span><\/span><\/p>\n<p> <span style=\"color: #000000;\">Deze functie gebruikt de volgende basissyntaxis:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>my_function &lt;- <span style=\"color: #008000;\">function<\/span> (x, y){\n    <span style=\"color: #008000;\">tryCatch<\/span> (\n        <span style=\"color: #008080;\">#try to do this<\/span>\n        {\n        #some expression\n        },\n        <span style=\"color: #008080;\">#if an error occurs, tell me the error<\/span>\n        error= <span style=\"color: #008000;\">function<\/span> (e) {\n            message(' <span style=\"color: #ff0000;\">An Error Occurred<\/span> ')\n            print(e)\n        },\n        <span style=\"color: #008080;\">#if a warning occurs, tell me the warning<\/span>\n        warning= <span style=\"color: #008000;\">function<\/span> (w) {\n            message(' <span style=\"color: #ff0000;\">A Warning Occurred<\/span> ')\n            print(w)\n            <span style=\"color: #008000;\">return<\/span> (NA)\n        }\n    )\n}\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De volgende voorbeelden laten zien hoe u een <strong>tryCatch()-<\/strong> functie in de praktijk kunt gebruiken.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Voorbeeld: Maak een tryCatch()-functie in R<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Laten we zeggen dat we de volgende <strong>tryCatch()-<\/strong> functie maken die probeert de log van \u00e9\u00e9n waarde te nemen en deze vervolgens door een tweede waarde te delen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Als er een fout optreedt, geven we het bericht &#8222;Er is een fout opgetreden&#8220; weer en drukken we de fout vervolgens af naar R.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Als er een waarschuwing optreedt, geven we het bericht &#8218;Er is een waarschuwing opgetreden&#8216; weer, drukken we de waarschuwing af naar R en retourneren we vervolgens een NA-waarde.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Als er geen fouten of waarschuwingen optreden, retourneren we eenvoudigweg het functieresultaat.<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\">log_and_divide &lt;- <span style=\"color: #008000;\">function<\/span> (x, y){\n    <span style=\"color: #008000;\">tryCatch<\/span> (\n        {\n        result = log(x) \/ y\n        <span style=\"color: #008000;\">return<\/span> (result)\n        },\n        error= <span style=\"color: #008000;\">function<\/span> (e) {\n            message(' <span style=\"color: #ff0000;\">An Error Occurred<\/span> ')\n            print(e)\n        },\n        warning= <span style=\"color: #008000;\">function<\/span> (w) {\n            message(' <span style=\"color: #ff0000;\">A Warning Occurred<\/span> ')\n            print(w)\n            <span style=\"color: #008000;\">return<\/span> (NA)\n        }\n    )\n}\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Laten we deze functie in verschillende scenario&#8217;s uitvoeren.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Scenario 1: Er treden geen fouten of waarschuwingen op.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u de functie kunt gebruiken in een scenario waarin geen fouten of waarschuwingen optreden.<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#run function<\/span>\nlog_and_divide(10, 2)\n\n[1] 1.151293\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Omdat er geen fouten of waarschuwingen optreden, retourneert de functie eenvoudigweg het resultaat van de expressie, dat <strong>1.151293<\/strong> blijkt te zijn.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Scenario 2: Er treedt een fout op.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u de functie kunt gebruiken in een scenario waarin een fout optreedt:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#run function\n<\/span>log_and_divide(10)\n\nAn Error Occurred\n&lt;simpleError in doTryCatch(return(expr), name, parentenv, handler):\n  argument \"y\" is missing, with no default&gt;\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Omdat we slechts \u00e9\u00e9n argument aan de functie hebben opgegeven, ontvangen we het bericht &#8220; <strong>Er is een fout opgetreden<\/strong> &#8220; en zien we ook de exacte fout die door R wordt geproduceerd.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Scenario 3: Er treedt een waarschuwing op.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">De volgende code laat zien hoe u de functie kunt gebruiken in een scenario waarin een waarschuwing optreedt:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\"><span style=\"color: #000000;\"><span style=\"color: #008080;\">#run function\n<\/span>log_and_divide(-10, 2)\n\nA Warning Occurred\n&lt;simpleWarning in log(x): NaNs produced&gt;\n[1] NA\n<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Omdat we een negatieve waarde hebben opgegeven voor het eerste argument, kan R de log van een negatieve waarde niet berekenen, dus ontvangen we het bericht &#8220; <strong>Er heeft zich een waarschuwing voorgedaan<\/strong> &#8222;. We zien de exacte waarschuwing geproduceerd door R en de functie retourneert <strong>NA<\/strong> als heeft een negatieve waarde. resultaat.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Aanvullende bronnen<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">In de volgende tutorials wordt uitgelegd hoe u andere veelvoorkomende bewerkingen in R kunt uitvoeren:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/genest-voor-lus-in-r\/\" target=\"_blank\" rel=\"noopener\">Hoe maak je een geneste For-lus in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/r-toevoegen-aan-vector-in-lus\/\" target=\"_blank\" rel=\"noopener\">Hoe waarden aan een vector toe te voegen met behulp van een lus in R<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/r-functie-retour\/\" target=\"_blank\" rel=\"noopener\">Hoe de waarde van een functie in R te retourneren<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>U kunt een tryCatch() -functie in R gebruiken om de waarde van een expressie te retourneren of een aangepast bericht te genereren als er een waarschuwing of fout wordt aangetroffen. Deze functie gebruikt de volgende basissyntaxis: my_function &lt;- function (x, y){ tryCatch ( #try to do this { #some expression }, #if an error occurs, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-3018","post","type-post","status-publish","format-standard","hentry","category-gids"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hoe u uw eerste tryCatch()-functie schrijft in R - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt uitgelegd hoe u een tryCatch()-functie in R schrijft, inclusief een compleet voorbeeld.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe u uw eerste tryCatch()-functie schrijft in R - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt uitgelegd hoe u een tryCatch()-functie in R schrijft, inclusief een compleet voorbeeld.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-19T15:05:42+00:00\" \/>\n<meta name=\"author\" content=\"Dr.benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr.benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/\",\"url\":\"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/\",\"name\":\"Hoe u uw eerste tryCatch()-functie schrijft in R - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-19T15:05:42+00:00\",\"dateModified\":\"2023-07-19T15:05:42+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt uitgelegd hoe u een tryCatch()-functie in R schrijft, inclusief een compleet voorbeeld.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe u uw eerste trycatch()-functie schrijft in r\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/nl\/#website\",\"url\":\"https:\/\/statorials.org\/nl\/\",\"name\":\"Statorials\",\"description\":\"Uw gids voor statistische competentie\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\",\"name\":\"Dr.benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr.benjamin anderson\"},\"description\":\"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder\",\"sameAs\":[\"http:\/\/statorials.org\/nl\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hoe u uw eerste tryCatch()-functie schrijft in R - Statorials","description":"In deze tutorial wordt uitgelegd hoe u een tryCatch()-functie in R schrijft, inclusief een compleet voorbeeld.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe u uw eerste tryCatch()-functie schrijft in R - Statorials","og_description":"In deze tutorial wordt uitgelegd hoe u een tryCatch()-functie in R schrijft, inclusief een compleet voorbeeld.","og_url":"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/","og_site_name":"Statorials","article_published_time":"2023-07-19T15:05:42+00:00","author":"Dr.benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr.benjamin anderson","Gesch\u00e4tzte Lesezeit":"3\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/","url":"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/","name":"Hoe u uw eerste tryCatch()-functie schrijft in R - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-19T15:05:42+00:00","dateModified":"2023-07-19T15:05:42+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt uitgelegd hoe u een tryCatch()-functie in R schrijft, inclusief een compleet voorbeeld.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/probeer-het-nog-eens\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe u uw eerste trycatch()-functie schrijft in r"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/nl\/#website","url":"https:\/\/statorials.org\/nl\/","name":"Statorials","description":"Uw gids voor statistische competentie","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de"},{"@type":"Person","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219","name":"Dr.benjamin anderson","image":{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/","url":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr.benjamin anderson"},"description":"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder","sameAs":["http:\/\/statorials.org\/nl"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/comments?post=3018"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3018\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=3018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=3018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=3018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}