我想加载文件并使用文件数据创建预测模型。 UI还显示项目列表并显示所显示项目的价格输入。我想获取这些输入值,创建数据框并在创建的模型上运行预测。然后显示与项目对应的预测值。我是R的新手并且坚持使用reactiveEvents。目前正在显示某些功能定义而不是实际值。任何帮助是极大的赞赏。谢谢!
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')
)
)
))
newdata的输出:
结构(函数(...),{,if(length(outputArgs)!= 0&amp;&amp;
!hasExecuted $ get()){,警告(“未使用的参数:outputArgs。参数
outputArgs只是“,,”意味着在嵌入Shiny的片段时使用
代码在“,,,”R Markdown代码块中(使用运行时:闪亮)。跑步时
一个“,,”,完整的Shiny应用程序,请直接在“,,
。”中设置输出参数
“你的UI代码的相应输出功能。”),
hasExecuted $ set(TRUE),},if(is.null(formals(origRenderFunc))),
origRenderFunc(),否则origRenderFunc(...),},class =“function”,
outputFunc = function(outputId,placeholder = FALSE),{,pre(id =
outputId,class = paste(c(“shiny-text-output”,if(!placeholder)
“noplaceholder”),, collapse =“”)),},outputArgs = list(),hasExecuted
=)