Shiny Hierarchical update dataframe numerical implementation

Source: Internet
Author: User
Tags numeric
use shiny to achieve annual, quarterly and monthly value chain updates achieve Goals

Click button Annual budget update for all promotion percent updates
Click the button Quarterly budget update to update the percentage of the corresponding quarter and the month that corresponds to the quarter
Click the button month budget update to update the percentage of the corresponding month

Code

Global. R

# convert decimal to Percent output
numtop <-function (num, p=2) {
  num1 <-num * formt <-paste0
  ("%.", p, "f")
  Percen T <-sprintf (FORMT, NUM1)
  percent <-paste0 (percent, "%")
  percent
}

# Year's budget boost percent initial value
Bg_ Year <-Data.frame (year=2016,
                         bg_year_pct=3)

# Quarterly budget increase percent initial value Bg_quarter <-data.frame
(quarter= 1:4,
                         bg_quarter_pct=3)

# Monthly budget increase percent initial value
bg_month <-data.frame (month=1:12,
                       quarter=rep (1:4, each=3),
                       bg_month_pct=3)

Ui. R

Library (shiny) library (Shinydashboard) Shinyui (Dashboardpage (dashboardheader (title = "Sales budget of Stores"), Dashboar

    Dsidebar (Sidebarmenu (MenuItem ("Budget percent setting", TabName = "Tab_budget", Icon = icon ("gear")), Dashboardbody (TabItems (tabItem (tabname = "Tab_budget", Fluidrow (Box (col
                    Lapsible = True, Solidheader = true, background = "Orange", width = 4,height = 140,

                  TextInput ("Bg_year", "Annual Budget", 3), Actionbutton (' Btn_year ', "Annual budget update"),
                    Box ("Annual budget", collapsible = true, Solidheader = true, background = "Navy", Width = 5, height = 140,
                      Tableoutput ("Tab_bg_year")), box (collapsible = TRUE, Solidheader = TRUE, background = "Orange", width = 4,height = +, Selectinput ("Slt_quart 
    Er ", label =" Quarter ",                              choices = Unique (bg_quarter$quarter), selected = unique (b
                      G_quarter$quarter) [1]), TextInput ("Num_quarter", label = "Percentage of quarterly budget Increase", value = 3),
                      Actionbutton (' Btn_quarter ', ' Quarterly budget update '), Box ("Quarterly Budget", collapsible = TRUE, Solidheader = TRUE, background = "Navy", Width = 5, height = all, Tableoutput ("Tab_bg_q Uarter ")), box (collapsible = true, Solidheader = True, backgr 
                                  Ound = "Orange", width = 4,height = $, Selectinput ("slt_month", label = "Monthly", 
                      choices = Unique (bg_month$month), selected = Unique (Bg_month$month) [1]), TextInput ("Num_month", label = "Monthly budget increase percentage", value = 3), Actionbutton (' Btn_mon
          Th ', "Monthly budget update")        ), Box ("Monthly Budget", collapsible = true, Solidheader = true, background = "Navy
                ", Width = 5, height = 460, tableoutput (outputid=" Tab_bg_month ")) )
        )
      )
    )
  )
)

Server. R

Library (shiny) library (shinydashboard) shinyserver (function (input, Output) {########################## # # # Increase percentage setting 
  # # # ################## bg_year_update <-reactivevalues (dat=null) observe ({Bg_year_update$dat <-bg_year }) bg_quarter_update <-reactivevalues (dat=null) observe ({Bg_quarter_update$dat <-bg_quarter}) b G_month_update <-reactivevalues (dat=null) observe ({Bg_month_update$dat <-bg_month}) observe ({if (
        Input$btn_year) {isolate ({num <-input$bg_year bg_year_update$dat$bg_year_pct <-num bg_quarter_update$dat$bg_quarter_pct <-num bg_month_update$dat$bg_month_pct <-num})} if (I Nput$btn_quarter) {isolate ({num <-input$num_quarter sel <-input$slt_quarter Bg_quar Ter_update$dat$bg_quarter_pct[bg_quarter_update$dat$quarter==sel] <-num bg_month_update$dat$bg_month_pct[bg_m onth_update$dat$quarter==SEL] <-num})} if (Input$btn_month) {isolate ({num <-input$num_month sel &lt 

  ;-Input$slt_month Bg_month_update$dat$bg_month_pct[bg_month_update$dat$month==sel] <-num})}) Output$tab_bg_year <-rendertable ({tmp <-bg_year_update$dat tmp$year, <-(Paste, "year", Tmp$year ""

  ) tmp$bg_year_pct <-numtop (as.numeric (tmp$bg_year_pct)/+) names (TMP) <-C ("year", "percent increase") tmp}) Output$tab_bg_quarter <-rendertable ({tmp <-bg_quarter_update$dat tmp$quarter <-paste (tmp$quarter, "quarter tmp$bg_quarter_pct <-numtop (as.numeric (tmp$bg_quarter_pct)/sep=) names (TMP) <-C ("Quarter", "percent increase" ) tmp}) Output$tab_bg_month <-rendertable ({tmp <-bg_month_update$dat tmp$month <-paste (tmp$ month, "months", sep= "") tmp$quarter <-paste (tmp$quarter, "quarter", sep= "") tmp$bg_month_pct <-numtop (as.numeric (t
mp$bg_month_pct)/100)    Names (TMP) <-C ("month", "quarter", "increase percentage") tmp})
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.