首页 > 解决方案 > 为什么日期范围函数在 r 中不起作用

问题描述

我已经尝试了大部分事情,我不知道为什么它不起作用,我试图将日期列转换为日期对象但它仍然无法解决,任何人都可以指导我解决这个问题,因为我是有点坚持这个问题很长一段时间。大多数情况下,我在 MS sql server (sp) 上工作,所以在这种情况下,很容易在存储过程的参数中传递 input$date[1] 和 input$date[2] 但我在使用来自 csv 或电子表格的数据。我尝试了几种方法,但似乎都没有找到,正在寻找解决方法但找不到,如果可能,请指导我解决这个问题。我对此有点陌生,所以如果我在这里遗漏了什么,请告诉我。

library(shiny)
library(plotly)
library(dplyr)
re<- data.frame(
  stringsAsFactors = FALSE,
                     id = c(1L,2L,3L,4L,5L,
                            6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,16L,
                            17L,18L,19L,20L,21L,22L,23L,24L,25L,26L,27L,
                            28L,29L,30L),
                 season = c(2017L,2017L,2017L,
                            2017L,2017L,2017L,2017L,2017L,2017L,2017L,
                            2017L,2017L,2017L,2017L,2017L,2017L,2017L,2017L,
                            2017L,2017L,2017L,2017L,2017L,2017L,2017L,
                            2017L,2017L,2017L,2017L,2017L),
                   city = c("Hyderabad","Pune",
                            "Rajkot","Indore","Bangalore","Hyderabad",
                            "Mumbai","Indore","Pune","Mumbai","Kolkata",
                            "Bangalore","Rajkot","Kolkata","Delhi","Mumbai",
                            "Bangalore","Delhi","Hyderabad","Rajkot","Hyderabad",
                            "Indore","Kolkata","Mumbai","Pune","Rajkot",
                            "Kolkata","Mumbai","Pune","Bangalore"),
                   date = c("4/5/2017",
                            "4/6/2017","4/7/2017","4/8/2017","4/8/2017","4/9/2017",
                            "4/9/2017","4/10/2017","4/11/2017","4/12/2017",
                            "4/13/2017","4/14/2017","4/14/2017","4/15/2017",
                            "4/15/2017","4/16/2017","4/16/2017","4/17/2017",
                            "4/17/2017","4/18/2017","4/19/2017","4/20/2017",
                            "4/21/2017","4/22/2017","4/22/2017","4/23/2017",
                            "4/23/2017","4/24/2017","4/26/2017","4/27/2017"),
                  team1 = c("Sunrisers Hyderabad",
                            "Mumbai Indians","Gujarat Lions",
                            "Rising Pune Supergiant","Royal Challengers Bangalore",
                            "Gujarat Lions","Kolkata Knight Riders",
                            "Royal Challengers Bangalore","Delhi Daredevils","Sunrisers Hyderabad",
                            "Kings XI Punjab","Royal Challengers Bangalore",
                            "Rising Pune Supergiant","Kolkata Knight Riders",
                            "Delhi Daredevils","Gujarat Lions",
                            "Rising Pune Supergiant","Delhi Daredevils","Sunrisers Hyderabad",
                            "Royal Challengers Bangalore",
                            "Sunrisers Hyderabad","Kings XI Punjab","Kolkata Knight Riders",
                            "Mumbai Indians","Sunrisers Hyderabad",
                            "Kings XI Punjab","Kolkata Knight Riders","Rising Pune Supergiant",
                            "Rising Pune Supergiant",
                            "Royal Challengers Bangalore"),
                  team2 = c("Royal Challengers Bangalore","Rising Pune Supergiant",
                            "Kolkata Knight Riders","Kings XI Punjab","Delhi Daredevils",
                            "Sunrisers Hyderabad","Mumbai Indians",
                            "Kings XI Punjab","Rising Pune Supergiant","Mumbai Indians",
                            "Kolkata Knight Riders","Mumbai Indians",
                            "Gujarat Lions","Sunrisers Hyderabad","Kings XI Punjab",
                            "Mumbai Indians","Royal Challengers Bangalore",
                            "Kolkata Knight Riders","Kings XI Punjab","Gujarat Lions",
                            "Delhi Daredevils","Mumbai Indians",
                            "Gujarat Lions","Delhi Daredevils","Rising Pune Supergiant",
                            "Gujarat Lions","Royal Challengers Bangalore",
                            "Mumbai Indians","Kolkata Knight Riders","Gujarat Lions"),
            toss_winner = c("Royal Challengers Bangalore","Rising Pune Supergiant",
                            "Kolkata Knight Riders","Kings XI Punjab",
                            "Royal Challengers Bangalore","Sunrisers Hyderabad","Mumbai Indians",
                            "Royal Challengers Bangalore","Rising Pune Supergiant",
                            "Mumbai Indians","Kolkata Knight Riders",
                            "Mumbai Indians","Gujarat Lions","Sunrisers Hyderabad",
                            "Delhi Daredevils","Mumbai Indians",
                            "Royal Challengers Bangalore","Delhi Daredevils","Kings XI Punjab",
                            "Gujarat Lions","Sunrisers Hyderabad",
                            "Mumbai Indians","Gujarat Lions","Delhi Daredevils",
                            "Rising Pune Supergiant","Gujarat Lions",
                            "Royal Challengers Bangalore","Mumbai Indians",
                            "Kolkata Knight Riders","Gujarat Lions"),
          toss_decision = c("field","field",
                            "field","field","bat","field","field","bat",
                            "field","field","field","field","field","field",
                            "bat","field","field","bat","field","field","bat",
                            "field","field","field","field","field",
                            "field","field","field","field"),
                 result = c("normal","normal",
                            "normal","normal","normal","normal","normal",
                            "normal","normal","normal","normal","normal",
                            "normal","normal","normal","normal","normal",
                            "normal","normal","normal","normal","normal","normal",
                            "normal","normal","normal","normal","normal",
                            "normal","normal"),
             dl_applied = c(0L,0L,0L,0L,0L,
                            0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
                            0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L),
                 winner = c("Sunrisers Hyderabad",
                            "Rising Pune Supergiant","Kolkata Knight Riders",
                            "Kings XI Punjab","Royal Challengers Bangalore",
                            "Sunrisers Hyderabad","Mumbai Indians",
                            "Kings XI Punjab","Delhi Daredevils","Mumbai Indians",
                            "Kolkata Knight Riders","Mumbai Indians","Gujarat Lions",
                            "Kolkata Knight Riders","Delhi Daredevils",
                            "Mumbai Indians","Rising Pune Supergiant",
                            "Kolkata Knight Riders","Sunrisers Hyderabad",
                            "Royal Challengers Bangalore","Sunrisers Hyderabad","Mumbai Indians",
                            "Gujarat Lions","Mumbai Indians",
                            "Rising Pune Supergiant","Kings XI Punjab",
                            "Kolkata Knight Riders","Rising Pune Supergiant","Kolkata Knight Riders",
                            "Gujarat Lions"),
            win_by_runs = c(35L,0L,0L,0L,15L,
                            0L,0L,0L,97L,0L,0L,0L,0L,17L,51L,0L,27L,
                            0L,5L,21L,15L,0L,0L,14L,0L,26L,82L,3L,0L,
                            0L),
         win_by_wickets = c(0L,7L,10L,6L,0L,
                            9L,4L,8L,0L,4L,8L,4L,7L,0L,0L,6L,0L,4L,
                            0L,0L,0L,8L,4L,0L,6L,0L,0L,0L,7L,7L)
     )
