0

i am trying to build a dashboard with R shiny, i want to generate 2 value boxes show the month and hour including the largest number of records, month and hour are attributes in the dataset, The dataset is called crime, I need to find the specific codes to implement my function

total.records <- sum(crimes$OCCURENCE_ON_DATE)

total.records.hour{need to be implemented }

  output$value2 <- renderValueBox({

valueBox(
  formatC(total.records.hour, format="d", big.mark=',')
  ,'The hour including most records'
  ,icon = icon("gbp",lib='glyphicon')
  ,color = "navy")

})

Here I bring piece of codes and I don`t know what to do next, my dataset is crimes report in Boston.

  • I may be wrong, but this doesn't look like working R code. Please provide some more context; a minimal working complete shiny app would be ideal, but `crimes %>% output$value2 <- ...` is wrong. – r2evans May 06 '20 at 16:15
  • It is the shiny code, you can delete that piece of code, the original code is : total.paid <- sum(healthcare$TOTAL_PAID_BY_INSURANCE) total.paid.min <- healthcare %>% arrange(TOTAL_PAID_BY_INSURANCE) %>% select(TOTAL_PAID_BY_INSURANCE) %>% filter(row_number() ==1) I want to change it to the month including the largest number of records because I used the dataset CrimeReportInBoston. – Houyuan Bai May 06 '20 at 16:33
  • *"you can delete that piece of code"* ... I think it best if you [edit] your question and fix it (and even give more context). – r2evans May 06 '20 at 16:59
  • I fixed it , and now, I don`t know how to do the next step to grab the month and the hour including the largest number of records. – Houyuan Bai May 06 '20 at 20:53
  • Welcome to SO, Houyuan Bai! In general, a question on SO should be on one topic, stay focused. In addition, questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically after `set.seed(1)`), perhaps actual output (with verbatim errors/warnings) versus intended output. I suggest you create a new question and make it *reproducible*. – r2evans May 06 '20 at 20:56
  • Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans May 06 '20 at 20:56
  • Thanks for your advice, I changed the question. – Houyuan Bai May 07 '20 at 01:45
  • (1) `data set 'crimes' not found`, please be clear on its source. (2) I wonder if this is about `shinydashboard` or if this is manipulating data to get a summary statistic. If the latter, I suggest you simplify the problem to remove unnecessary components. – r2evans May 07 '20 at 03:16
  • The data set is on my computer and I don`t know how to upload it on the SO since it is really huge, and I think I might need to remove this component. – Houyuan Bai May 07 '20 at 21:16
  • I've already suggested `dput`, `data.frame`, and three links with those suggestions and more. – r2evans May 07 '20 at 21:19
  • ok, I need to have a try, thanks for your advice. – Houyuan Bai May 07 '20 at 22:28
  • But i really don`t know how to implement the code, it is too difficult – Houyuan Bai May 07 '20 at 22:31

1 Answers1

0

Random data. You said that "month and hour are attributes in the dataset", so I'll first generate random data over one year, then I'll take that POSIXt time to generate the two columns of $month and $hour. (They'll be strings, but it doesn't matter.) After those are made, we no longer need or use $when.

set.seed(42)
crimes0 <- data.frame(when = as.POSIXct('2020-01-01 00:00:00', tz="UTC") + runif(100000, min = 0, max = 365*24*3600))
head(crimes0)
#                  when
# 1 2020-11-29 21:42:03
# 2 2020-12-08 00:46:50
# 3 2020-04-14 10:34:56
# 4 2020-10-30 02:43:16
# 5 2020-08-22 05:41:26
# 6 2020-07-08 11:16:49
range(crimes0$when)
# [1] "2020-01-01 00:04:19 UTC" "2020-12-30 23:56:02 UTC"

crimes0 <- within(crimes0, {
  month = format(when, format = "%b")
  hour  = format(when, format = "%H")
})
head(crimes0)
#                  when hour month
# 1 2020-11-29 21:42:03   21   Nov
# 2 2020-12-08 00:46:50   00   Dec
# 3 2020-04-14 10:34:56   10   Apr
# 4 2020-10-30 02:43:16   02   Oct
# 5 2020-08-22 05:41:26   05   Aug
# 6 2020-07-08 11:16:49   11   Jul

Finding the most frequent occurrences.

sort(table(crimes0$month), decreasing = TRUE)
#  Jul  May  Jan  Oct  Mar  Dec  Aug  Nov  Sep  Jun  Feb  Apr 
# 8623 8543 8539 8456 8429 8346 8319 8294 8242 8193 8073 7943 

sort(table(crimes0$hour), decreasing = TRUE)
#   01   23   14   05   07   09   03   06   00   15   21   10   02   16   11   18   13   12   17   19   20   22   04   08 
# 4264 4246 4239 4237 4231 4227 4217 4214 4207 4193 4193 4189 4171 4171 4147 4124 4107 4102 4100 4093 4092 4082 4080 4074 

Now adapt that to something that might work in a shiny app. I'm assuming that either (a) the data itself is reactive, or (b) there is other filtering of the data such that your counts are reactive and will change. For example, if the user is given the choice of filtering the types of crimes to drill down into, then the two value boxes would depict the most frequent month/hour for that type.

library(shiny)
library(shinydashboard)

# if this is shiny, then the data is or should be something reactive ...
# I'll assume that 'crimes' is reactive data
crimes_rx <- reactive({
  # ...
  crimes0
})
crimes_freq <- reactive({
  dat <- req(crimes_rx())
  list(
    month = sort(table(dat$month), decreasing = TRUE),
    hour = sort(table(dat$hour), decreasing = TRUE)
  )
})
output$value1 <- renderValueBox({
  req(crimes_freq())
  valueBox(
    names(crimes_freq()$month)[1]
    ,'The month including most records'
    ,icon = icon("gbp",lib='glyphicon')
    ,color = "navy")
})
output$value2 <- renderValueBox({
  req(crimes_freq())
  valueBox(
    names(crimes_freq()$hour)[1]
    ,'The hour including most records'
    ,icon = icon("gbp",lib='glyphicon')
    ,color = "navy")
})

For your own shiny app, make sure that this crimes_rx is the data you want summarized in the value boxes. (If you are using static data, then just use crimes_rx <- reactive({ crimes }) or remove that and replace all references of dat$ by crimes$ in crimes_freq.)

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • I think the code works, but I generated the data 8623 and 4624 in the month and hour value box, which should be 7 and 20 if it is correct. – Houyuan Bai May 08 '20 at 04:29
  • Wow, the value box month outputs the right number, but the hour value box still outputs a wrong number 01.Since in my dataset the 20 including the most number of records, I want it outputs 20. – Houyuan Bai May 08 '20 at 17:12