{"id":3633,"date":"2023-07-16T10:37:23","date_gmt":"2023-07-16T10:37:23","guid":{"rendered":"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/"},"modified":"2023-07-16T10:37:23","modified_gmt":"2023-07-16T10:37:23","slug":"r-verifique-se-o-arquivo-existe","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/","title":{"rendered":"Como verificar se o arquivo existe em r (com exemplos)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Voc\u00ea pode usar a seguinte sintaxe b\u00e1sica para verificar se existe um arquivo em seu diret\u00f3rio de trabalho atual em R:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>file. <span style=\"color: #3366ff;\">exists<\/span> (' <span style=\"color: #ff0000;\">my_data.csv <span style=\"color: #000000;\">')<\/span><\/span><\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Esta fun\u00e7\u00e3o retornar\u00e1 <strong>TRUE<\/strong> se o arquivo existir ou <strong>FALSE<\/strong> se n\u00e3o existir.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Voc\u00ea tamb\u00e9m pode usar uma instru\u00e7\u00e3o if else para ler um arquivo em R somente se ele existir:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>data &lt;- ' <span style=\"color: #ff0000;\">my_data.csv<\/span> '\n\nif(file. <span style=\"color: #3366ff;\">exists<\/span> (data)){<\/strong>\n<strong>df &lt;- read. <span style=\"color: #3366ff;\">csv<\/span> (data)<\/strong>\n<strong>} else {<\/strong>\n<strong><span style=\"color: #008000;\">print<\/span> (' <span style=\"color: #ff0000;\">Does not exist<\/span> ')<\/strong>\n<strong>}<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">O exemplo a seguir mostra como usar essas fun\u00e7\u00f5es na pr\u00e1tica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Exemplo: verifique se o arquivo existe em R<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Digamos que meu <a href=\"https:\/\/statorials.org\/pt\/setwd-getwd-em-r\/\" target=\"_blank\" rel=\"noopener\">diret\u00f3rio de trabalho<\/a> atual em R seja uma pasta chamada <strong>test_data<\/strong> com tr\u00eas arquivos CSV:<\/span><\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-29854 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cheque1.jpg\" alt=\"\" width=\"426\" height=\"356\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">Posso usar <strong>list.files()<\/strong> para listar os nomes de cada arquivo no diret\u00f3rio de trabalho:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#display the names of every file in current working directory\n<\/span>list. <span style=\"color: #3366ff;\">files<\/span> ()\n[1] \"my_data.csv\" \"my_new_data.csv\" \"some_old_data.csv\"\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Posso usar <strong>file.exists()<\/strong> para verificar se um determinado arquivo existe no diret\u00f3rio de trabalho atual:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#check if file 'my_data.csv' exists in current working directory\n<\/span>file. <span style=\"color: #3366ff;\">exists<\/span> (' <span style=\"color: #ff0000;\">my_data.csv<\/span> ')\n\n[1] TRUE<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">A fun\u00e7\u00e3o retorna <strong>TRUE<\/strong> , o que nos informa que o arquivo &#8216;my_data.csv&#8217; realmente existe no diret\u00f3rio de trabalho atual.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Podemos ent\u00e3o usar a seguinte instru\u00e7\u00e3o <strong>if else<\/strong> para importar um arquivo somente se ele existir:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define file name<\/span>\ndata &lt;- ' <span style=\"color: #ff0000;\">my_data.csv<\/span> '\n\n<span style=\"color: #008080;\">#import file only if it exists<\/span>\nif(file. <span style=\"color: #3366ff;\">exists<\/span> (data)){<\/strong>\n<strong>df &lt;- read. <span style=\"color: #3366ff;\">csv<\/span> (data)<\/strong>\n<strong>} else {<\/strong>\n<strong><span style=\"color: #008000;\">print<\/span> (' <span style=\"color: #ff0000;\">Does not exist<\/span> ')<\/strong>\n<strong>}\n\n<span style=\"color: #008080;\">#view contents of CSV file<\/span>\ndf\n\n  team points assists\n1 to 14 4\n2 B 26 7\n3 C 29 8\n4 D 20 3\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Como o arquivo existe, podemos import\u00e1-lo com sucesso.<\/span><\/p>\n<p> <span style=\"color: #000000;\">No entanto, suponha que estejamos tentando importar um arquivo que n\u00e3o existe:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define file name<\/span>\ndata &lt;- ' <span style=\"color: #ff0000;\">this_data.csv<\/span> '\n\n<span style=\"color: #008080;\">#import file only if it exists<\/span>\nif(file. <span style=\"color: #3366ff;\">exists<\/span> (data)){<\/strong>\n<strong>df &lt;- read. <span style=\"color: #3366ff;\">csv<\/span> (data)<\/strong>\n<strong>} else {<\/strong>\n<strong><span style=\"color: #008000;\">print<\/span> (' <span style=\"color: #ff0000;\">Does not exist<\/span> ')<\/strong>\n<strong>}\n\n[1] \u201cDoes not exist\u201d\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Recebemos a mensagem \u201cN\u00e3o existe\u201d, que nos informa que um arquivo chamado <strong>this_data.csv<\/strong> n\u00e3o existe no diret\u00f3rio de trabalho atual.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Os tutoriais a seguir explicam como usar outras fun\u00e7\u00f5es comuns em R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/leia-o-arquivo-zip-em-r\/\" target=\"_blank\" rel=\"noopener\">Como ler arquivos Zip em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/importar-csv-para-r\/\" target=\"_blank\" rel=\"noopener\">Como importar arquivos CSV para R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/importar-excel-para-r\/\" target=\"_blank\" rel=\"noopener\">Como importar arquivos Excel para R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/renomear-arquivo-em-r\/\" target=\"_blank\" rel=\"noopener\">Como renomear arquivos em R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voc\u00ea pode usar a seguinte sintaxe b\u00e1sica para verificar se existe um arquivo em seu diret\u00f3rio de trabalho atual em R: file. exists (&#8216; my_data.csv &#8216;) Esta fun\u00e7\u00e3o retornar\u00e1 TRUE se o arquivo existir ou FALSE se n\u00e3o existir. Voc\u00ea tamb\u00e9m pode usar uma instru\u00e7\u00e3o if else para ler um arquivo em R somente se [&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-3633","post","type-post","status-publish","format-standard","hentry","category-guia"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Como verificar se existe um arquivo em R (com exemplos) \u2013 Estatologia<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como verificar se existe um arquivo em uma pasta em R, com um exemplo.\" \/>\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\/pt\/r-verifique-se-o-arquivo-existe\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Como verificar se existe um arquivo em R (com exemplos) \u2013 Estatologia\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como verificar se existe um arquivo em uma pasta em R, com um exemplo.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-16T10:37:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cheque1.jpg\" \/>\n<meta name=\"author\" content=\"Dr. benjamim anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr. benjamim anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tempo estimado de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/\",\"url\":\"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/\",\"name\":\"Como verificar se existe um arquivo em R (com exemplos) \u2013 Estatologia\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-16T10:37:23+00:00\",\"dateModified\":\"2023-07-16T10:37:23+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como verificar se existe um arquivo em uma pasta em R, com um exemplo.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como verificar se o arquivo existe em r (com exemplos)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/pt\/#website\",\"url\":\"https:\/\/statorials.org\/pt\/\",\"name\":\"Statorials\",\"description\":\"O seu guia para a literacia estat\u00edstica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/pt\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-PT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\",\"name\":\"Dr. benjamim anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. benjamim anderson\"},\"description\":\"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais\",\"sameAs\":[\"https:\/\/statorials.org\/pt\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Como verificar se existe um arquivo em R (com exemplos) \u2013 Estatologia","description":"Este tutorial explica como verificar se existe um arquivo em uma pasta em R, com um exemplo.","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\/pt\/r-verifique-se-o-arquivo-existe\/","og_locale":"pt_PT","og_type":"article","og_title":"Como verificar se existe um arquivo em R (com exemplos) \u2013 Estatologia","og_description":"Este tutorial explica como verificar se existe um arquivo em uma pasta em R, com um exemplo.","og_url":"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/","og_site_name":"Statorials","article_published_time":"2023-07-16T10:37:23+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cheque1.jpg"}],"author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/","url":"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/","name":"Como verificar se existe um arquivo em R (com exemplos) \u2013 Estatologia","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-16T10:37:23+00:00","dateModified":"2023-07-16T10:37:23+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como verificar se existe um arquivo em uma pasta em R, com um exemplo.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/r-verifique-se-o-arquivo-existe\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como verificar se o arquivo existe em r (com exemplos)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/pt\/#website","url":"https:\/\/statorials.org\/pt\/","name":"Statorials","description":"O seu guia para a literacia estat\u00edstica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/pt\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-PT"},{"@type":"Person","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666","name":"Dr. benjamim anderson","image":{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr. benjamim anderson"},"description":"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais","sameAs":["https:\/\/statorials.org\/pt"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/comments?post=3633"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3633\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=3633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=3633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=3633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}