Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Shining App中为R输出预测图和预测_R_Shiny - Fatal编程技术网

在Shining App中为R输出预测图和预测

在Shining App中为R输出预测图和预测,r,shiny,R,Shiny,我正在尝试建立一个工具,用户可以选择一些参数,例如合作伙伴、预算、月份、年份,并返回下个月的预测值 我构建了几个外部函数来执行该任务。每个函数执行一些任务并将数据传递给下一个函数,最后一个函数构建四个模型,使用这些模型进行预测,构建GGPlot并将其存储在一个列表中,一旦函数运行,就可以访问该列表 如何提取要通过闪亮应用程序显示的曲线图和预测 用户界面: 我可以根据用户输入访问对象并在服务器功能中输出它们吗?例如: plot = reg_mod(input$Partner, input$Budg

我正在尝试建立一个工具,用户可以选择一些参数,例如合作伙伴、预算、月份、年份,并返回下个月的预测值

我构建了几个外部函数来执行该任务。每个函数执行一些任务并将数据传递给下一个函数,最后一个函数构建四个模型,使用这些模型进行预测,构建GGPlot并将其存储在一个列表中,一旦函数运行,就可以访问该列表

如何提取要通过闪亮应用程序显示的曲线图和预测

用户界面:

我可以根据用户输入访问对象并在服务器功能中输出它们吗?例如:

plot = reg_mod(input$Partner, input$Budget, input$Month, input$year)

output$plot = renderPlot(plot[[1]])
其中plot[[1]]调用第一个对象,该对象是来自reg_mod的plot

编辑:

这里是一个助手函数的例子,它创建一些数据并输出一个绘图,我想把它源到一个闪亮的应用程序中,类似于我想做的。注意,唯一的月份选择选项是2017年8月和2017年,这很好,可以只运行应用程序并运行默认值

Helper = function(partner, budget, month, year){

 nums = seq(0 , 10, length.out = 100)

 Registrations = 5 + 2*rnorm(100, 20, 7)*nums
 dates = seq.Date(as.Date("2017-01-01"), as.Date("2017-04-10"), by ="days")

  Registrations = data.frame(Date = dates, Registrations = Registrations)

  if(partner == "dsp" & month == "August" & year == 2017){

    Registrations$Registrations = 0.5*budget*Registrations$Registrations
  }

  if(partner == "search" & month == "August" & year == 2017){

    Registrations$Registrations = 2*budget*Registrations$Registrations

  }

  if(partner == "social" & month == "August" & year == 2017){

    Registrations$Registrations = budget*Registrations$Registrations

  }

  Plot = ggplot(data = Registrations, aes(x = Date)) + 
    geom_line(aes(y = Registrations, colour = "Actual"), size = 1)

  List_Read = list("Plot" = Plot)

  return(List_Read)

}
以下是我的UI和服务器功能:

library(shiny)
library(ggplot2)
# Define UI for application that outputs ggplot
ui <-  fluidPage(
  titlePanel("Forecasting Tool"),

  sidebarLayout(
    sidebarPanel(

     selectInput("Partner", h3("Partner"), 
            choices = list("all", "dsp","search","social"), selected="dsp"),

  numericInput("Budget", 
               h3("Budget"), 
               value = 100),

selectInput("Month", h3("Month"), 
              choices = list("January", "February", "March", "April","May", 
              "June", "July",  "August", "September", "October", "November", 
              "December"), 
              selected = "August"),

  selectInput("Year", h3("Year"), 
              choices = list(2017,2018), 
              selected = 2017),


       submitButton("Submit"), h3("")),

mainPanel(plotOutput("plot"))

 )
)
# Define server logic required to output ggplot from helper function
server <- function(input, output, session) {

  source("C:/Users/ksiopes/Documents/Ad Hoc/Survival/Prefinal Insights 
   Function Files/For Shiny Function/Testing/Helper3.R", 
     local = FALSE)

  plot_List = eventReactive(input$Submit,{

    Helper(input$Partner, input$Budget, input$Month, input$Year)

  })

  #Output the plot
  output$plot<- renderPlot({
    plot = plot_List()
    plot[[1]]
  })

}

# Run the application 
shinyApp(ui = ui, server = server)
编辑2:

library(shiny)
library(ggplot2)


source("C:/Users/ksiopes/Documents/Ad Hoc/Survival/Prefinal Insights 
Function Files/For Shiny Function/Testing/Helper3.R", local = TRUE)

# Define UI for application that uses function to create random data and 
output ggplot
ui <-  fluidPage(
  titlePanel("Forecasting Tool"),

  sidebarLayout(
    sidebarPanel(

      selectInput("Partner", h3("Partner"), 
              choices = list("all", "dsp","search","social"), selected = 
"dsp"),

      numericInput("Budget", 
                   h3("Budget"), 
                   value = 100),

      selectInput("Month", h3("Month"), 
                  choices = list("January", "February", "March", 
                   "April","May", "June", "July", "August", "September", 
                    "October", "November", "December"), 
              selected = "August"),

      selectInput("Year", h3("Year"), 
                  choices = list(2017,2018), 
                  selected = 2017),


      submitButton("Submit"), h3("")),

    mainPanel(plotOutput("plot"))

  )
)

# Define server logic required to run sourced function and output ggplot
server <- function(input, output, session) {


  plot_List = eventReactive(input$Submit,{



    Helper(input$Partner, input$Budget, input$Month, input$Year)


  })
    #Output the plot
    output$plot<- renderPlot({
    plot = plot_List()
    plot[[1]]

    })



}

# Run the application 
shinyApp(ui = ui, server = server)

以下是关于如何实现这一点的一般框架:

与本地脚本相比,将外部函数加载到一个闪亮的应用程序需要一些额外的工作,但并不多。我们仍然可以使用源函数,唯一的区别是必须设置local=T,并且该函数必须放在app目录中。一个是源函数,我们可以相对容易地将用户输入传递给它

第一步 在server.R&ui.R或app.R所在的应用程序目录中使用函数保存脚本

步骤2 源函数

#Basically source the path to the function file you put in the app directory
source("your-app-dir/functions.R")
步骤3 使用函数,输出绘图

server.R

server<-function(input,output,session){

#Function reg_mod in this file
source("your-app-dir/functions.R")

#Assuming the return of reg_mod is correct
plot_List<-eventReactive(input$Submit, {

reg_mod(input$Partner, input$Budget, input$Month, input$year)

})

#Output the plot
output$plot<- renderPlot({
plot<-plot_List()
plot[[1]] 
})


}

你的问题可能太广泛,我们无法在这里有效地帮助你。我强烈建议您从一个简单的闪亮教程开始,熟悉您连接ui和服务器功能的方式。非常感谢!还有一个问题:我应该把eventReactive函数包装在observeEvent中,输入$Submit,{……}吗?thanks@KeitheventReactive本身就是一个observeEvent,唯一的区别是它将返回一个变量,我们将使用其中一个,而不是两个。我们正在关注事件eventReactiveinput$Submit。。。因此,在按下submit时,代码块将运行,并返回反应变量plot_list的值,下面是该原理的解释,非常感谢您的帮助。非常感谢。你千方百计回答我的问题。我很难在应用程序的主面板中输出绘图。我不确定会发生什么,。我一直在玩弄代码,但似乎无法将其输出。关于故障排除有什么想法吗?谢谢
#Basically source the path to the function file you put in the app directory
source("your-app-dir/functions.R")
server.R

server<-function(input,output,session){

#Function reg_mod in this file
source("your-app-dir/functions.R")

#Assuming the return of reg_mod is correct
plot_List<-eventReactive(input$Submit, {

reg_mod(input$Partner, input$Budget, input$Month, input$year)

})

#Output the plot
output$plot<- renderPlot({
plot<-plot_List()
plot[[1]] 
})


}
library(shiny)
library(ggplot2)


source("Helper3.R", local = TRUE)

# Define UI for application that uses function to create random data and 

ui <-  fluidPage(
  titlePanel("Forecasting Tool"),

  sidebarLayout(
    sidebarPanel(

      selectInput("Partner", h3("Partner"), 
                  choices = list("all", "dsp","search","social"), selected = 
                    "dsp"),

      numericInput("Budget", 
                   h3("Budget"), 
                   value = 100),

      selectInput("Month", h3("Month"), 
                  choices = list("January", "February", "March", 
                                 "April","May", "June", "July", "August", "September", 
                                 "October", "November", "December"), 
                  selected = "August"),

      selectInput("Year", h3("Year"), 
                  choices = list(2017,2018), 
                  selected = 2017),


      actionButton("Submit", "Submit"), 

      h3("")),

    mainPanel(plotOutput("plot"))

  )
)

# Define server logic required to run sourced function and output ggplot
server <- function(input, output, session) {


  plot_List<-eventReactive(input$Submit, {

    print("ran")
    Helper(input$Partner, input$Budget, input$Month, input$Year)



  })
  #Output the plot
  output$plot<- renderPlot({
    plot = plot_List()
    plot[[1]]

  })



}

# Run the application 
shinyApp(ui = ui, server = server)