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