{"id":3869,"date":"2023-07-15T01:54:12","date_gmt":"2023-07-15T01:54:12","guid":{"rendered":"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/"},"modified":"2023-07-15T01:54:12","modified_gmt":"2023-07-15T01:54:12","slug":"r-verifique-se-o-diretorio-existe","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/","title":{"rendered":"Como verificar se existe um diret\u00f3rio em r (com exemplo)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Voc\u00ea pode usar os seguintes m\u00e9todos para verificar se existe um diret\u00f3rio em R:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>M\u00e9todo 1: verifique se o diret\u00f3rio existe<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>dir. <span style=\"color: #3366ff;\">exists<\/span> (file. <span style=\"color: #3366ff;\">path<\/span> (main_dir, sub_dir))\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Esta fun\u00e7\u00e3o retornar\u00e1 <strong>TRUE<\/strong> se o diret\u00f3rio existir e <strong>FALSE<\/strong> caso contr\u00e1rio.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>M\u00e9todo 2: Crie um diret\u00f3rio se ele n\u00e3o existir<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define directory\n<\/span>my_directory &lt;- file. <span style=\"color: #3366ff;\">path<\/span> (main_dir, sub_dir)\n\n<span style=\"color: #008080;\">#create directory if it doesn't exist\n<\/span>if ( <span style=\"color: #800080;\">!<\/span> dir. <span style=\"color: #3366ff;\">exists<\/span> (my_directory)) {dir. <span style=\"color: #3366ff;\">create<\/span> (my_directory)}<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Observe que <strong>main_dir<\/strong> e <strong>sub_dir<\/strong> s\u00e3o strings que especificam os caminhos do diret\u00f3rio principal e do subdiret\u00f3rio.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Os exemplos a seguir mostram como usar cada m\u00e9todo na pr\u00e1tica.<\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Exemplo 1: Verifique se o diret\u00f3rio existe<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Suponha que queiramos verificar se existem os seguintes diret\u00f3rios:<\/span><\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">\u201cC:\/Usu\u00e1rios\/bob\/\u201d<\/span><\/li>\n<li> <span style=\"color: #000000;\">\u201cC:\/Usu\u00e1rios\/bob\/Documentos\u201d<\/span><\/li>\n<li> <span style=\"color: #000000;\">\u201cC:\/Usu\u00e1rios\/bob\/Data_Science_Documents\u201d<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Podemos usar a seguinte sintaxe para fazer isso:<\/span><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define main directory\n<\/span>main_dir &lt;- \"C:\/Users\/bob\/\"\n\n<span style=\"color: #008080;\">#define various sub directories\n<\/span>sub_dir1 &lt;- \"Documents\"\nsub_dir2 &lt;- \"Data_Science_Documents\"\n\n<span style=\"color: #008080;\">#check if main directory exists\n<\/span>dir.exists( <span style=\"color: #3366ff;\">file.path<\/span> (main_dir))\n\n[1] TRUE\n\n<span style=\"color: #008080;\">#check if main directory and sub directory 1 exists\n<\/span>dir.exists( <span style=\"color: #3366ff;\">file.path<\/span> (main_dir, sub_dir1))\n\n[1] TRUE\n\n<span style=\"color: #008080;\">#check if main directory and sub directory2 exists\n<\/span>dir.exists( <span style=\"color: #3366ff;\">file.path<\/span> (main_dir, sub_dir2))\n\n[1] FALSE\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Pelo resultado podemos ver:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">\u201cC:\/Usu\u00e1rios\/bob\/\u201d \u2013 <strong>Existe<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">\u201cC:\/Usu\u00e1rios\/bob\/Documentos\u201d \u2013 <strong>Existe<\/strong><\/span><\/li>\n<li> <span style=\"color: #000000;\">\u201cC:\/Users\/bob\/Data_Science_Documents\u201d \u2013 <strong>N\u00e3o existe<\/strong><\/span><\/li>\n<\/ul>\n<h2> <span style=\"color: #000000;\"><strong>M\u00e9todo 2: Crie um diret\u00f3rio se ele n\u00e3o existir<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">Digamos que queremos criar o seguinte diret\u00f3rio, se ele ainda n\u00e3o existir:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">\u201cC:\/Usu\u00e1rios\/bob\/Data_Science_Documents\u201d<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Podemos usar a seguinte sintaxe para fazer isso:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define main directory\n<span style=\"color: #000000;\">main_dir &lt;- \"C:\/Users\/bob\/\"<\/span>\n\n#define sub directory\n<span style=\"color: #000000;\">sub_dir &lt;- \"Data_Science_Documents\"<\/span>\n\n#define directory\n<\/span>my_directory &lt;- file. <span style=\"color: #3366ff;\">path<\/span> (main_dir, sub_dir)\n\n<span style=\"color: #008080;\">#create directory if it doesn't exist\n<\/span>if ( <span style=\"color: #800080;\">!<\/span> dir. <span style=\"color: #3366ff;\">exists<\/span> (my_directory)) {dir. <span style=\"color: #3366ff;\">create<\/span> (my_directory)}<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Se navegarmos at\u00e9 esta pasta em nosso computador, podemos ver que este diret\u00f3rio n\u00e3o existe, mas j\u00e1 foi criado:<\/span> <\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-31227 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/lecteur1.jpg\" alt=\"\" width=\"661\" height=\"123\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">Observe que se este diret\u00f3rio j\u00e1 existisse, um novo n\u00e3o seria criado.<\/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 realizar outras tarefas comuns em R:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/pt\/carregar-varios-pacotes-em-r\/\" target=\"_blank\" rel=\"noopener\">Como carregar v\u00e1rios pacotes em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/verifique-se-o-pacote-esta-instalado\/\" target=\"_blank\" rel=\"noopener\">Como verificar se um pacote est\u00e1 instalado em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/ambiente-claro-em-r\/\" target=\"_blank\" rel=\"noopener\">Como limpar o ambiente em R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Voc\u00ea pode usar os seguintes m\u00e9todos para verificar se existe um diret\u00f3rio em R: M\u00e9todo 1: verifique se o diret\u00f3rio existe dir. exists (file. path (main_dir, sub_dir)) Esta fun\u00e7\u00e3o retornar\u00e1 TRUE se o diret\u00f3rio existir e FALSE caso contr\u00e1rio. M\u00e9todo 2: Crie um diret\u00f3rio se ele n\u00e3o existir #define directory my_directory &lt;- file. path (main_dir, [&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-3869","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 diret\u00f3rio em R (com exemplo) \u2013 Estatologia<\/title>\n<meta name=\"description\" content=\"Este tutorial explica como verificar se existe um diret\u00f3rio em um ambiente R, com v\u00e1rios exemplos.\" \/>\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-diretorio-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 diret\u00f3rio em R (com exemplo) \u2013 Estatologia\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica como verificar se existe um diret\u00f3rio em um ambiente R, com v\u00e1rios exemplos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-15T01:54:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/lecteur1.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-diretorio-existe\/\",\"url\":\"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/\",\"name\":\"Como verificar se existe um diret\u00f3rio em R (com exemplo) \u2013 Estatologia\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-15T01:54:12+00:00\",\"dateModified\":\"2023-07-15T01:54:12+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica como verificar se existe um diret\u00f3rio em um ambiente R, com v\u00e1rios exemplos.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Como verificar se existe um diret\u00f3rio em r (com exemplo)\"}]},{\"@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 diret\u00f3rio em R (com exemplo) \u2013 Estatologia","description":"Este tutorial explica como verificar se existe um diret\u00f3rio em um ambiente R, com v\u00e1rios exemplos.","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-diretorio-existe\/","og_locale":"pt_PT","og_type":"article","og_title":"Como verificar se existe um diret\u00f3rio em R (com exemplo) \u2013 Estatologia","og_description":"Este tutorial explica como verificar se existe um diret\u00f3rio em um ambiente R, com v\u00e1rios exemplos.","og_url":"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/","og_site_name":"Statorials","article_published_time":"2023-07-15T01:54:12+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/lecteur1.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-diretorio-existe\/","url":"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/","name":"Como verificar se existe um diret\u00f3rio em R (com exemplo) \u2013 Estatologia","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-15T01:54:12+00:00","dateModified":"2023-07-15T01:54:12+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica como verificar se existe um diret\u00f3rio em um ambiente R, com v\u00e1rios exemplos.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/r-verifique-se-o-diretorio-existe\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"Como verificar se existe um diret\u00f3rio em r (com exemplo)"}]},{"@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\/3869","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=3869"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/3869\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=3869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=3869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=3869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}