CalCOFI / capstone

Capstone project for CCDSP fellowship, where we will store data and code
1 stars 2 forks source link

Map Animation #75

Closed PK0207 closed 2 years ago

PK0207 commented 2 years ago

We want to be able to input a start and end date, and animate through the sampling patterns of the years and quarters on the basemap.

PK0207 commented 2 years ago

Currently, we have a way to update the slider range with the dateRange input. Was trying to find a way to find the corresponding quarter for a date by using this: quarter(bottle$date)[which(strptime(as.Date(input$times), format = "%d%y%m") %in% strptime(bottle$date, format = "%d%y%m"))]

But the output of that is only ever 2. I think I am misunderstanding the dataset here. The slider range updates just fine, and I can read out the slider value fine as well. image

PK0207 commented 2 years ago

I believe I also am misunderstanding how the map updates normally. The map doesn't update when the quarter and year are changed: image image

PK0207 commented 2 years ago

@bbest Would you mind taking a look at my code here? No rush, would just be helpful. https://github.com/PK0207/CalCOFI22/blob/f9152f216acc5230d14d638ec20c11b8b58e02d5/scripts/shiny/app.R#L236-L247

bbest commented 2 years ago

Hi @PK0207,

You can use lubridate::quarter() to get the quarter of a date.

library(dplyr)

tibble(
  date = sprintf("2022-%02d-01", 1:12) %>% as.Date()) %>% 
  mutate(
    quarter = lubridate::quarter(date))

which results in:

# A tibble: 12 × 2
   date       quarter
   <date>       <int>
 1 2022-01-01       1
 2 2022-02-01       1
 3 2022-03-01       1
 4 2022-04-01       2
 5 2022-05-01       2
 6 2022-06-01       2
 7 2022-07-01       3
 8 2022-08-01       3
 9 2022-09-01       3
10 2022-10-01       4
11 2022-11-01       4
12 2022-12-01       4
bbest commented 2 years ago

You might want to make Quarter a SelectInput() with options 1:4

PK0207 commented 2 years ago

Changing quarter to select input fixed the year updating so that works, but the quarter updating still doesn't. I added a function called get_quarter to page_functions.R: https://github.com/PK0207/CalCOFI22/blob/f773be30ff2f80ebe5a9102e00c6081db2082c67/scripts/shiny/spatial-page/page_functions.R#L235-L242 and call it in app.R like so: https://github.com/PK0207/CalCOFI22/blob/fa07cdae42189d2d2499d062977bcfba8d099698/scripts/shiny/app.R#L237-L239

But the quarter still won't update. I thought it might be a type issue but the output class is numeric? Thanks so much for your help.

PK0207 commented 2 years ago

Looking at the reactive log, it looks like the input$times never gets to the quarter_val updateSelectInput: image

bbest commented 2 years ago

Hi @PK0207,

Sorry, I wasn't understanding your original problem.

I think all you need is:

  observe({
    updateSelectInput(session, "qr2", selected = quarter(input$times))
  })

Your get_quarter() function doesn't make sense to me. It doesn't even appear to use the input argument times, so would naturally produce the same answer. Besides the quarter is determined by the date alone, so you shouldn't need to look it up in a table (unless you need to verify its existence amongst observations, but that's a separate unrelated matter to updating the display of Quarter to coincide with the times animation slider date).

PK0207 commented 2 years ago

Using your tibble argument worked! I'm not quite sure why these fixes worked but I very much appreciate it.

bbest commented 2 years ago

What does my "tibble argument" mean? I simply recommended swapping your custom get_quarter() function with lubridate::quarter(). The get_quarter() function would always return the same value and never use anything that varied, i.e. input$times because its one input argument time never gets used inside the function. Hopefully this explains why it would now work.

PK0207 commented 2 years ago

sorry, I meant the piece of code you recommended, poor wording on my part. I see now where I made my mistake, a silly mistake. Sorry for the misunderstanding! Thanks for the help.