R的Shiny App中的输出预测图和预测

时间:2019-01-14 16:25:20

标签: r shiny

我正在尝试构建一个工具,用户可以在其中选择多个参数(例如合作伙伴,预算,月份,年份),并获取下个月指标的预定值。

我已经建立了一些外部功能来执行任务。每个函数执行一些任务,并将数据传递到下一个函数,最后一个函数构建四个模型,使用这些模型进行预测,构建ggplots并将它们存储在一个列表中,这些函数一旦运行就可以访问。

如何提取要通过Shiny App显示的图表和预测?

用户界面:

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

  sidebarLayout(
    sidebarPanel(

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

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

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

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


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

mainPanel(plotOutput("plot"))

  )
)

如何构建服务器功能,使其能够识别出submit按钮已被触发并运行外部功能?

我的最终函数称为reg_mod,它会触发所有其他函数,然后返回列表中的最终对象(图表,预测等),其输入如下:

reg_mod(partner, budget, month, year)

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

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

output$plot = renderPlot(plot[[1]])

其中plot[[1]]调用第一个对象,它是来自reg_mod的情节

编辑:

因此,这是一个辅助函数的示例,该函数创建一些数据并输出一个绘图,我想要将该绘图导出到一个闪亮的App中,与我想要执行的操作类似。 (请注意,唯一的月份选择选项是“八月”,而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)

1 个答案:

答案 0 :(得分:2)

这是有关如何执行此操作的通用框架:

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

步骤1) 将具有功能的脚本保存在应用程序目录中(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]] 
})


}

可以在here

中找到有关在Shiny中使用外部功能的其他帮助。

在此示例中here有关使用反应性环境的其他帮助

编辑: 解决该图形无法渲染的问题是,由于使用了错误的ui元素,因此您没有激活要运行的函数。应该是actionButton而不是submitButton

actionButton("Submit", "Submit"),改用它

完整代码

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)