shiny分級更新dataframe數值實現

來源:互聯網
上載者:User
使用shiny實現對年、季度、月取值的連鎖更新 實現目標

點擊按鈕年預算更新對所有的提升百分比更新
點擊按鈕季度預算更新對對應季度以及季度對應的月份的提升百分比更新
點擊按鈕月份預算更新對對應月份的提升百分比更新

代碼

global.R

# 小數轉化為百分數輸出numtop <- function(num, p=2){  num1 <- num * 100  formt <- paste0("%.", p, "f")  percent <- sprintf(formt, num1)  percent <- paste0(percent, "%")  percent}# 年的預算提升百分比初始值bg_year <- data.frame(year=2016,                         bg_year_pct=3)# 季度的預算提升百分比初始值bg_quarter <- data.frame(quarter=1:4,                         bg_quarter_pct=3)# 月度的預算提升百分比初始值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 = "門店銷售預算達成分析"),    dashboardSidebar(      sidebarMenu(        menuItem("預算百分比設定", tabName = "tab_budget", icon = icon("gear"))      )    ),    dashboardBody(      tabItems(        tabItem(tabName = "tab_budget",                fluidRow(                  box(collapsible = TRUE,                    solidHeader = TRUE, background = "orange", width = 4,height = 140,                    textInput("bg_year", "年預算", 3),                    actionButton('btn_year',"年預算更新")                  ),                  box("年預算", collapsible = TRUE,                    solidHeader = TRUE, background = "navy", width = 5, height = 140,                    tableOutput("tab_bg_year")                  ),                  box(collapsible = TRUE,                      solidHeader = TRUE, background = "orange", width = 4,height = 240,                      selectInput("slt_quarter", label = "季度",                                   choices = unique(bg_quarter$quarter),                                   selected = unique(bg_quarter$quarter)[1]),                      textInput("num_quarter", label = "季度預算提升百分比", value = 3),                      actionButton('btn_quarter',"季度預算更新")                  ),                  box("季度預算", collapsible = TRUE,                      solidHeader = TRUE, background = "navy", width = 5, height = 240,                      tableOutput("tab_bg_quarter")                  ),                  box(collapsible = TRUE,                      solidHeader = TRUE, background = "orange", width = 4,height = 300,                      selectInput("slt_month", label = "月度",                                   choices = unique(bg_month$month),                                   selected = unique(bg_month$month)[1]),                      textInput("num_month", label = "月度預算提升百分比", value = 3),                      actionButton('btn_month',"月度預算更新")                  ),                  box("月度預算", 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) {  ##########################  ### 預算提升百分比設定 ###  ##################  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  })  bg_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(input$btn_quarter){      isolate({        num <- input$num_quarter        sel <- input$slt_quarter        bg_quarter_update$dat$bg_quarter_pct[bg_quarter_update$dat$quarter==sel] <- num        bg_month_update$dat$bg_month_pct[bg_month_update$dat$quarter==sel] <- num       })    }    if(input$btn_month){      isolate({        num <- input$num_month        sel <- 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(tmp$year, "年", sep="")    tmp$bg_year_pct <- numtop(as.numeric(tmp$bg_year_pct) / 100)    names(tmp) <- c("年", "提升百分比")    tmp  })  output$tab_bg_quarter <- renderTable({    tmp <- bg_quarter_update$dat    tmp$quarter <- paste(tmp$quarter, "季度", sep="")    tmp$bg_quarter_pct <- numtop(as.numeric(tmp$bg_quarter_pct) / 100)    names(tmp) <- c("季度", "提升百分比")    tmp  })  output$tab_bg_month <- renderTable({    tmp <- bg_month_update$dat    tmp$month <- paste(tmp$month, "月份", sep="")    tmp$quarter <- paste(tmp$quarter, "季度", sep="")    tmp$bg_month_pct <- numtop(as.numeric(tmp$bg_month_pct) / 100)    names(tmp) <- c("月份", "季度", "提升百分比")    tmp  })})

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.