library(shiny)
## Warning: package 'shiny' was built under R version 4.2.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(shinydashboard)
## Warning: package 'shinydashboard' was built under R version 4.2.3
## 
## Attaching package: 'shinydashboard'
## The following object is masked from 'package:graphics':
## 
##     box
#SERVER AND UI CODE combined into 1 R Script(Ui.r and server.r codes in 1 file)
# Load the data
data <- read.csv("C:/Users/91990/Desktop/energydata.csv", header = TRUE)

# Convert date column to a date format
data$date <- as.Date(data$date)

# Define UI
ui <- dashboardPage(
  dashboardHeader(title = "Energy Consumption Dashboard- Based On Appliance Energy Dataset"),
  dashboardSidebar(
    selectInput("appliance", "Choose Appliance",
                choices = c("All", unique(data$Appliances))),
    dateRangeInput("dates", "Choose Date Range",
                   start = min(data$date), end = max(data$date)),
    selectInput("variables", "Choose Variables",
                choices = c("T1", "RH_1", "T2", "RH_2", "T3", "RH_3", "T4", "RH_4",
                            "T5", "RH_5", "T6", "RH_6", "T7", "RH_7", "T8", "RH_8",
                            "T9", "RH_9", "T_out", "Press_mm_hg", "RH_out", "Windspeed",
                            "Visibility", "Tdewpoint"),
                multiple = TRUE),
    selectInput("plots", "Choose Plots",
                choices = c("line", "point", "bar"),
                multiple = TRUE),
    actionButton("plotBtn", "Plot")
  ),
  dashboardBody(
    fluidRow(
      box(plotOutput("plot"), width = 12)
    )
  )
)

# Define server
server <- function(input, output) {
  
  # Filter data based on user inputs
  filtered_data <- eventReactive(input$plotBtn, {
    data %>%
      filter(Appliances == input$appliance | input$appliance == "All",
             date >= input$dates[1] & date <= input$dates[2]) %>%
      select(date, input$variables)
  })
  
  # Create plot based on user inputs
  output$plot <- renderPlot({
    p <- ggplot(filtered_data(), aes(x = date))
    if ("line" %in% input$plots) {
      p <- p + geom_line(aes_string(y = input$variables), color = "red")
    }
    if ("point" %in% input$plots) {
      p <- p + geom_point(aes_string(y = input$variables), color = "blue")
    }
    if ("bar" %in% input$plots) {
      p <- p + geom_bar(aes_string(y = input$variables), stat = "identity", fill = "green")
    }
    p <- p + labs(title = "Energy Consumption Dashboard Plot- Shreyas Shashank Deulkar 20BDS0390")

    p + scale_color_manual(values=c("red", "blue", "green"))
  })
}

# Run the app
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.