shiny分级更新dataframe数值实现

1 篇文章 0 订阅

使用shiny实现对年、季度、月取值的连锁更新

实现目标

点击按钮年预算更新对所有的提升百分比更新
点击按钮季度预算更新对对应季度以及季度对应的月份的提升百分比更新
点击按钮月份预算更新对对应月份的提升百分比更新

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
  })
})
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值