NBE
NBE

Reputation: 651

How to show the last date entry from a reactive data frame in shiny R

I have a shiny app that I want to show somewhere in the UI the date of the last data entry added to the spreadsheet. I'm sure this is very simple but I can't seem to figure it out. I would appreciate it if someone can point me in the right direction.

Sample Data

df<-structure(list(`AQS Code` = c(340071001, 340170006, 340010006, 
340070002, 340273001, 340150002, 340290006, 340410007, 340190001, 
340030006, 340110007, 340250005, 340130003, 340315001, 340210005, 
340230011, 340219991, 340071001, 340170006, 340010006), Latitude = c("39.684249999999999", 
"40.670250000000003", "39.464872", "39.934446000000001", "40.787627999999998", 
"39.800339000000001", "40.064830000000001", "40.924579999999999", 
"40.515262", "40.870435999999998", "39.422272999999997", "40.277647000000002", 
"40.720989000000003", "41.058616999999998", "40.283092000000003", 
"40.462181999999999", "40.3125", "39.684249999999999", "40.670250000000003", 
"39.464872"), State = c("NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
"NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
"NJ", "NJ"), `Site Name` = c("Ancora State Hospital", "Bayonne", 
"Brigantine", "Camden Spruce St", "Chester", "Clarksboro", "Colliers Mills", 
"Columbia", "Flemington", "Leonia", "Millville", "Monmouth University", 
"Newark Firehouse", "Ramapo", "Rider University", "Rutgers University", 
"Washington Crossing", "Ancora State Hospital", "Bayonne", "Brigantine"
), `Elev (m)` = c("33.0", "3.0", "5.0", "4.0", "278.0", "12.0", 
"41.0", "146.0", "45.0", "1.0", "22.0", "8.0", "27.0", "307.0", 
"30.0", "19.0", "61.0", "33.0", "3.0", "5.0"), County = c("Camden, NJ", 
"Hudson, NJ", "Atlantic, NJ", "Camden, NJ", "Morris, NJ", "Gloucester, NJ", 
"Ocean, NJ", "Warren, NJ", "Hunterdon, NJ", "Bergen, NJ", "Cumberland, NJ", 
"Monmouth, NJ", "Essex, NJ", "Passaic, NJ", "Mercer, NJ", "Middlesex, NJ", 
"Mercer, NJ", "Camden, NJ", "Hudson, NJ", "Atlantic, NJ"), Date = structure(c(17956, 
17956, 17956, 17956, 17956, 17956, 17956, 17956, 17956, 17956, 
17956, 17956, 17956, 17956, 17956, 17956, 17956, 17957, 17957, 
17957), class = "Date"), AQI_value = c(36, 30, 36, 32, 34, 30, 
40, 26, 27, 31, 38, 44, 28, 35, 36, 35, 35, 31, 34, 34)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L))

Code

    library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(textOutput("test"))
)

server <- function(input, output) { 

  df_reac<-reactive({
    df
  })

  output$test<-renderText(max(df_reac()$Date,na.rm = T))
  }

shinyApp(ui, server)

It keeps giving me a weird number and not the max date... not sure why though.

Upvotes: 1

Views: 85

Answers (1)

akrun
akrun

Reputation: 887158

Iit is possible that the Date class (can be checked with output$test<-renderText(class(max(df_reac()$Date,na.rm = TRUE))))

gets coerced to integer storage mode. We can convert to character and it should print correctly

output$test<-renderText(as.character(max(df_reac()$Date,na.rm = TRUE)))

-output

enter image description here

Upvotes: 1

Related Questions