0

I want to load a file and create predictive model using file data. The UI also displays a list of items and takes price input for the items displayed. I would like to take these input values, create a dataframe and run prediction over the created model. And then display the predicted values corresponding to the items. I am kind of new to R and stuck with the reactiveEvents. Currently some function definition is getting displayed instead of the actual values. Any help is greatly appreciated. Thanks!

server.R

shinyServer(
library(htmlwidgets)
function(input, output) {
Items<-c("1","2","3")
Items_Desc<-c("A","B","C")
mydata<-data.frame(Items_Desc,Items)

# helper function for making multiple price input
shinyInput = function(FUN, len, id,...) { 
  inputs = character(len) 
  for (i in seq_len(len)) { 
    inputs[i] = as.character(FUN(paste0(id, i),label=NULL,value=0,...)) 
  } 
  inputs 
} 
# helper function to get user inputs
  shinyValue = function(id, len) { 
  unlist(lapply(seq_len(len), function(i) { 
    value = input[[paste0(id, i)]] 
    if (is.null(value)) NA else value 
  })) 
} 

#Reactive model for file opening 
model<- reactive(function(){
  if(is.null(input$file1)){return()}
  inFile <- input$file1
  file.rename(inFile$datapath,paste(inFile$datapath, ".xlsx", sep=""))
  modeldata<-data.frame(read_excel(paste(inFile$datapath, ".xlsx",sep=""), 1))
  modeldata$Train_Flag<-'Y'
  create_model(modeldata)

})
model_equation<-reactive({model()})
output$mytable = DT::renderDataTable({
  data.frame(mydata,Price=shinyInput(numericInput,nrow(mydata),"price_",min=2,max=200,step=2),Sales="")
}, selection='none',server = FALSE, escape = FALSE, options = list( 
  paging=TRUE,
  preDrawCallback = JS('function() { 
                       Shiny.unbindAll(this.api().table().node()); }'), 
  drawCallback = JS('function() { 
                    Shiny.bindAll(this.api().table().node()); } ') 
  ) )

observeEvent(input$Go,{  

Price_selected<-reactive({data.frame(selected=shinyValue("price_",nrow(mydata)))})
Date<-renderPrint({input$Date})
newdata<-reactive({cbind(mydata,Date,Price_selected)})
temp <- renderPrint({Sales_predict(model_equation,newdata,derived_modeldata)})
finaldata<-cbind(newdata,temp)

output$mytable = DT::renderDataTable({finaldata}, selection='none',server = FALSE, escape = FALSE, options = list( 
  paging=TRUE,
  preDrawCallback = JS('function() { 
                       Shiny.unbindAll(this.api().table().node()); }'), 
  drawCallback = JS('function() { 
                    Shiny.bindAll(this.api().table().node()); } ') 
  ) )



})

}
)

ui.R

library(shinythemes)
shinyUI(fluidPage(theme=shinytheme("united"),
titlePanel("xxx"),
sidebarLayout(
 sidebarPanel(# Input: Select a file ----
  fileInput("file1", "Choose excel File",
            multiple = TRUE,
            accept = c(".xlsx")),
    actionButton("Go", 'Go')
),
mainPanel(
  h3('Results of prediction'),
        DT::dataTableOutput('mytable')

 ) 
 )
))

output of newdata: structure(function (...) ,{, if (length(outputArgs) != 0 &&
!hasExecuted$get()) {, warning("Unused argument: outputArgs. The argument
outputArgs is only ", , "meant to be used when embedding snippets of Shiny code in an ", , "R Markdown code chunk (using runtime: shiny). When running a ", , "full Shiny app, please set the output arguments directly in ", ,
"the corresponding output function of your UI code."), hasExecuted$set(TRUE), }, if (is.null(formals(origRenderFunc))) , origRenderFunc(), else origRenderFunc(...),}, class = "function", outputFunc = function (outputId, placeholder = FALSE) ,{, pre(id = outputId, class = paste(c("shiny-text-output", if (!placeholder) "noplaceholder"), , collapse = " ")),}, outputArgs = list(), hasExecuted = )

sak
  • 111
  • 1
  • 11
  • I'm not too familiar with shiny myself, but I can see a few things. 1st: reactive returns a function, so you need to use parenthesis when you use `Price_selected` etc. 2nd: I doubt you need that many reactive expressions. Only the inputs might be enough(not sure about this)? Lastly, renaming your csv as xlsx and reading it as an excel file cannot be a good thing. Use the appropriate function for the given format. – Rohit Apr 30 '18 at 06:59
  • @Rohit I have tried with parenthesis and it give the same result. Also i had earlier used reactive at only the input places but i got errors at other places that it has to be enclosed in reactive as these expressions value will also change when the reactive inputs change. Regarding the file format the chances of csv file being loaded are almost none. If needed I will change that accordingly. Currently the file is loading correctly. – sak Apr 30 '18 at 07:06
  • Any `render` function is a reactive environment. Modifications based on dynamic data need to be made within these functions. Eg: define `newdata` within the `renderPrint` function. – Rohit Apr 30 '18 at 09:44
  • Tried using render print same output..Adding output in ques – sak Apr 30 '18 at 09:57

0 Answers0