Skip to content

Instantly share code, notes, and snippets.

@liliya2022
Last active March 2, 2023 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liliya2022/a6d3737a2b5e074f4c35c5f2b0c8aae6 to your computer and use it in GitHub Desktop.
Save liliya2022/a6d3737a2b5e074f4c35c5f2b0c8aae6 to your computer and use it in GitHub Desktop.
#libraries:
library(shiny)
library(shinythemes)
library(lubridate)
library(dygraphs)
library(xts)
library(tidyverse)
bitcoin <-read.csv(file = 'BTC-USD.csv', stringsAsFactors = F)
bitcoin$Date <- ymd(bitcoin$Date)
ui <- fluidPage(
theme = shinytheme("cosmo"),
# App title ----
titlePanel(strong("Bitcoin Dashboard")),
sidebarLayout(
sidebarPanel(
h4(strong("Bitcoin closing prices 2014-2022")),
br(),
selectInput('selectOutput',
'Select output',
choices = colnames(bitcoin)[c(2,3,4,5,7)]),
dateRangeInput('selectDate',
'Select date',
start = min(bitcoin$Date),
end = max(bitcoin$Date)),
br(),
br(),
checkboxInput("logInput", "Plot y axis on log scale", value = FALSE),
),
mainPanel(dygraphOutput("priceGraph", width = "100%", height = "800px"))
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
getData <- reactive({
# get inputs
selectedOutput <- input$selectOutput
startDate <- input$selectDate[1]
endDate <- input$selectDate[2]
# filter data
data <- bitcoin %>%
select(Date, selectedOutput) %>%
filter(Date >= startDate & Date <= endDate)
# formatting in case of market cap
if(selectedOutput == "Volume"){
data["Volume"] <- lapply(data["Volume"], FUN = function(x) x/1000000000)
}
# converting to logscale for bitcoin price
if(input$logInput == TRUE & input$selectOutput != "Volume"){
data[selectedOutput] = log(data[selectedOutput])
}
data
})
output$priceGraph <- renderDygraph({
data <- getData()
time_series <- xts(data, order.by = data$Date)
dygraph(time_series)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment