I am designing an R Shiny app, and I have been able to display the selected file name back to the radio button. However, I have tried many methods, including getting help from an AI bot, but it seems to not work well. I want to be able to visualize again after clicking on the radio button with the file name, without having to select the file again from the beginning. Here is my server.R code:
library(shiny)
library(ggplot2)
library(dplyr)
library(readr)
library(lubridate)
selectedFileNames <- reactiveVal(character(0))
shinyServer(function(input, output, session) {
data <- reactive({
req(input$file)
selectedFileName <- input$file$name
if (!(selectedFileName %in% selectedFileNames())) {
selectedFileNames(c(selectedFileNames(), selectedFileName))
}
read.csv(input$file$datapath, stringsAsFactors = FALSE)
})
observe({
updateRadioButtons(session, "selectedOption", choices = c("", selectedFileNames()), selected = input$selectedOption)
})
filteredData <- reactive({
data_filtered <- data()
date_range <- input$dateRange
if (!is.null(date_range)) {
start_date <- as.POSIXct(date_range[1], format = "%Y-%m-%d", tz = "UTC")
end_date <- as.POSIXct(date_range[2], format = "%Y-%m-%d", tz = "UTC")
data_filtered <- data_filtered %>%
filter(between(as.POSIXct(Start.Time), start_date, end_date))
}
data_filtered
})
output$userSearchPlot <- renderPlot({
ggplot(filteredData(), aes(x = as.Date(Start.Time), fill = factor(User))) +
geom_bar() +
labs(title = "Number of User Searches Over Time",
x = "Date",
y = "Number of Searches",
fill = "User") +
theme_minimal() +
scale_x_date(date_labels = "%Y-%m-%d", date_breaks = "1 day") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
})
output$summaryText <- renderPrint({
summary_data <- filteredData() %>%
group_by(date = as.Date(Start.Time)) %>%
summarise(NumUsers = n_distinct(User), NumSearches = n()) %>%
as.data.frame()
summary_label <- "Summary of User Searches Over Time"
# Thêm tiêu đề cho các cột
colnames(summary_data) <- c("Date", "Number of User", "Number of Searcher")
cat(summary_label, "\n")
print(summary_data)
})
})