Filter Slider and Shared Data with CrossTalk in Shiny App does not work in UI function

41 Views Asked by At

I'm working on a Shiny App that allows users to upload a CSV file and then use a filter slider for date selection. However, I'm encountering an issue with the filter slider—it keeps resetting and doesn't allow me to slide.

I have two questions:

  1. Fixing Filter Slider in Server Function: Is there a way to fix this problem with the filter slider when I try to place it in the server function?

  2. Passing Data from Server to UI: The filter slider works properly when placed in the UI function. In this case, is there a way to pass data from the server function to the UI function? I want to update the shared data part of the filter slider to reflect the updated uploaded file.

I appreciate any help or suggestions. You can experiment with the code below, and download the CSV file through this link.

I already tried multiple ways to tackle this challenge including using global variable, reactive value and putting the filter slider inside the UI and Server function. It turns out the problem lies on putting it in the Server function no matter if the variable of the shared data is reactive or not.

# Your R code here
# Include the relevant parts of your code, such as libraries, functions, and UI/server definitions.
# Be concise and focus on the specific issue.
library(shiny)


process_function <- function(data) {
  data$format_date <- as.Date(data$Date, "%Y-%m-%d")
  
  data$info <- ifelse(
    test = (data$Date == "" | is.na(data$Date)),
    yes = paste0("Voyage: ", data$Voyage),
    no = paste0("Voyage: ", data$Voyage, "<br>Date: ", data$Date)
  )
  
  data$format_date <- as.Date(data$Date, "%Y-%m-%d")
  data$coords <- paste0("Lon: ", round(data$Lon, 4), "<br>Lat: ", round(data$Lat, 4))
  data$info <- paste0(data$info, "<br>", data$coords)
  
  return(data)
}


ui <- fluidPage(
  fileInput("file", "Choose a file"),
  conditionalPanel(
    condition = "output.fileUploaded",
    uiOutput("filter")
  )
)

server <- function(input, output, session) {
  output$fileUploaded <- reactive({
    !is.null(input$file)
  })
  
  shared_data <- reactiveVal(NULL)
  
  minDate <- reactiveVal(1)
  maxDate <- reactiveVal(10)
  
  observeEvent(input$file, {
    req(input$file)
    
    process_data <- process_function(read.csv(input$file$datapath))
    min_date_ <- round(min(as.numeric(str_extract(process_data$Date, "^\\d\\d\\d\\d")), na.rm = TRUE), -1)
    max_date_ <- round(max(as.numeric(str_extract(process_data$Date, "^\\d\\d\\d\\d")), na.rm = TRUE) + 10, -1)
    min_date_ <- as.Date(paste0(min_date_, "-01-02"))
    max_date_ <- as.Date(paste0(max_date_, "-12-31"))
    
    minDate(min_date_)
    maxDate(max_date_)

    shared_data(SharedData$new(process_data))
  })
  
  output$filter <- renderUI({
    if (!is.null(shared_data())) {
      filter_slider(
        id = "slider_ap",
        label = "Date",
        min = minDate(),
        max = maxDate(),
        sharedData = shared_data(),
        column = ~format_date
      )
    }
  })
  
  outputOptions(output, "fileUploaded", suspendWhenHidden = FALSE)
}

shinyApp(ui = ui, server = server)


0

There are 0 best solutions below