ui <- fluidPage(
  titlePanel("Plotly - dateRangeInput"),
  sidebarLayout(
    sidebarPanel(
      dateRangeInput(inputId = "date",label = "Date",
                 start = Sys.Date() - 28, end = Sys.Date() + 2,
                 format = "mm/dd/yyyy",
                 separator = "TO")),
    mainPanel(
      fluidPage(plotlyOutput("p1"))
    )
  )
)

server<-function(input,output,session){
  
observe({
    updateDateRangeInput(
      session,
      inputId = "date"
    )
  })
  
output$p1<-renderPlotly({
  a <- as.Date(re$date) 

  p<-plot_ly(x=(re$city),y=re$win_by_wickets,type="bar") %>%  
  filter(a, a >= as.Date(input$date[1]) & a<= as.Date(input$date[2]))
  p
  
})
}
shinyApp(ui,server)

标签: r

解决方案


我已经尝试过了,但它会引发错误......

library(shiny)
library(plotly)
library(dplyr)
library(readr)
library(shinydashboard)
library(shinythemes)
library(dashboardthemes)
library(shinyWidgets)
# total<-read.csv("c:/users/dell/desktop/ssss.csv")
total<-tibble::tribble(
         ~Order.ID, ~Order_Date, ~CustomerName,              ~State,                ~City, ~sales,
         "B-25601",  "4/5/2017",      "Bharat",           "Gujarat",          "Ahmedabad",  1275L,
         "B-25602",  "4/6/2017",       "Pearl",       "Maharashtra",               "Pune",    66L,
         "B-25603",  "4/7/2017",       "Jahan",    "Madhya Pradesh",             "Bhopal",     8L,
         "B-25604",  "4/8/2017",      "Divsha",         "Rajasthan",             "Jaipur",    80L,
         "B-25605",  "4/8/2017",     "Kasheen",       "West Bengal",            "Kolkata",   168L,
         "B-25606",  "4/9/2017",       "Hazel",         "Karnataka",          "Bangalore",   424L,
         "B-25607",  "4/9/2017",    "Sonakshi", "Jammu and Kashmir",            "Kashmir",  2617L,
         "B-25608", "4/10/2017",     "Aarushi",        "Tamil Nadu",            "Chennai",   561L,
         "B-25609", "4/11/2017",      "Jitesh",     "Uttar Pradesh",            "Lucknow",   119L,
         "B-25610", "4/12/2017",      "Yogesh",             "Bihar",              "Patna",  1355L,
         "B-25611", "4/13/2017",       "Anita",            "Kerala", "Thiruvananthapuram",    24L,
         "B-25612", "4/14/2017",   "Shrichand",            "Punjab",         "Chandigarh",   193L,
         "B-25613", "4/14/2017",      "Mukesh",           "Haryana",         "Chandigarh",   180L,
         "B-25614", "4/15/2017",     "Vandana",  "Himachal Pradesh",              "Simla",   116L,
         "B-25615", "4/15/2017",      "Bhavna",            "Sikkim",            "Gangtok",   107L,
         "B-25616", "4/16/2017",       "Kanak",               "Goa",                "Goa",    12L,
         "B-25617", "4/16/2017",       "Sagar",          "Nagaland",             "Kohima",    38L,
         "B-25618", "4/17/2017",       "Manju",    "Andhra Pradesh",          "Hyderabad",    65L,
         "B-25619", "4/17/2017",      "Ramesh",           "Gujarat",          "Ahmedabad",   157L,
         "B-25620", "4/18/2017",      "Sarita",       "Maharashtra",               "Pune",    75L,
         "B-25621", "4/19/2017",      "Deepak",    "Madhya Pradesh",             "Bhopal",    87L,
         "B-25622", "4/20/2017",     "Monisha",         "Rajasthan",             "Jaipur",    50L,
         "B-25623", "4/21/2017",      "Atharv",       "West Bengal",            "Kolkata",  1364L,
         "B-25624", "4/22/2017",        "Vini",         "Karnataka",          "Bangalore",   476L,
         "B-25625", "4/22/2017",       "Pinky", "Jammu and Kashmir",            "Kashmir",   257L,
         "B-25626", "4/23/2017",      "Bhishm",       "Maharashtra",             "Mumbai",   856L,
         "B-25627", "4/23/2017",      "Hitika",    "Madhya Pradesh",             "Indore",   485L,
         "B-25628", "4/24/2017",       "Pooja",             "Bihar",              "Patna",    25L,
         "B-25629", "4/26/2017",      "Hemant",            "Kerala", "Thiruvananthapuram",  1076L,
         "B-25630", "4/27/2017",       "Sahil",            "Punjab",         "Chandigarh",   107L,
         "B-25631", "4/28/2017",        "Ritu",           "Haryana",         "Chandigarh",    68L,
         "B-25632", "4/28/2017",      "Manish",  "Himachal Pradesh",              "Simla",   781L,
         "B-25633", "4/29/2017",        "Amit",            "Sikkim",            "Gangtok",    43L,
         "B-25634", "4/29/2017",      "Sanjay",               "Goa",                "Goa",    30L,
         "B-25635", "4/30/2017",       "Nidhi",          "Nagaland",             "Kohima",   160L,
         "B-25636", "4/30/2017",       "Nishi",       "Maharashtra",             "Mumbai",   259L,
         "B-25637",  "5/1/2017",       "Ashmi",    "Madhya Pradesh",             "Indore",  1603L,
         "B-25638",  "5/1/2017",       "Parth",       "Maharashtra",               "Pune",   494L,
         "B-25639",  "5/2/2017",       "Lisha",    "Madhya Pradesh",             "Bhopal",    98L,
         "B-25640",  "5/3/2017",     "Paridhi",         "Rajasthan",             "Jaipur",    68L,
         "B-25641",  "5/4/2017",     "Parishi",       "West Bengal",            "Kolkata",    42L,
         "B-25642",  "5/5/2017",        "Ajay",         "Karnataka",          "Bangalore",   116L,
         "B-25643",  "5/6/2017",       "Kirti", "Jammu and Kashmir",            "Kashmir",    22L,
         "B-25644",  "5/6/2017",      "Mayank",       "Maharashtra",             "Mumbai",    14L,
         "B-25645",  "5/7/2017",      "Yaanvi",    "Madhya Pradesh",             "Indore",   305L,
         "B-25646",  "5/7/2017",       "Sonal",             "Bihar",              "Patna",   362L,
         "B-25647",  "5/8/2017",      "Sharda",            "Kerala", "Thiruvananthapuram",    12L,
         "B-25648",  "5/9/2017",      "Aditya",            "Punjab",         "Chandigarh",   353L,
         "B-25649", "5/10/2017",      "Rachna",           "Haryana",         "Chandigarh",   193L,
         "B-25650", "5/11/2017",      "Chirag",       "Maharashtra",             "Mumbai",   233L,
         "B-25651", "5/12/2017",      "Anurag",    "Madhya Pradesh",             "Indore",   228L,
         "B-25652", "5/13/2017",     "Tushina",               "Goa",                "Goa",   333L,
         "B-25653", "5/13/2017",       "Farah",          "Nagaland",             "Kohima",   534L,
         "B-25654", "5/14/2017",       "Sabah",       "Maharashtra",             "Mumbai",    53L,
         "B-25655", "5/14/2017",        "Nida",    "Madhya Pradesh",             "Indore",   158L,
         "B-25656", "5/16/2017",    "Priyanka",       "Maharashtra",               "Pune",   149L,
         "B-25657", "5/17/2017",      "Tulika",    "Madhya Pradesh",             "Bhopal",   105L,
         "B-25658", "5/19/2017",     "Shefali",         "Rajasthan",             "Jaipur",    26L,
         "B-25659", "5/21/2017",   "Sanskriti",       "West Bengal",            "Kolkata",    97L,
         "B-25660", "4/18/2008",      "Shruti",         "Karnataka",          "Bangalore",    59L,
         "B-25661", "4/19/2008",  "Subhashree", "Jammu and Kashmir",            "Kashmir",   635L,
         "B-25662", "4/19/2008",       "Sweta",       "Maharashtra",             "Mumbai",    46L,
         "B-25663", "4/20/2008",  "Pournamasi",    "Madhya Pradesh",             "Indore",  1103L,
         "B-25664", "4/20/2008", "Pratyusmita",             "Bihar",              "Patna",    55L,
         "B-25665", "4/21/2008",   "Chayanika",            "Kerala", "Thiruvananthapuram",    45L,
         "B-25666", "4/22/2008",       "Tanvi",            "Punjab",         "Chandigarh",    24L,
         "B-25667", "4/23/2008",      "Anjali",           "Haryana",         "Chandigarh",    35L,
         "B-25668", "4/24/2008",        "Rhea",  "Himachal Pradesh",              "Simla",  1560L,
         "B-25669", "4/25/2008",      "Piyali",            "Sikkim",            "Gangtok",   133L,
         "B-25670", "4/26/2008",     "Charika",               "Goa",                "Goa",   114L,
         "B-25671", "4/26/2008",      "Mitali",          "Nagaland",             "Kohima",   143L,
         "B-25672", "4/27/2008",    "Akanksha",    "Andhra Pradesh",          "Hyderabad",    40L,
         "B-25673", "4/27/2008",     "Arsheen",           "Gujarat",          "Ahmedabad",    34L,
         "B-25674", "4/28/2008",      "Mahima",       "Maharashtra",               "Pune",    42L,
         "B-25675", "4/29/2008",      "Shreya",    "Madhya Pradesh",             "Bhopal",    89L,
         "B-25676", "4/30/2008",     "Chandni",         "Rajasthan",             "Jaipur",    19L,
         "B-25677",  "5/1/2008",        "Ekta",       "West Bengal",            "Kolkata",   249L,
         "B-25678",  "5/1/2008",     "Bathina",         "Karnataka",          "Bangalore",   711L,
         "B-25679",  "5/2/2008",        "Avni",       "Maharashtra",             "Mumbai",   496L,
         "B-25680", "5/25/2008",     "Aayushi",    "Madhya Pradesh",             "Indore",   389L,
         "B-25681",  "5/3/2008",      "Bhawna",    "Madhya Pradesh",             "Indore",    40L,
         "B-25682",  "5/4/2008",     "Krutika",             "Bihar",              "Patna",    23L,
         "B-25683",  "5/4/2008",      "Shreya",            "Kerala", "Thiruvananthapuram",   382L,
         "B-25684",  "5/5/2008",    "Samiksha",       "Maharashtra",             "Mumbai",   637L,
         "B-25685",  "5/6/2008",     "Sheetal",    "Madhya Pradesh",             "Indore",   117L,
         "B-25686",  "5/7/2008",       "Pooja",  "Himachal Pradesh",              "Simla",   182L,
         "B-25687",  "5/8/2008",      "Sanjna",       "Maharashtra",             "Mumbai",   880L,
         "B-25688",  "5/8/2008",      "Swetha",    "Madhya Pradesh",             "Indore",   154L,
         "B-25689",  "5/9/2008", "Bhaggyasree",       "Maharashtra",             "Mumbai",   816L,
         "B-25690", "5/28/2008",      "Gunjan",    "Madhya Pradesh",             "Indore",  1629L,
         "B-25691", "5/10/2008",     "Akancha",       "Maharashtra",             "Mumbai",    68L,
         "B-25692", "5/11/2008",      "Rashmi",    "Madhya Pradesh",             "Indore",   314L,
         "B-25693", "5/11/2008",       "Parna",    "Madhya Pradesh",             "Bhopal",   122L,
         "B-25694", "5/12/2008",  "Subhasmita",         "Rajasthan",             "Jaipur",    22L,
         "B-25695", "5/13/2008",      "Suhani",       "West Bengal",            "Kolkata",   434L,
         "B-25696", "5/14/2008",      "Noopur",         "Karnataka",          "Bangalore",  1061L,
         "B-25697", "5/28/2008",       "Vijay", "Jammu and Kashmir",            "Kashmir",    50L,
         "B-25698", "5/15/2008",      "Amisha",        "Tamil Nadu",            "Chennai",    37L
         )

 
