How to manipulate/use reactive expression like a normal dataframe for further computation?

爱⌒轻易说出口 提交于 2019-12-24 08:29:49

问题


This is my first ever Shiny App. I have already created a reactive expression which imports a data i.e data1() and followed by an observe expression to determine the two columns selected. Another reactive expression makes data2() which shows the data that is been formed in the above observe event. I want to use the data created in function data2() for further computation, but i am unable to use it.

I want to use the data of data2() as a dataframe in my observeEvent function. But i am unable to use it and subsequently not able to apply the log furmula as well. I need help regarding how i can use data2() as a dataframe for executing my code further. Attaching the server and ui code.

The rawdata looks like -

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

and so on.......


server part

    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"))
                       ))) 

The outcome that is expected is data2() to be used as dataframe. But i am unable to use it, and my renderDataTable is showing no output as i am not able to use the data created in data2()

来源:https://stackoverflow.com/questions/57097085/how-to-manipulate-use-reactive-expression-like-a-normal-dataframe-for-further-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!