Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the astra-sites domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the jetpack domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wpforms-lite domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wordpress-seo domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114

Notice: A função _load_textdomain_just_in_time foi chamada incorretamente. O carregamento da tradução para o domínio astra foi ativado muito cedo. Isso geralmente é um indicador de que algum código no plugin ou tema está sendo executado muito cedo. As traduções devem ser carregadas na ação init ou mais tarde. Leia como Depurar o WordPress para mais informações. (Esta mensagem foi adicionada na versão 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114

Warning: Cannot modify header information - headers already sent by (output started at /home/statplace/public_html/site/wp-includes/functions.php:6114) in /home/statplace/public_html/site/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home/statplace/public_html/site/wp-includes/functions.php:6114) in /home/statplace/public_html/site/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home/statplace/public_html/site/wp-includes/functions.php:6114) in /home/statplace/public_html/site/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home/statplace/public_html/site/wp-includes/functions.php:6114) in /home/statplace/public_html/site/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home/statplace/public_html/site/wp-includes/functions.php:6114) in /home/statplace/public_html/site/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home/statplace/public_html/site/wp-includes/functions.php:6114) in /home/statplace/public_html/site/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home/statplace/public_html/site/wp-includes/functions.php:6114) in /home/statplace/public_html/site/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /home/statplace/public_html/site/wp-includes/functions.php:6114) in /home/statplace/public_html/site/wp-includes/rest-api/class-wp-rest-server.php on line 1893
{"id":6733,"date":"2020-07-02T13:00:46","date_gmt":"2020-07-02T16:00:46","guid":{"rendered":"https:\/\/site.statplace.com.br\/?p=6733"},"modified":"2024-10-02T14:05:59","modified_gmt":"2024-10-02T14:05:59","slug":"operador-bang-bang-no-r","status":"publish","type":"post","link":"https:\/\/site.statplace.com.br\/blog\/operador-bang-bang-no-r\/","title":{"rendered":"Bang bang operator no R"},"content":{"rendered":"\n

Artigo escrito com a colabora\u00e7\u00e3o de Larisse Annie Saldanha<\/p>\n\n\n\n

No tidy evaluation, todas as fun\u00e7\u00f5es de cita\u00e7\u00e3o s\u00e3o, na verdade, fun\u00e7\u00f5es de quase-cita\u00e7\u00e3o, porque tamb\u00e9m oferecem suporte \u00e0 cita\u00e7\u00e3o. Onde a quotation \u00e9 o ato de capturar uma express\u00e3o n\u00e3o avaliada, unquotation \u00e9 a capacidade de avaliar seletivamente partes de uma express\u00e3o citada.<\/p>\n\n\n\n

Juntos, isso \u00e9 chamado quase-quota\u00e7\u00e3o. A quase-quota\u00e7\u00e3o facilita a cria\u00e7\u00e3o de fun\u00e7\u00f5es que combinam o c\u00f3digo escrito pelo autor da fun\u00e7\u00e3o com o c\u00f3digo escrito pelo usu\u00e1rio da fun\u00e7\u00e3o. Isso ajuda a resolver uma grande variedade de problemas desafiadores.<\/p>\n\n\n\n

Um argumento citado \u00e9 capturado pela fun\u00e7\u00e3o e \u00e9 processado de algum jeito personalizado. Se voc\u00ea n\u00e3o tiver certeza se um argumento \u00e9 citado ou avaliado, tente executar o c\u00f3digo fora da fun\u00e7\u00e3o. Se n\u00e3o funcionar ou fizer algo diferente, esse argumento precisa ser citado. <\/p>\n\n\n\n

Unquoting <\/h2>\n\n\n\n

Unquoting \u00e9 a habilidade de avaliar seletivamente partes de uma express\u00e3o citada de outra forma. Use bang bang `!!` para citar um \u00fanico argumento numa dada fun\u00e7\u00e3o. Por exemplo:<\/p>\n\n\n\n

 ```{r}\n x <- expr(-1)\n expr(f(!!x,y)) ```\n <!-- ![](img\/Diagrama1.PNG) --> <\/pre>\n\n\n\n

A fun\u00e7\u00e3o `quote()` \u00e9 equivalente \u00e0 fun\u00e7\u00e3o `expr()`. Ela simplesmente retorna o argumento. Veja:<\/p>\n\n\n\n

 ```{r echo=TRUE}\n quote(x+y) ``` <\/pre>\n\n\n\n

Assim como chamamos objetos, `!!` tamb\u00e9m funciona para s\u00edmbolos e constantes: <\/p>\n\n\n\n

 ```{r echo=TRUE}\n a <- sym(\"y\")\n b <- 1\n expr(f(!!a, !!b)) ``` <\/pre>\n\n\n\n

Formalmente, `quo()` e `expr()` s\u00e3o fun\u00e7\u00f5es de quase-cita\u00e7\u00e3o, bang bang `!!` \u00e9 um operador de cita\u00e7\u00e3o, e `!!!` \u00e9 um operador de divis\u00e3o de cita\u00e7\u00e3o. Estes termos tem um rico hist\u00f3rico em linguagens Lisp, e em linguagens modernas como Julia e Racket.<\/p>\n\n\n\n

O operador `!!!` toma uma lista de express\u00f5es e insere ela no local do operador. Por exemplo:<\/p>\n\n\n\n

 ```{r echo=TRUE}\n xs <- exprs(1,a,-b)\n expr(f(!!!xs,y)) ``` <\/pre>\n\n\n\n
 ```{r echo=TRUE}\n ys <- set_names(xs, c(\"a\",\"b\",\"c\"))\n expr(f(!!!ys,d=4)) ```  <\/pre>\n\n\n\n

Exemplo 1 <\/h2>\n\n\n\n

Os exemplos que ser\u00e3o mostrados, ser\u00e3o do banco de dados `iris` do R. <\/p>\n\n\n\n

 ```{r echo=TRUE}\n head(iris)\n ``` <\/pre>\n\n\n\n

Suponha que todas as plantas deste banco de dados pertencem \u00e0 novas esp\u00e9cias, **pallida** e queremos atualizar os valores da coluna que correspondem \u00e0s esp\u00e9cies. Usamos ent\u00e3o `mutate()`:<\/p>\n\n\n\n

 ```{r echo=TRUE}\n mutate(iris, Species = \"Pallida\") %>% head() ```  <\/pre>\n\n\n\n

Por defini\u00e7\u00e3o, o `dplyr` cita o nome e avalia o valor. Por isso, podemos facilmente trocar o lado do valor do par por um objeto e, novamente, ele \u00e9 avaliado e obtemos um resultado id\u00eantico. <\/p>\n\n\n\n

 ```{r echo=TRUE}\n targetValue = \"Pallida\"\n mutate(iris, Species = targetValue) %>% head() ```  <\/pre>\n\n\n\n

Quando tentamos especificar o valor por um nome, n\u00e3o funciona. Ele somente cria uma coluna. <\/p>\n\n\n\n

 ```{r echo=TRUE}\n targetColumn = \"Species\"\n mutate(iris, targetColumn = \"Pallida\") %>% head() ``` <\/pre>\n\n\n\n

Uso do Operador bang bang `!!` <\/h2>\n\n\n\n

Como dito anteriormente, o prop\u00f3sito deste operador \u00e9 citar o argumento. Ent\u00e3o, se antes citarmos o nome e avaliar o valor, a cita\u00e7\u00e3o do nome resolveria nosso problema? Infelizmente n\u00e3o completamente. Mas com o uso do operador `:= (defini\u00e7\u00e3o)` resolvemos completamente o problema.<\/p>\n\n\n\n

 ```{r echo=TRUE}\n targetColumn = \"Species\"\n mutate(iris, !!targetColumn := \"Pallida\") %>% head() ```  <\/pre>\n\n\n\n

E mais: <\/p>\n\n\n\n

 ```{r echo=TRUE}\n targetColumn = \"Species\"\n mutate(iris, !!targetColumn := targetValue) %>% head() ``` <\/pre>\n\n\n\n

Uso em Outras Fun\u00e7\u00f5es do `dplyr`<\/h2>\n\n\n\n
 ```{r echo=TRUE}\n a <-\"Species\" \n filter(iris, !!a == \"versicolor\") %>% head() ```  <\/pre>\n\n\n\n

N\u00e3o funciona, j\u00e1 que `a` \u00e9 um s\u00edmbolo e queremos que ele seja avaliado em `Species`. Para funcionar, criamos um s\u00edmbolo. Precisamos transformar o caracter string `”Species”` em s\u00edmbolo. <\/p>\n\n\n\n

 ```{r echo=TRUE}\n a <-\"Species\"\n filter(iris, !!rlang :: sym(a) == \"versicolor\") %>% head() ``` <\/pre>\n\n\n\n

Exemplo 2<\/h2>\n\n\n\n

Banco de Dados `Starwars` do R, onde: <\/p>\n\n\n\n