ui <- dashboardPage(
  dashboardHeader(title="dashboard"),
  dashboardSidebar(
    sidebarMenu(
      dateRangeInput(inputId = "date",
                 label = 'Date range',
                 start = Sys.Date() - 28, end = Sys.Date() + 2,
                 format = "mm/dd/yyyy",
                 separator = "TO"),
    selectInput(inputId = "p1",label="filter drop down",choices = c(unique(total$State)))
      )),
  dashboardBody(
    shinyDashboardThemes(
      theme = "onenote"
    ),
  fluidPage(
  plotlyOutput("state", height = 200),
  plotlyOutput("city", height = 200),
  plotlyOutput("customer", height = 200),
  plotlyOutput("sales", height = 300),
  dataTableOutput("datatable"))))

axis_titles <- . %>%
  layout(
    xaxis = list(title = ""),
    yaxis = list(title = "Sales"))

server <- function(input, output, session) {
  
  State <- reactiveVal()
  City <- reactiveVal()
  CustomerName<- reactiveVal()
  Order_Date <- reactiveVal()
  
  observeEvent(event_data("plotly_click", source = "State"), {
    State(event_data("plotly_click", source = "State")$x)
    City(NULL)
    Order_Date(NULL)
  })
  
 observeEvent(event_data("plotly_click", source = "City"), {
    City(event_data("plotly_click", source = "City")$x)
    CustomerName(NULL)
    Order_Date(NULL)
  })
 
  observeEvent(event_data("plotly_click", source = "CustomerName"), {
    CustomerName(event_data("plotly_click", source = "CustomerName")$x)
    Order_Date(NULL)
  })
  
  observeEvent(event_data("plotly_click", source = "Order_Date"), {
    Order_Date(event_data("plotly_click", source = "Order_Date")$x)
  })
  
  ## filter the data
  # filtered_data <- reactive({
  #   dplyr::filter(total, total$State == input$p1)
  # })
  
  output$state <- renderPlotly({
  
    total$Order_Date<-as.Date(total$Order_Date,format = "%m/%d/%Y")
     re_filt <- filter(total,
           between(total$Order_Date,
                   as.Date(input$date[1]) ,
                   as.Date(input$date[2])))
     total%>%
      count(State, wt = sales) %>%
      plot_ly(data = re_filt,x = ~State, y = ~n, source = "State") %>%
      axis_titles() %>% 
      layout(title = "State")
  })

   output$city <- renderPlotly({
    if (is.null(State())) return(NULL)
    total%>%
      filter( State %in% State()) %>%
      count(City, wt = sales) %>%
      plot_ly(x = ~City, y = ~n, source = "City") %>%
      axis_titles() %>%
      layout(title = State())
  })
 
  
  output$customer <- renderPlotly({
    if (is.null(City())) return(NULL)
    total%>%
      filter(City %in% City()) %>%
      count(CustomerName, wt = sales) %>%
      plot_ly(x = ~CustomerName, y = ~n, source = "CustomerName") %>%
      axis_titles() %>%
      layout(title = City())
  })
  
  output$sales <- renderPlotly({
    if (is.null(CustomerName())) return(NULL)
    
    total %>%
      filter(CustomerName %in% CustomerName()) %>%
      count(Order_Date, wt = sales) %>%
      plot_ly(x = ~Order_Date, y = ~n, source = "Order_Date") %>%
      add_lines() %>%
      axis_titles() %>%
      layout(title = paste(CustomerName(), "sales over time"))
  })
  
  output$datatable <- renderDataTable({
    if (is.null(Order_Date())) return(NULL)
    
    total %>%
      filter(
        CustomerName %in% CustomerName(),
        as.Date(Order_Date) %in% as.Date(Order_Date())
      )
  })
  
}
runApp(list(ui = ui, server = server), launch.browser = TRUE)

我也尝试过你的方法,但它会抛出同样的错误,如果你能告诉我我在这里做错了什么以便我可以纠正它,那将是非常有帮助的。


推荐阅读