Formattable functions doesn't work in pipeline

> library(tidyverse)
> library(formattable)
> accounting(
+ "39990" %>% as.integer()/100
+ )
[1] 399.90
> 
> "39990" %>% as.integer()/100 %>% accounting()
[1] 399.9

Obviously, accounting function doesn't work in pipeline %>%.
Anyone know how to fix it?

This is not an issue with accouting function, it is more a matter of order of operation in the pipe.
as.integer()/100 is not a correct pipe step. You need to correctly isolate it using a function define before or using dot anonymous function, so that accounting applies correctly on the result of this.
There are also special pipe-friendly operation in magrittr for this kind of issue.

library(magrittr)
library(formattable)
accounting("39990" %>% as.integer()/100)
#> [1] 399.90
# Taking care of pipe operation precedence
"39990" %>% {as.integer(.)/100} %>% accounting()
#> [1] 399.90
# using special pipe operation from magrittr
"39990" %>% as.integer() %>% magrittr::divide_by(100) %>% accounting()
#> [1] 399.90

Created on 2018-06-06 by the reprex package (v0.2.0).

EDIT: the question is also on SO - @EconKid see Policies in FAQ

2 Likes

Hi, @cderv,

Thanks for your answer.

At first, I try,

"39990" %>% as.integer() %>% ./100 %>% accounting()

But I get the error message.

Error in .(.) : could not find function "."

magrittr::divide_by(100) is really what I want.

I find you both answer this question on the community and Stack Overflow.
For Policy in FAQ, I delete the same question on Stack Overflow. Thanks for the reminder.

1 Like