如何像正常数据框一样操纵/使用反应性表达式进行进一步计算?

时间:2019-07-18 14:36:57

标签: r shiny shinydashboard shiny-server

这是我的第一个有光泽的应用程序。 我已经创建了一个反应式表达式,该表达式导入数据即data1(),然后跟随一个观察表达式以确定所选的两列。 另一个反应式表达式使data2()表示在上述观察事件中形成的数据。 我想将在data2()函数中创建的数据用于进一步的计算,但是我无法使用它。

我想将data2()的数据用作我的watchEvent函数中的数据框。但我无法使用它,因此也无法应用原木屑。 我需要有关如何将data2()用作进一步执行代码的数据框的帮助。 附加服务器和ui代码。

原始数据看起来像-

Date           Month             Total_visit           Media_Spend
1/1/2017         1                123000               2000000
1/2/2017         2                234700               2500000

以此类推.......


服务器部分

    library(shiny)
    library(dplyr)
    library(prophet)
    library(ggplot2)
    library(forecast)
    library(shinydashboard)
    library(DT)
    library(rstan)


    shinyServer(function(input,output, session){

       data1 <- reactive({
                        inFile <- input$file1
                        if (is.null(inFile))
                        return(NULL)
                        read.csv(inFile$datapath)
                      })
      observe({
        updateSelectInput(session, inputId = "Dep_Var" , choices = colnames(data1()))
        updateSelectInput(session, inputId = "Ind_Var" , choices = colnames(data1()))
              })

         data2<- reactive({  input1<-data1()
         Dep_Var<-input$Dep_Var   #Total Visits
         Ind_Var<-input$Ind_Var   #Media Spend

         data_p<- input1%>% select(Date,Dep_Var,Ind_Var)
         return(data_p)})


       # output$contents <- renderDataTable(data2()) -------was just checking the output

    }

    observeEvent(input$Submit, {Forecastdata <- reactive({

       data_2p <- data2()

       data_2p$y <- log10(as.numeric(data_2p$Dep_Var))
       data_2p$IndVarLog <- log10((data_2p$Ind_Var))
       data_2p$ds <- as.Date(data_2p$Date, format = "%m/%d/%Y")

             m <- prophet(seasonality.mode = 'multiplicative')
             m <- add_regressor(m, "IndVarLog")
             m <- fit.prophet(m, data_2p)
             forecast_val <- predict(m, data_2p)
    return(data_2p)
     }
     )
          output$contents <- renderDataTable(Forecastdata())
          }
          )
    }
    ) 

    ------------------- ui part


    dashboardPage(
      dashboardHeader(title = "Forecasting Simulator"),
       dashboardSidebar(
         sidebarMenu(
          menuItem(fileInput(inputId =  "file1", label = "Choose a File",
                             multiple = FALSE,
                             accept =c('text/csv', 
                                       'text/comma-separated-values,text/plain', 
                                       '.csv'))),

          checkboxInput("header", "Header", TRUE),

          selectInput("Dep_Var" ,"Select KPI", choices=c()),

          selectInput("Ind_Var","Select Independent Variable", choices = c())
          ),

          actionButton("Submit", "Forecast")
                  ),



    dashboardBody(



              fluidRow(
                        box( width = 8, title = "Final Data",
                            DT::dataTableOutput("contents"))
                       ))) 

预期的结果是将data2()用作数据帧。 但是我无法使用它,而我的renderDataTable没有显示任何输出,因为我无法使用在data2()中创建的数据

0 个答案:

没有答案
相关问题