options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages("shiny")
## Installing package into 'C:/Users/Acer/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'shiny' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Acer\AppData\Local\Temp\RtmpWiOWzy\downloaded_packages
library(shiny)
## Warning: package 'shiny' was built under R version 4.5.2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.2
## 
## 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(readxl)
## Warning: package 'readxl' was built under R version 4.5.2
# ================================
#  LOAD DATA
# ================================
data <- read_excel("D:/2025/IZIN BELAJAR/SEMESTER 2/ANALISIS DAN VISUALISASI DATA/Tugas 3 - weather.xlsx")
head(data)
## # A tibble: 6 × 22
##   MinTemp MaxTemp Rainfall Evaporation Sunshine        WindGustDir WindGustSpeed
##     <dbl>   <dbl>    <dbl>       <dbl> <chr>           <chr>       <chr>        
## 1     8      24.3      0           3.4 6.3             NW          30           
## 2    14      26.9      3.6         4.4 9.699999999999… ENE         39           
## 3    13.7    23.4      3.6         5.8 3.3             NW          85           
## 4    13.3    15.5     39.8         7.2 9.1             NW          54           
## 5     7.6    16.1      2.8         5.6 10.6            SSE         50           
## 6     6.2    16.9      0           5.8 8.199999999999… SE          44           
## # ℹ 15 more variables: WindDir9am <chr>, WindDir3pm <chr>, WindSpeed9am <chr>,
## #   WindSpeed3pm <dbl>, Humidity9am <dbl>, Humidity3pm <dbl>,
## #   Pressure9am <dbl>, Pressure3pm <dbl>, Cloud9am <dbl>, Cloud3pm <dbl>,
## #   Temp9am <dbl>, Temp3pm <dbl>, RainToday <chr>, RISK_MM <dbl>,
## #   RainTomorrow <chr>
# ================================
#  UI
# ================================
ui <- fluidPage(
  titlePanel("Aplikasi Visualisasi Data Interaktif"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("xvar", "Pilih variabel x:", choices = names(data)),
      selectInput("yvar", "Pilih variabel y:", choices = names(data)),
      selectInput("plottype", "Pilih jenis plot:",
                  choices = c("Scatter Plot" = "scatter",
                              "Line Plot"    = "line",
                              "Bar Plot"     = "bar",
                              "Tabel Data"   = "table"))
    ),
    
    mainPanel(
      conditionalPanel(
        condition = "input.plottype != 'table'",
        plotOutput("plot")
      ),
      conditionalPanel(
        condition = "input.plottype == 'table'",
        dataTableOutput("datatable")
      )
    )
  )
)
## `shiny::dataTableOutput()` is deprecated as of shiny 1.8.1.
## Please use `DT::DTOutput()` instead.
## See <https://rstudio.github.io/DT/shiny.html> for more information.
# ================================
#  SERVER
# ================================
server <- function(input, output) {
  
  output$plot <- renderPlot({
    req(input$xvar, input$yvar)
    
    p <- ggplot(data, aes_string(x = input$xvar, y = input$yvar))
    
    if (input$plottype == "scatter") {
      p <- p + geom_point(color = "blue") + ggtitle("Scatter Plot")
    }
    else if (input$plottype == "line") {
      p <- p + geom_line(color = "darkgreen") + ggtitle("Line Plot")
    }
    else if (input$plottype == "bar") {
      p <- p + geom_bar(stat = "identity", fill = "orange") + ggtitle("Bar Plot")
    }
    
    p + theme_minimal()
  })
  
  output$datatable <- renderDataTable({
    data
  })
}

# ================================
#  RUN APP
# ================================
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents