R语言 保存路径历史到单选按钮

hmae6n7t  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(102)

我正在设计一个R Shiny应用程序,我已经能够将选定的文件名显示回单选按钮。
然而,我尝试了很多方法,包括从AI bot那里获得帮助,但都没有成功。我希望能够在点击带有文件名的单选按钮后再次可视化数据,而不必从头开始重新选择文件。下面是我的server.R代码:

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"

    colnames(summary_data) <- c("Date", "Number of User", "Number of Searcher")

    cat(summary_label, "\n")
    print(summary_data)
  })
})

字符串

hzbexzde

hzbexzde1#

您可以将上传的数据集存储在一个React式列表(reactiveValues)中,并在用户选择单选按钮时访问此列表。

server <- function(input, output, session) {
  
  selectedFileNames <- reactiveVal(character(0))
  
  Datasets <- reactiveValues()
  
  observeEvent(input$file, {
    selectedFileName <- input$file$name
    
    if (!(selectedFileName %in% selectedFileNames())) {
      selectedFileNames(c(selectedFileNames(), selectedFileName))
    }
    
    dat <- read.csv(input$file$datapath, stringsAsFactors = FALSE)
    Datasets[[selectedFileName]] <- dat
  })
  
  observeEvent(selectedFileNames(), {
    currentFileName <- selectedFileNames()[length(selectedFileNames())]
    updateRadioButtons(
      session, "selectedOption", choices = c("", selectedFileNames()), 
      selected = currentFileName
    )
  })
  
  data <- eventReactive(input$selectedOption, {
    Datasets[[input$selectedOption]]
  }, ignoreInit = TRUE)
  
  ......
  
}

字符串

相关问题