Problem with reset button that I created in shiny code

In the shiny code below I created a reset button, but it just deletes the graph that is generated on the screen and doesn't do anything for the chosen date and category. So I would like some help with this: ie when I press reset, I would not want any date/category to be selected ie the fields would be left empty. Is it possible to do that?

Executable code below:

     library(shiny)
     library(shinythemes)
     library(dplyr)
     library(tidyverse)
     library(lubridate)
     library(stringr)

function.test<-function(){
  
  df1 <- structure(
    list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
         date2 = c("2021-07-01","2021-07-01","2021-07-02","2021-07-03"),
         Category = c("FDE","ABC","FDE","FDE"),
         Week= c("Friday","Monday","Wednesday","Wednesday"),
         DR1 = c(4,1,1,6),
         DR01 = c(4,1,1,3), DR02= c(4,2,2,4),DR03= c(9,5,5,2),
         DR04 = c(5,4,3,4),DR05 = c(5,4,2,4),
         DR06 = c(2,4,2,4),DR07 = c(2,5,4,2),
         DR08 = c(3,4,2,4),DR09 = c(2,3,4,2)),
    class = "data.frame", row.names = c(NA, -4L))
  
  
  return(df1)
}

f1 <- function(df1, dmda, CategoryChosse) {
  
  x<-df1 %>% select(starts_with("DR0"))
  
  x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
  PV<-select(x, date2,Week, Category, DR1, ends_with("PV"))
  
  med<-PV %>%
    group_by(Category,Week) %>%
    summarize(across(ends_with("PV"), median))
  
  SPV<-df1%>%
    inner_join(med, by = c('Category', 'Week')) %>%
    mutate(across(matches("^DR0\\d+$"), ~.x + 
                    get(paste0(cur_column(), '_PV')),
                  .names = '{col}_{col}_PV')) %>%
    select(date1:Category, DR01_DR01_PV:last_col())
  
  SPV<-data.frame(SPV)
  
  mat1 <- df1 %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(starts_with("DR0")) %>%
    pivot_longer(cols = everything()) %>%
    arrange(desc(row_number())) %>%
    mutate(cs = cumsum(value)) %>%
    filter(cs == 0) %>%
    pull(name)
  
  (dropnames <- paste0(mat1,"_",mat1, "_PV"))
  
  SPV <- SPV %>%
    filter(date2 == dmda, Category == CategoryChosse) %>%
    select(-any_of(dropnames))
  
  datas<-SPV %>%
    filter(date2 == ymd(dmda)) %>%
    group_by(Category) %>%
    summarize(across(starts_with("DR0"), sum)) %>%
    pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
    mutate(name = readr::parse_number(name))
  colnames(datas)[-1]<-c("Days","Numbers")
  
  if(as.Date(dmda) < min(as.Date(df1$date1))){
    datas <- datas %>% 
      group_by(Category) %>% 
      slice(1:max(Days)+1) %>%
      ungroup
  }else{
    datas <- datas %>% 
      group_by(Category) %>% 
      slice((as.Date(dmda) - min(as.Date(df1$date1) [
        df1$Category == first(Category)])):max(Days)+1) %>%
      ungroup
  }
  
  plot(Numbers ~ Days,  xlim= c(0,45), ylim= c(0,30),
       xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))
  
  model <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
  
  new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
  new.data <- rbind(0, new.data)
  lines(new.data$Days,predict(model,newdata = new.data),lwd=2)
  coef<-coef(model)[2]
  points(0, coef, col="red",pch=19,cex = 2,xpd=TRUE)
  text(.99,coef + 1,max(0, round(coef,1)), cex=1.1,pos=4,offset =1,col="black")
  
}


ui <- fluidPage(
  
  ui <- shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                          br(),
                          
                          tabPanel("",
                                   sidebarLayout(
                                     sidebarPanel(
                                       
                                       uiOutput("date"),
                                       uiOutput("mycode"),
                                       actionButton("reset", "Reset"),
                                       
                                       br(),
                                       
                                       
                                     ),
                                     
                                     mainPanel(
                                       tabsetPanel(
                                         tabPanel("", plotOutput("graph",width = "100%", height = "600")
                                         )
                                       ),
                                     ))
                          )))


server <- function(input, output,session) {
  
  data <- reactive(function.test())
  
  output$date <- renderUI({
    req(data())
    all_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day")
    disabled <- as.Date(setdiff(all_dates, as.Date(data()$date2)), origin = "1970-01-01")
    dateInput(input = "date2", 
              label = h4("Data"),
              min = min(data()$date2),
              max = max(data()$date2),
              value = min(data()$date2),
              format = "dd-mm-yyyy",
              datesdisabled = disabled)
    
  })
  
  output$mycode <- renderUI({
    req(input$date2)
    df1 <- data()
    df2 <- df1[as.Date(df1$date2) %in% input$date2,]
    selectInput("code", label = h4("Category"),choices=unique(df2$Category))
  })
  
  output$graph <- renderPlot({
    req(input$date2,input$code)
    f1(data(),as.character(input$date2),as.character(input$code))
  })
  
  observeEvent(input$reset, {
    output$graph <- renderPlot(plot.new())
    
    output$date <- renderUI({
      req(data())
      all_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day")
      disabled <- as.Date(setdiff(all_dates, as.Date(data()$date2)), origin = "1970-01-01")
      dateInput(input = "date2", 
                label = h4("Data"),
                min = min(data()$date2),
                max = max(data()$date2),
                value = min(data()$date2),
                format = "dd-mm-yyyy",
                datesdisabled = disabled)
      
    })
  })
  
}

shinyApp(ui = ui, server = server)

Rather than having reset remove your render for the graph, have it reset the inputs that are the input$date2 and input$code.

probably

 output$date <- renderUI({
 input$reset # adding a dependency on the reset, so it rerurns when reset is pressed 
      req(data())

is sufficient.

Thanks for the answer @nirgrahamuk ! Could you insert how my observeEvent(input$reset, { would look like?

I gave you a code insert... my idea doesnt involve writing an observeEvent to watch input$reset, its about adding input$reset to the dependencies of output$date renderUI, which I showed how to do.

I understood better now, thanks @nirgrahamuk ! I have an easy question that you might help: when I run shiny, it automatically generates the graph. Is it possible to get started without generating this graph? Only generate, after choosing a date on the calendar?

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.