How to filter a data table by range of months and for each year?

I have a data table with a monthly date variable and, obviously, my variable which contains the values. I would like to filter by range of month (January-June) for each year. For example, the user can select the month range and the years they would like info to be displayed.

An idea of what I would like to achieve is in the following image:

Have you attempted to write any code in this direction ?

1 Like

I think we need to see your code and some sample data. See
FAQ Asking Questions

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here between
```

```

1 Like

@jrkrideau @nirgrahamuk Thank you both for your reply. This is the data I have. I would like to mention that the date variable is monthly and from 2009-2023.

I also include the code I have been trying.

This is in the ui:

,dashboardBody( 
    tabItems(
      tabItem(
        tabName = "tab3", h1("Exportaciones Petroleras"),p(""),fluidRow(bs4Card(
        column(
          title = "",width = 6,align = "center",
          highchartOutput("plot3", height = 580, width = 472)
        ),
        column(
          title = "",width = 6,align = "left",selectizeInput("man",
"Año",
c("All",
unique(as.character(dftabla33$Fecha))),multiple = T),DT::dataTableOutput("table3")
        )))))
  ))

And this in the server:

output$table3 <- DT::renderDataTable(DT::datatable({
      data <- dftabla33
     
      
      if (!is.null(input$man)) {
        if ("All" %in% input$man && length(input$man) == 1) {
          # If only "All" is selected, show all data
          data <- dftabla33
        } else if (length(input$man) > 0) {
          # If other options are selected, filter the data
          data <- data[data$Fecha %in% input$man,]
        }
      }
      data
      
    }))  

structure(list(Fecha = structure(c(14245, 14276, 14304, 14335,
14365, 14396, 14426, 14457, 14488, 14518, 14549, 14579, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156,
15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431,
15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706,
15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979,
16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252,
16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526,
16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801,
16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075,
17106, 17136, 17167, 17198, 17226, 17257), class = "Date"), variable = c("Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)",
"Tasa mensual (eje der.)", "Tasa mensual (eje der.)", "Tasa mensual (eje der.)"
), datos = c(-3.59159272043931, -13.2195046486671, 30.1367732369871,
27.6042225637375, 14.6490337735206, 13.4787549430764, 1.24852165401856,
24.7025963751922, -21.1572081382421, 13.9849474738433, -8.23945686386968,
25.6212722575873, -10.2132589747505, -13.2759527728943, 32.3872453985209,
8.03447950939424, -29.06759289676, 30.8938185574846, -13.4239731851839,
-3.27961742280718, 7.75076669862085, 18.2494172789919, -6.68411433611219,
17.7351725992179, -8.78275423251661, 4.16282127011993, 29.5506144084733,
-18.0160337214221, 17.8437586630656, -5.91942389666771, 5.47977932818071,
-12.7117519275677, 5.35516933145779, -4.91892005552276, 5.95495730957187,
-2.25479844487607, 28.6362730173046, -10.8578536701659, -2.99635367366634,
9.4461682099884, -0.641183622741748, -22.8611997920537, 0.153125274010835,
15.1859005981159, 5.28359717413163, -16.0449772086209, -11.8358005408786,
21.6722375533231, -0.770633919250552, 11.5526071092695, 0.892343177342303,
-15.1845510042873, 4.65800391840776, 2.69494477616599, 20.3066626343085,
-5.72710204386094, 6.56808388602235, -12.7031691769524, -11.6104155649502,
14.0479876551997, -9.0047015485183, 6.41355150065031, 15.1208141918232,
-33.6266956540816, 72.6850251121862, -14.7169772137174, -9.66294774289257,
7.85543105123967, -17.8714574998128, -1.60772817473499, -6.88667538846679,
-34.15822987071, -2.23355318475443, -14.1952738476415, 17.6990632053981,
-5.18638026916872, 34.2843565934531, -12.1944984262373, -7.14717816691006,
-11.6164627428431, -11.7032214375737, -12.2626463087285, -12.5411376110884,
-12.5013195781876, -12.9974164393788, -13.6611440515084, 40.5646535311779,
9.03034403185343, 38.681642945405, 4.298982836203, -5.05049182489092,
-11.8268170108545, 4.66986597060912, 12.4426989984289, -10.5577147781081,
14.0975994257872, 1.33944746559094, -0.134691518914209, -6.36733926573526,
7.61897949665729)), row.names = c(NA, 100L), class = "data.frame")

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.