【发布时间】:2021-10-08 18:48:32
【问题描述】:
我有一份人员名单以及他们每个月从事的项目数量。我希望我闪亮应用的用户选择日期范围输入显示的月份。
library(dplyr)
testdata <- tibble(employee = c("Justin", "Corey","Sibley"),
apr_2021 = c(10, 100, 101),
may_2021 = c(1, 4, 7),
jun_2021 = c(4, 5, 6),
jul_2021 = c(11, 11, 45),
aug_2021 = c(4, 5, 7),
sep_2021 = c(2, 1, 0),
oct_2021 = c(4, 5, 8),
nov_2021 = c(4, 1, 1))
我将 daterangeinput() 配置为 M-YYYY 或 Oct_2021 的格式。当我尝试调整 date_filter 输入时,我收到该列不存在的错误(例如,当用户输入 2021 年 8 月 3 日时:
Warning: Error in : Can't subset columns that don't exist.
x Column `2021_03` doesn't exist.
有两种解决方案之一。理想情况下,我想知道为什么 R 会更改我在 daterangeinput 中指定的格式以及如何修复它,这样我就可以保留我当前的代码(如下)。我不确定这是否与我使用 today() 函数有关,但我需要 R 来了解当前月份和年份。
如果不可能的话,第二个解决方案是如何将这些字符串转换为 R 尝试使用的新格式(似乎是 YYYY-mm-dd)。
这是一个可重现的例子:
library(shiny)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)
library(dplyr)
library(htmltools)
library(lubridate)
library(stringr)
ui = fluidPage(
tabsetPanel(
tabPanel("View 1", fluid = TRUE,
sidebarLayout(
sidebarPanel(
h4("Select Your Desired Filters"),
div(id = "inputs",
dateRangeInput(
inputId = "date_filter",
label = "Filter by Month and Year",
start = today(),
end = (today() + 90),
min = "Apr-2021",
max = NULL,
format = "M-yyyy",
startview = "month",
weekstart = 0,
language = "en",
separator = " to ",
width = NULL,
autoclose = TRUE
),
br()),
),
mainPanel(
DT::dataTableOutput("mytable")
)
)
)
)
)
server = function(input, output, session) {
#Here's the dataset
testdata <- tibble(employee = c("Justin", "Corey","Sibley"),
apr_2021 = c(10, 100, 101),
may_2021 = c(1, 4, 7),
jun_2021 = c(4, 5, 6),
jul_2021 = c(11, 11, 45),
aug_2021 = c(4, 5, 7),
sep_2021 = c(2, 1, 0),
oct_2021 = c(4, 5, 8),
nov_2021 = c(4, 1, 1))
select_values <- reactive({
from_date <- as.character(input$date_filter[1])
from_date <- tolower(str_replace_all(from_date, "-..-", "_"))
to_date <- as.character(input$date_filter[2])
to_date <- tolower(str_replace_all(to_date, "-..-", "_"))
testdata %>%
dplyr::select(employee, from_date:to_date)
})
output$mytable = DT::renderDataTable({
datatable(select_values())
})
}
shinyApp(ui = ui, server = server)
【问题讨论】:
标签: r date dplyr shiny stringr