I have undertaken a tutorial to familiarize myself with R Shiny by using this link.
I have followed the script using the highcharter
package, however, whilst the datatable is rendered in the dashboard, no data appears, even though the UTF-8 csv has the relevant data.
R Shiny script used:
# INSERT Libraries HERE
library(shiny)
library(tidyverse)
library(lubridate)
library(highcharter)
# Load the dataset from your CSV file
shefClimateNoNA <- read.csv("C:/Users/Documents/R_files/climate_data/Sheffield_Climate_5year.csv")
# Verify that the TIMESTAMP column is in the correct format (if needed)
shefClimateNoNA$TIMESTAMP <- as.POSIXct(shefClimateNoNA$TIMESTAMP, format = "%Y-%m-%d %H:%M:%S")
##########################
##### User interface #####
##########################
ui <- fluidPage(
# Title
titlePanel(
h1("Hadfield Green Roof 5-year Dataset", style = "padding-bottom: 20px")
),
# Sidebar layout
sidebarLayout(
sidebarPanel(
dateRangeInput(
'dateRange',
label = paste('Date range'),
start = "2011-03-01", end = "2011-12-31",
min = "2011-03-01", max = "2016-02-28",
separator = " to ", format = "dd/mm/yyyy",
startview = 'month', weekstart = 1
)
),
mainPanel(
DT::dataTableOutput(outputId = "dataTable"),
highchartOutput(outputId = "timeSeries"),
)
)
)
###########################
##### Server function #####
###########################
server <- function(input, output, session) {
# Filter data based on selections
output$dataTable <- DT::renderDataTable(DT::datatable({
shefClimateNoNA %>%
filter(
between(
TIMESTAMP,
as_datetime(as.character(input$dateRange[1])),
as_datetime(as.character(input$dateRange[2]))
)
)
}))
# Time series for air temperatures
output$timeSeries <- renderHighchart({
hchart(
shefClimateNoNA %>%
filter(
between(
TIMESTAMP,
as_datetime(as.character(input$dateRange[1])),
as_datetime(as.character(input$dateRange[2]))
)
) %>%
mutate(
TIMESTAMP = format.Date(TIMESTAMP, "%d %b %y")
),
type = "line",
hcaes(x = TIMESTAMP, y = AirTC_Avg),
name = "Air temperature"
) %>%
hc_xAxis(
tickAmount = 10,
tickInterval = 30,
labels = list(format = "{value:%b %y}")
)
})
}
##################################
##### Call shinyApp function #####
##################################
shinyApp(ui = ui, server = server)
Screenshot of output:
It's a date conversion issue. By using shefClimateNoNA$TIMESTAMP <- as.POSIXct(shefClimateNoNA$TIMESTAMP, format = "%Y-%m-%d %H:%M:%S")
, you introduce NA
since the original data looks like 01-Mar-2011 00:00:00
. Use strptime
:
> dput(head(shefClimateNoNA))
structure(list(TIMESTAMP = c("01-Mar-2011 00:00:00", "01-Mar-2011 01:00:00",
"01-Mar-2011 02:00:00", "01-Mar-2011 03:00:00", "01-Mar-2011 04:00:00",
"01-Mar-2011 05:00:00"), WS_ms_Avg = c(1.121, 1.445, 1.511, 0.388,
0.268, 0.386), AirTC_Avg = c(3.091, 3.059, 2.825, 2.941, 3.084,
3.14), RH = c(81.5, 82.1, 86.5, 84.6, 83.1, 83), Slr_kW = c(0,
0, 0, 0, 0, 0), BP_mbar = c(600, 600, 600, 600, 600, 600)), row.names = c(NA,
6L), class = "data.frame")
shefClimateNoNA$TIMESTAMP <-
as.POSIXct(strptime(shefClimateNoNA$TIMESTAMP, format = "%d-%b-%Y %H:%M:%S"))
> head(shefClimateNoNA)
TIMESTAMP WS_ms_Avg AirTC_Avg RH Slr_kW BP_mbar
1 2011-03-01 00:00:00 1.121 3.091 81.5 0 600
2 2011-03-01 01:00:00 1.445 3.059 82.1 0 600
3 2011-03-01 02:00:00 1.511 2.825 86.5 0 600
4 2011-03-01 03:00:00 0.388 2.941 84.6 0 600
5 2011-03-01 04:00:00 0.268 3.084 83.1 0 600
6 2011-03-01 05:00:00 0.386 3.140 83.0 0 600
The error will disappear then.