我正在创建闪亮的仪表板,无法使用selectInput过滤数据,因为我正在使用全局变量进行选择

时间:2019-05-28 21:01:09

标签: shiny shinydashboard

我正在使用 “ selectInput(” Region“,” Region“,choices = unique(dat $ Region))”创建selectInput,还使用updateSelectInput根据每个过滤器进行过滤。但是我正面临以下问题。

unique(dat $ Region)中的错误:找不到对象'dat'

代码流: 1,使用过滤器对数据进行子集 2.为此显示rhansontable和图表 3.输入调整后的入站和调整后的出站值 4.按保存按钮后,将rhosonson表写入数据框 5.更改过滤器后,用户可以再次写

            d<-list(1,2,3,4)

            ui<-dashboardPage(
              dashboardHeader(title ="Interim daily flow"),
              dashboardSidebar(

                selectInput("Region","Region", choices = unique(dat$Region)),
                selectInput("DC_Network","DC Network", choices = unique(dat$DC_Network)),
                selectInput("DC_nbr","DC", choices = unique(dat$DC_nbr)),
                selectInput("Dept_nbr","DEPARTMENT", choices = unique(dat$Dept_nbr)),
                selectInput("Chamber","CHAMBER", choices = unique(dat$Chamber)),
                selectInput("Catg_nbr","CATEGORY", choices = unique(dat$Catg_nbr)),
                selectInput("Timeframe","TIMEFRAME(Weeks)",choices = d),
                dateInput("Startweek","START DATE",daysofweekdisabled=c(0,1,2,3,4,5),value=min(dat$Date),weekstart = 0,min = min(dat$Date),max = max(dat$Date))
              ),
              dashboardBody(


              div(
                  tabsetPanel(type = "tabs",
                              tabPanel("Input tab", 
                                       fluidRow(column(12,plotOutput('graph'))
                                       ),
                                       br(),
                                       fluidRow(
                                         column(12,rHandsontableOutput("myTable")),
                                         column(12,actionButton("Save","Save changes"))
                                       )
                              ),tabPanel("Accuracy",
                                        column(12,selectInput("Timeframe_ac","Timeframe",choices = d)),
                                        column(12,dateInput("Startweek_ac","START DATE",daysofweekdisabled=c(0,1,2,3,4,5),value = min(acc_data$Date),min = min(acc_data$Date),max =             max(acc_data$Date))),
                                        column(12,plotOutput('acc_plot'))
                              ) 
                  )

                )
                ))


            server<-function(session,input,output) {

              a<-Sys.time()
              data_n<-read.csv("/data/forecast_ui_data/data.csv",stringsAsFactors = F)
              acc_data_n<-read.csv("/data/forecast_ui_data/acc_data.csv",stringsAsFactors = F)
              c<-Sys.time()
              print("load time")
              print(c-a)

              data_n<- data.frame(lapply(data_n,as.character),stringsAsFactors = F)
              acc_data_n<-data.frame(lapply(acc_data_n,as.character),stringsAsFactors = F)
              data_n[,c(10:17)]<-data.frame(lapply(data_n[,c(10:17)],as.integer),stringsAsFactors = F)
              acc_data_n[,c(10:19)]<-data.frame(lapply(acc_data_n[,c(10:19)],as.integer),stringsAsFactors = F)

              if(grepl("-",data_n$Date[1])){
                data_n$Date<-as.Date(data_n$Date,"%Y-%m-%d")
              }else{data_n$Date<-as.Date(data_n$Date,"%m/%d/%Y")}

              if(grepl("-",acc_data_n$Date[1])){
                acc_data_n$Date<-as.Date(acc_data_n$Date,"%Y-%m-%d")
              }else{acc_data_n$Date<-as.Date(acc_data_n$Date,"%m/%d/%Y")}

              dat<<-data_n
              acc_data<<-acc_data_n

              observeEvent(input$Region,{
                x <-dat %>% filter(Region %in% input$Region) %>% select(DC_Network)
                updateSelectInput(session, "DC_Network","DC Network",choices = unique(x$DC_Network))
              })
              observeEvent(input$DC_Network,{
                x <-dat %>% filter(DC_Network %in% input$DC_Network) %>% select(DC_nbr)
                updateSelectInput(session, "DC_nbr","DC",choices = unique(x$DC_nbr))
              })
              observeEvent(input$DC_nbr,{
                y<- dat %>% filter(DC_Network %in% input$DC_Network) %>% filter(DC_nbr %in% input$DC_nbr) %>% select(Dept_nbr)
                updateSelectInput(session, "Dept_nbr","DEPARTMENT",choices = unique(y$Dept_nbr))    
              })

              observeEvent(input$Dept_nbr,{
                z<- dat %>% filter(DC_Network %in% input$DC_Network) %>% filter(DC_nbr %in% input$DC_nbr)%>% filter(Dept_nbr %in% input$Dept_nbr) %>% select(Chamber)
                updateSelectInput(session, "Chamber","CHAMBER", choices = unique(z$Chamber))
              })

              observeEvent(input$Chamber,{
                z<- dat %>% filter(DC_Network %in% input$DC_Network) %>% filter(DC_nbr %in% input$DC_nbr)%>% filter(Dept_nbr %in% input$Dept_nbr) %>% filter(Chamber %in% input$Chamber) %>% select(Catg_nbr)
                updateSelectInput(session,"Catg_nbr","CATEGORY",choices = unique(z$Catg_nbr))
              })

              fa<-reactive({ 
                a<-as.integer(input$Timeframe)*7
                dat %>%filter(DC_nbr %in% input$DC_nbr) %>% filter(Dept_nbr %in% input$Dept_nbr) %>% filter(Catg_nbr %in% input$Catg_nbr) %>% filter(Chamber %in% input$Chamber) %>% filter(Date >= input$Startweek & Date < input$Startweek+days(a))
              })

              acc<-reactive({ 
                a<-as.integer(input$Timeframe_ac)*7
                acc_data %>%filter(DC_nbr %in% input$DC_nbr) %>% filter(Chamber %in% input$Chamber) %>% filter(Date >= input$Startweek_ac & Date < input$Startweek_ac+days(a))

              })

              output$graph <- renderPlot({
                f<-fa()
                f<- f %>% select(Date,inbound_volume,outbound_volume)
                f1<-melt(f,id.vars="Date")
                titl<-"Unconstrained Inbound and Outbound Volume"
                fill=c("#003896","#f47b20")
                plot_fun(f1,titl,fill)
              })

              output$myTable<-renderRHandsontable({
                fa<-fa()
                final_editable<-fa[,c("Date","Week","DC_Network","DC_nbr","Catg_nbr","Chamber","inbound_volume","outbound_volume","adjust_ib","adjust_ob")]
                rhandsontable(final_editable)%>% hot_col(c("Date","Week","DC_Network","DC_nbr","Catg_nbr","Chamber","inbound_volume","outbound_volume"), readOnly = TRUE)})


              nw_values <- reactiveValues()
              observeEvent(input$Save,{
                nw_values$data <- hot_to_r(input$myTable)
                chngd_tbl<-data.frame(nw_values$data)

                chngd_tbl<-chngd_tbl[,c("Date","DC_nbr","Catg_nbr","Chamber","adjust_ib","adjust_ob")]
                colnames(chngd_tbl)<-c("Date","DC_nbr","Catg_nbr","Chamber","adjust_ib_nw","adjust_ob_nw")
                str(chngd_tbl)

                s<-merge(dat,chngd_tbl,by=c("Date","DC_nbr","Catg_nbr","Chamber"),all.x = TRUE)
                s$adjust_ib<-coalesce(s$adjust_ib_nw,s$adjust_ib)
                s$adjust_ob<-coalesce(s$adjust_ob_nw,s$adjust_ob)
                s<-s[,!(colnames(s) %in% c("adjust_ib_nw","adjust_ob_nw"))]
                dat<-s
                r<-try(write.csv(s,"/data/forecast_ui_data/data.csv",row.names = F))
                print(r)
                if(is.null(r)){
                  #shinyalert(title="Saved!", "Given inputs are saved",type = "success")
                  showNotification("Given inputs are saved!",type = "message")
                }else{
                  showNotification("something went wrong",type="error")}

              })

            }

            shinyApp(ui = ui, server = server)

我期望selectInput中列的所有唯一值

0 个答案:

没有答案