【问题标题】:R shiny to find the mode of one attribute from a dataset,R闪亮从数据集中找到一个属性的模式,
【发布时间】:2020-05-06 15:17:44
【问题描述】:

我正在尝试使用 R 闪亮构建仪表板,我想生成 2 个值框显示月份和小时,包括最大数量的记录,月份和小时是数据集中的属性,数据集称为犯罪,我需要找到具体的代码来实现我的功能

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

})

这里我带来了一段代码,我不知道下一步该做什么,我的数据集是波士顿的犯罪报告。

【问题讨论】:

  • 我可能错了,但这看起来不像工作的 R 代码。请提供更多背景信息;一个最小的工作完整闪亮的应用程序是理想的,但crimes %&gt;% output$value2 &lt;- ... 是错误的。
  • 是闪亮的代码,你可以删除那段代码,原来的代码是:total.paid % 安排(TOTAL_PAID_BY_INSURANCE) %>% select(TOTAL_PAID_BY_INSURANCE) %>% filter(row_number() ==1) 我想将其更改为包含最多记录数的月份,因为我使用了数据集 CrimeReportInBoston。
  • “你可以删除那段代码” ...我认为最好edit你的问题并解决它(甚至提供更多上下文)。
  • 我修好了,现在不知道下一步要怎么做,包括最大记录的月份和小时。
  • 欢迎来到SO,白侯源!一般来说,关于 SO 的问题应该集中在一个主题上,保持专注。此外,关于 SO(尤其是在 R 中)的问题,如果它们是可重复的和独立的,那么它们会做得更好。我的意思是包括尝试的代码(请明确说明非基础包)、示例代表性数据(可能通过dput(head(x)) 或以编程方式构建数据(例如data.frame(...)),可能在set.seed(1) 之后随机生成),可能是实际输出(带有逐字错误/警告)与预期输出。我建议您创建一个新问题并使其可重现

标签: r date shiny


【解决方案1】:

随机数据。你说“月份和小时是数据集中的属性”,所以我先生成一年以上的随机数据,然后我会用POSIXt的时间来生成@的两列987654322@ 和$hour。 (它们将是字符串,但没关系。)在这些生成之后,我们不再需要或使用$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

查找最频繁出现的事件。

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 

现在调整它以适应可能在闪亮应用中工作的东西。我假设 (a) 数据本身是反应性的,或者 (b) 有其他数据过滤,这样您的计数是 reactive 并且会改变。例如,如果让用户选择过滤要深入研究的犯罪类型,则两个值框将描述该类型最频繁的月份/小时。

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")
})

对于您自己的闪亮应用,请确保此 crimes_rx 是您希望在值框中汇总的数据。 (如果您使用的是静态数据,则只需使用 crimes_rx &lt;- reactive({ crimes }) 或将其删除并将 dat$ 的所有引用替换为 crimes$ 中的 crimes_freq。)

【讨论】:

  • 我认为代码可以,但是我在月份和小时值框中生成了数据8623和4624,如果正确的话应该是7和20。
  • 哇,值框月份输出正确的数字,但小时值框仍然输出错误的数字 01。由于在我的数据集中包含最多记录数的 20,我希望它输出 20。
猜你喜欢
  • 2019-01-18
  • 2021-07-08
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
相关资源
最近更新 更多