3

I am trying to create a shiny R application where the user inputs 2 dates: the start date and the end date(assuming that the user will choose either of the dates for a particular week).By choosing the dates the user will be able to see how much he will be selling each item from a list of items next week within those days. I have been provided with data on what percent of total sales happen each day within a week. Using that and using data on sales of each item from past week I have tried to create the app. However I think I am making some error while using the reactive expression. Any help will be greatly appreciated. I have provided the code below.

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()
  })
  })

1 Answer 1

5

I'm not 100% clear on your underlying objective, but regardless the code below runs for me. I tried to comment all of the changes I made - they were mostly just minor syntactic errors - but let me know if you would like me to clarify anything.


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()
  })
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.