total_price()
The function is in the folder R
You can install the development version of terribleCode from GitHub with:
# install.packages("devtools")
devtools::install_github("svalvaro/terribleCode")
library(terribleCode)
## basic example code
df <- data.frame(
product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
price_EUR = c(217,517,279,173,110),
price_USD = c(249,591,319,198,125)
)
# 20 % discount for EUR
total_price(df, currency = 'EUR')
#> [1] 1036.8
(217+517+279+173+110)*0.8
#> [1] 1036.8
df <- data.frame(
product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
price_EUR = c(217,NA,' ',173,110),
price_USD = c(249,591,319,198,125)
)
total_price(df, currency = 'EUR') # 20% discount
#> [1] 400
(217+173+110)*0.8
#> [1] 400
df <- data.frame(
product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
price_EUR = c(217,517,279,173,110),
price_USD = c(249,'591','unknown','NA',125)
)
total_price(df, currency = 'USD') # 25% discount
#> [1] 723.75
(249+591+125)*0.75
#> [1] 723.75
df <- data.frame(
product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
price_EUR = c(217,517,279,173,110),
price_USD = c(249,591,319,198,125),
price_CZK = c(6122, 14530, 7843, 4868, 3073)
)
total_price(df, currency = 'CZK')
#> [1] 36436
(6122+14530+7843+4688+3073)
#> [1] 36256
df <- data.frame(
product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
price_EUR = c(217,517,279,173,110),
price_USD = c(249,591,319,198,125)
)
df2 <- data.frame(
product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
price_EUR = c(217,517,279,173,110),
USD_price = c(249,591,319,198,125)
)
total_price(df, currency = 'USD') == total_price(df2, currency = 'USD')
#> [1] TRUE
df <- data.frame(
product = c('smartwatch', 'laptop', 'monitor', 'headphones', 'printer'),
price_EUR = c(217,517,279,173,110),
price_USD = c(249,591,319,198,125)
)
total_price(df, currency = 'CZK')
#> Error in total_price(df, currency = "CZK"): The prices for the provided currency were not found.
vector <- c(217,517,279,173,110)
total_price(vector, currency = 'CZK')
#> Error in total_price(vector, currency = "CZK"): Data provided is not a data.frame
Lastly, I have removed the dependency of the magritrr
package by
removing the usage of the pipe: %>%
. It was unnecessary and some users
might not be familiar with it.
A total of six different tests have been created for this function. They are in the folder tests.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.