提问人:db2020 提问时间:10/27/2023 更新时间:10/28/2023 访问量:62
RShiny 呈现的无数据数据表
RShiny rendered datatable without data
问:
我是 RShiny 的新手,并且已经进行了一个教程,通过使用以下链接来熟悉 RShiny,https://dataviz.shef.ac.uk/blog/05/02/2021/Shiny-Template。
我使用“Highcharter”包遵循脚本,但是,当数据表在仪表板中呈现时,即使 UTF-8 csv 具有相关数据,也不会显示任何数据。
使用的 RShiny 脚本
# 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)
答:
0赞
Jan
10/28/2023
#1
这是一个日期转换问题。通过使用 ,您可以引入,因为原始数据看起来像 。用:shefClimateNoNA$TIMESTAMP <- as.POSIXct(shefClimateNoNA$TIMESTAMP, format = "%Y-%m-%d %H:%M:%S")
NA
01-Mar-2011 00:00:00
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
然后错误将消失。
评论
shefClimateNoNA %>% filter(between( TIMESTAMP, as_datetime(as.character(input$dateRange[1])),as_datetime(as.character(input$dateRange[2])) ))