使用多个反应值

Using multiple reactive values

我正在尝试创建一个闪亮的 R 应用程序,用户可以在其中输入 2 个日期:开始日期和结束日期(假设用户将选择特定周的任一日期)。通过选择日期用户将能够在下周的那些日子里看到他将从商品列表中销售的每件商品的价格。我得到了一周内每天发生的总销售额百分比的数据。使用它并使用过去一周的每件商品的销售数据,我试图创建该应用程序。但是我认为我在使用反应式表达式时犯了一些错误。任何帮助将不胜感激。我提供了以下代码。

ui.R
library(shiny)
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      dateInput('Start_Date',label = "starting on:",value = Sys.Date())
      dateInput('End_Date',label = "Ending on:",value = Sys.Date())
    ),
    mainPanel(
      tableoutput("mytable")
      )
  )
  ))

server.R
library(shiny)
library(stats)
shinyServer(function(input, output) {
  Days<-c("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
  Percent_sales_by_day<-c(.10,.14,.14,.14,.14,.17,.17)
  Data_days<-data.frame(Days,Percent)
  items_sold<-c("A","B","C","D")
  sales_last_week<-c("100","200","300","800")
  Data_sales<-data.frame(items_sold,sales_last_week)
  Day_vector<-reactive({
    weekdays(seq(as.Date(input$Start_Date),as.Date(input$End_Date),by = "days")) 
  })
  Daily_split_vector<-reactive({
    library(dplyr)
    Data_days%>%
      filter(Days %in% Day_vector())
    Data_days$Percent_sales_by_day
  })
  Daily_split_value<-reactive({
    sum(Daily_split_vector())
  })
  Forecast<-reactive({
    Data_sales%>%
      mutate(sales_last_week=sales_last_week* Daily_split_value())
  })
  output$mytable<-renderTable({
    Forecast()
  })
  })

我不是 100% 清楚你的基础 objective,但不管下面的代码是否适合我。我试图评论我所做的所有更改 - 它们大多只是轻微的语法错误 - 但如果您希望我澄清任何内容,请告诉我。


ui.R:

library(shiny)
##
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(

      dateInput(
        'Start_Date',
        label = "starting on:",
        value = Sys.Date()
      ), ## added comma
      dateInput(
        'End_Date',
        label = "Ending on:",
        value = Sys.Date())
    ),

    mainPanel(
      tableOutput("mytable") ## 'tableOutput' not 'tableoutput'
    )

  )
))

server.R:

library(shiny)
library(dplyr)
options(stringsAsFactors=F)  ## try to avoid factors unless you
                             ## specifically need them
##
shinyServer(function(input, output) {

  Days <- c(
    "Sunday","Monday","Tuesday","Wednesday",
    "Thursday","Friday","Saturday")

  Percent_sales_by_day <- c(
    .10,.14,.14,.14,.14,.17,.17)

  Data_days <- data.frame(
    Days,
    Percent_sales_by_day) ## changed from 'Percent'

  items_sold <- c("A","B","C","D")

  sales_last_week <- c(
    100,200,300,800) ## changed from character (???) to numeric type

  Data_sales <- data.frame(
    items_sold,
    sales_last_week)

  Day_vector <- reactive({
    weekdays(
      seq.Date(
        as.Date(input$Start_Date),
        as.Date(input$End_Date),
        by = "day")) 
  })

  Daily_split_vector <- reactive({
    Data_days %>%
      filter(Days %in% Day_vector()) %>% ## added pipe
   ## Data_days$Percent_sales_by_day  ## changed this line
      select(Percent_sales_by_day)    ## to this line
  })

  Daily_split_value <- reactive({
    sum(Daily_split_vector())
  })

  Forecast <- reactive({
    Data_sales%>%
      mutate(
        sales_last_week=sales_last_week* Daily_split_value())
  })

  output$mytable <- renderTable({
    Forecast()
  })
})