为什么我渲染的数据表不包含数据?

问题描述 投票:0回答:1

我是 R Shiny 的新手,并使用以下链接学习了一个教程来熟悉 R Shiny:https://dataviz.shef.ac.uk/blog/05/02/2021/Shiny-Template

我已使用“Highcharter”包遵循脚本,但是,当数据表在仪表板中呈现时,即使 UTF-8 csv 具有相关数据,也不会显示任何数据。

使用 R Shiny 脚本

# 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)


输出截图

r shiny dashboard shinydashboard
1个回答
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

然后错误就会消失。

© www.soinside.com 2019 - 2024. All rights reserved.