0

我想加载一个文件并使用文件数据创建预测模型。UI 还显示项目列表并为显示的项目输入价格。我想获取这些输入值,创建一个数据框并在创建的模型上运行预测。然后显示项目对应的预测值。我对 R 有点陌生,并且坚持使用响应事件。目前显示的是一些函数定义而不是实际值。任何帮助是极大的赞赏。谢谢!

服务器.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()); } ') 
  ) )



})

}
)

用户界面

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

 ) 
 )
))

newdata 的输出: structure(function (...) ,{, if (length(outputArgs) != 0 &&
!hasExecuted$get()) {, warning("未使用的参数:outputArgs。参数
outputArgs 只是 ", , "意味着在 ", , "R Markdown 代码块中嵌入 Shiny 代码片段时使用(使用运行时:闪亮)。运行 ", , "完整的 Shiny 应用程序时,请直接在 ", , 中设置输出参数
"你的 UI 代码对应的输出函数。"), 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"), , 折叠= " ")),}, outputArgs = list(), hasExecuted = )

4

0 回答 0