Skip to content

Instantly share code, notes, and snippets.

@aureliennicosia
Last active August 25, 2016 20:28
Show Gist options
  • Save aureliennicosia/9d1054babdd5bdda6cbce5188a22b498 to your computer and use it in GitHub Desktop.
Save aureliennicosia/9d1054babdd5bdda6cbce5188a22b498 to your computer and use it in GitHub Desktop.
chiffrier
list.of.packages <- c("ggplot2", "devtools", "lubridate", "nlme",
"plotly", "shinydashboard", "shiny", "git2r", 'DT')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,
"Package"])]
if (length(new.packages)) install.packages(new.packages)
library(devtools)
install_github("trestletech/shinyTable", force=TRUE)
library(shinyTable)
library(shiny)
library(shinydashboard)
# chiffrier Guy Beaulieu
if (!require("DT")) install.packages('DT')
library(DT)
# 1.data frame sur le produit
print.money <- function(x, ...) {
paste0("$", formatC(as.numeric(x), format="f", digits=2, big.mark=","))
}
print.pourcentage <- function(x, ...) {
paste0( formatC(as.numeric(x*100), format="f", digits=2, big.mark=","), '%')
}
update_df.produit <- function( prix.coutant, marge.brute, list.pourcentage){
df.produit <- data.frame( matrix(NA, nrow = 8, ncol = 2),
row.names = c('Prix coutant du produit', 'Prix de gros',
'Marge brute', 'Profit Compagnie et admin.',
'Royauté', 'Bonification',
'Comission directe', 'Prix de vente'))
colnames(df.produit) = c('Pourcentage', 'Prix')
df.produit$Pourcentage <- list.pourcentage
df.produit['Prix coutant du produit',]$Prix <- prix.coutant
df.produit['Marge brute',]$Prix <- marge.brute
df.produit['Prix de gros',]$Prix <- prix.coutant + marge.brute
df.produit['Profit Compagnie et admin.',]$Prix <- marge.brute*df.produit['Profit Compagnie et admin.',]$Pourcentage
df.produit['Royauté',]$Prix <- marge.brute*df.produit['Royauté',]$Pourcentage
df.produit['Bonification',]$Prix <- marge.brute*df.produit['Bonification',]$Pourcentage
df.produit['Prix de vente',]$Prix <- df.produit['Prix de gros',]$Prix/(1-df.produit['Comission directe',]$Pourcentage)
df.produit['Comission directe',]$Prix <- df.produit['Marge brute',]$Prix*df.produit['Comission directe',]$Pourcentage
return(df.produit)
}
#df.produit <- update_df.produit(df.produit, prix.coutant = 56, marge.brute = 80, list.pourcentage)
pourc.royaute = function(p, nbr.mois, p_royaute) return(sum(p^c(1:nbr.mois))-p_royaute)
update_df.royaute <- function( nbr.mois,prix.coutant, marge.brute, nbr.personne, bon.max, method = c('geo', 'normal'), list.pourcentage){
#if(bon.max > nbr.mois ) stop('le nombre de mois maximum de royauté dois etre inferieur au nombre de mois considéré')
df.produit <- update_df.produit( prix.coutant = prix.coutant, marge.brute = marge.brute, list.pourcentage = list.pourcentage)
df.royaute <- data.frame( matrix(NA, nrow = 4 + nbr.mois, ncol = nbr.mois + 1))
colnames(df.royaute) <- c('Pourcentage royauté', paste("Mois ", 1:nbr.mois, sep=""))
rownames(df.royaute) <- c('Client père', paste("Génération ", 1:nbr.mois, sep=""),
'Royauté client', 'Total vente', 'Total Marge brute')
# pourcentage de royraute
df.royaute['Client père',]$`Pourcentage royauté` <- df.produit['Royauté',]$Pourcentage
df.royaute[ paste("Génération ", 1:nbr.mois, sep=""),]$`Pourcentage royauté` <- rep(0,nbr.mois)
if (method == 'geo'){
solve_pourc.royaute <- uniroot(f = pourc.royaute, interval = c(0,1), nbr.mois = nbr.mois, p_royaute = df.produit['Royauté',]$Pourcentage)
df.royaute[ paste("Génération ", 1:min(bon.max, nbr.mois), sep=""),]$`Pourcentage royauté` <- solve_pourc.royaute$root^c(1:min(bon.max, nbr.mois));
}
if (method == 'normal') df.royaute[ paste("Génération ", 1:min(bon.max, nbr.mois), sep=""),]$`Pourcentage royauté` <- df.produit['Royauté',]$Pourcentage/min(bon.max, nbr.mois);
# nombre de personnes par mois
df.royaute[ paste("Génération ", 1:nbr.mois, sep=""),paste("Mois ", 1:nbr.mois, sep="")] <- matrix(0,nbr.mois, nbr.mois)
df.royaute['Client père',paste("Mois ", 1:nbr.mois, sep="")] <- 1
df.royaute['Génération 1',"Mois 1"] <- nbr.personne
for (i in (2:nbr.mois)){
df.royaute['Génération 1',paste("Mois ", as.character(i), sep="")] <- df.royaute['Génération 1',paste("Mois ", as.character(i-1), sep="")] +
nbr.personne
}
for (j in (2:nbr.mois)){
if (nbr.mois >= 2 ){
for (i in (j:nbr.mois)){
df.royaute[paste("Génération ", as.character(j), sep=""),
paste("Mois ", as.character(i), sep="")] <-df.royaute[paste("Génération ", as.character(j), sep=""),
paste("Mois ", as.character(i-1), sep="")] +
nbr.personne*df.royaute[paste("Génération ", as.character(j-1), sep=""),
paste("Mois ", as.character(i-1), sep="")]
}
}
}
# calcul royauté +total vente et on regarde pour la bonification
for (i in (1:nbr.mois)){
df.royaute['Royauté client',paste("Mois ", as.character(i), sep="")] <-
df.produit['Marge brute',]$Prix*
df.royaute[ paste("Génération ", 1:nbr.mois, sep=""),paste("Mois ", as.character(i), sep="")] %*%
df.royaute[ paste("Génération ", 1:nbr.mois, sep=""),'Pourcentage royauté']
df.royaute['Total vente',paste("Mois ", as.character(i), sep="")] <- sum( df.royaute[paste("Génération ", 1:nbr.mois, sep=""),paste("Mois ", as.character(i), sep="")])*df.produit['Prix de vente',]$Prix
df.royaute['Total Marge brute',paste("Mois ", as.character(i), sep="")] <- sum( df.royaute[paste("Génération ", 1:nbr.mois, sep=""),paste("Mois ", as.character(i), sep="")])*df.produit['Marge brute',]$Prix
}
return(df.royaute)
}
#update_df.royaute(df.produit, nbr.mois, nbr.personne, bon.max, method = 'geo')
update_df.royauteBoni <- function(prix.coutant, marge.brute, list.pourcentage, nbr.mois, nbr.personne, bon.max , method = 'geo', bonificiation){
if (length(bonificiation[,1]) != nbr.mois) stop('l escalier de bonification doit etre composé de', nbr.mois, ' nombres de mois')
df.royaute_sansBon <- update_df.royaute( prix.coutant = prix.coutant, marge.brute = marge.brute, nbr.mois = nbr.mois,
nbr.personne = nbr.personne, bon.max = bon.max, method = method, list.pourcentage = list.pourcentage)
for ( i in (1:nbr.mois)){
if (df.royaute_sansBon['Total vente', paste("Mois ", as.character(i), sep="")] > bonificiation[i,1]) {
bon.max_new = bon.max + 1
list.pourcentage_new = list.pourcentage
list.pourcentage_new[5] = list.pourcentage[5] + bonificiation[i,2]
df.produit_new <-update_df.produit( prix.coutant = prix.coutant, marge.brute = marge.brute, list.pourcentage = list.pourcentage_new )
df.royaute_sansBon <- update_df.royaute(prix.coutant = prix.coutant, marge.brute = marge.brute, nbr.mois = nbr.mois,
nbr.personne = nbr.personne, bon.max = bon.max_new, method = method, list.pourcentage = list.pourcentage_new)
}
}
df.royaute_sansBon$`Pourcentage royauté`[c(1:(1+nbr.mois))] = print.pourcentage(df.royaute_sansBon$`Pourcentage royauté`)[c(1:(1+nbr.mois))]
for( i in (0:2)){
df.royaute_sansBon[dim(df.royaute_sansBon)[1]-i,-1] <- print.money(df.royaute_sansBon[dim(df.royaute_sansBon)[1]-i,-1])
}
return(df.royaute_sansBon)
}
print.money <- function(x, ...) {
paste0("$", formatC(as.numeric(x), format="f", digits=2, big.mark=","))
}
print.pourcentage <- function(x, ...) {
paste0( formatC(as.numeric(x*100), format="f", digits=3, big.mark=","), '%')
}
# Setup Shiny app back-end components
# -------------------------------------
server <- function(input, output) {
pourcentage <-reactive({
list.pourcentage<- c(1,1,1,input$p.profit/100,input$p.royaute/100,input$p.bonification/100,input$p.comission/100,1)
})
bonification <- reactive({
bonificiation = cbind(input$valeur.1*seq(1:input$nbr.mois), seq(from = 0, to = input$p.bonification/100, length.out = input$nbr.mois))
})
output$df.produit <- renderTable({
#list.pourcentage<- c(1,1,1,0.4,0.5,0.1,0.2,1)
df.produit = update_df.produit( prix.coutant = input$prix.coutant,
marge.brute = input$marge.brute, list.pourcentage = pourcentage() )
df.produit$Prix <- print.money(df.produit$Prix)
df.produit$Pourcentage <- print.pourcentage(df.produit$Pourcentage)
return(df.produit)
})
output$df.royaute <- DT::renderDataTable({
#list.pourcentage<- c(1,1,1,0.4,0.5,0.1,0.2,1)
df.produit = update_df.produit( prix.coutant = input$prix.coutant,
marge.brute = input$marge.brute, list.pourcentage = pourcentage() )
#df.produit = update_df.produit( prix.coutant = input$prix.coutant, marge.brute = input$marge.brute, list.pourcentage = list.pourcentage )
#up = update_df.royaute(df.produit, input$nbr.mois, input$nbr.personne, input$bon.max, input$method)
up = update_df.royauteBoni( input$prix.coutant , input$marge.brute , list.pourcentage= pourcentage(),
input$nbr.mois, input$nbr.personne, input$bon.max ,method = input$method ,bonificiation= bonification())
# df.royaute_sansBon[c(1:(1+input$nbr.mois)),1] = print.pourcentage(df.royaute_sansBon[c(1:(1+input$nbr.mois)),1])
# for( i in ((input$nbr.mois+1):dim(df.royaute_sansBon)[1])){
# df.royaute_sansBon[i,-1] <- print.money(df.royaute_sansBon[i,-1])
#
# }
return(datatable(up, rownames = TRUE ))
})
output$prix.coutant <- renderValueBox({
valueBox(
paste0(input$prix.coutant, "$"), "Prix coutant", icon = icon("list"),
color = "purple"
)
})
}
# This is the user-interface definition of a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
# list.pourcentage<- c(1,1,1,0.4,0.5,0.1,0.2,1)
# prix.coutant = 56
# marge.brute = 80
# nbr.mois <- 7
# nbr.personne <-2
# bon.max <- 5
# list.pourcentage<- c(1,1,1,0.4,0.5,0.1,0.2,1)
# bon.max = 7, method = 'geo'
#bonificiation = cbind(350*seq(1:nbr.mois), seq(from = 0, to = 0.1, length.out = nbr.mois))
sidebar <- dashboardSidebar(
numericInput("prix.coutant", ("Prix coutant"), value= 20),
numericInput("marge.brute", ("Marge brute"), value = 80),
sliderInput("nbr.mois",("Nombre de mois"), value = 5, min = 1, max = 10 ),
sliderInput("bon.max",("Nombre de generation de royaute maximum sans bonification"), value = 5, min = 1, max = 10 ),
sliderInput("nbr.personne",("Nombre de personnes ajoutes par mois"), value = 2, min = 1, max = 5 ),
selectInput("method",
label = ("Methode de la repartition des royautes"),
choices = list("geometrique" = "geo",
"standard" = "normal"),
selected = "geo")
)
# Simple header -----------------------------------------------------------
dashboardPage(
dashboardHeader(title = "Chiffrier by Aurelien Nicosia", titleWidth = 450),
dashboardSidebar(sidebar ), #,titleWidth = 250
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(
title = "Bonification",width= 6,collapsible= TRUE,status = "warning", solidHeader = TRUE, br(),
tabPanel("boni",
numericInput("valeur.1", "Volume d'affaire genere pour avoir 1% de bonification", value = 10000)
)
),
box(
title = "Pourcentage",width= 6,collapsible= TRUE,status = "warning", solidHeader = TRUE, br(),
tabPanel("Produit",
numericInput("p.profit", ("Pourcentage profit compagnie et administration"), value = 40),
numericInput("p.royaute", ("pourcentage royaute"), value = 50),
numericInput("p.bonification", ("Pourcentage bonification"), value = 10),
numericInput("p.comission", ("Pourcentage comission directe sur prix de vente"), value = 20)
)
)),
fluidRow( box(
title = "Information sur le produit",width= 4,collapsible= TRUE,status = "primary", solidHeader = TRUE, br(),
tableOutput("df.produit")),
box(title= "Royaute" ,width= 8,statut = "info",collapsible= TRUE, solidHeader = TRUE, br(),
DT::dataTableOutput("df.royaute"))
))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment