使R闪亮记住具有多标签面板的应用程序的输入和输出

ruoxqz4g  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(78)

我目前在一个闪亮的应用程序中有2个tabPanel()。该应用程序允许用户上传Excel文件。上传之后,UI元素以selectizeInput()的形式生成,用户可以在其中选择x和y变量。输出是一个x变量和多个y变量的堆叠plotly()图我在应用程序中有两个选项卡面板,在第一个选项卡面板中,我选择excel文件的sheet 1及其相应的x和y变量。类似地,我为第二个tabPanel选择了sheet 2,并选择了它们的x和y,
我在这里的问题是,我希望Shiny在切换到第二个tabPanel()后记住第一个tabPanel()的输入,反之亦然。目前,当用户在选项卡面板之间切换时,应用程序会刷新到默认状态。它记得在选项卡中选择了哪个工作表,但不记得x和y变量,这里是一个工作示例代码.我还附上了Excel文件作为参考-https://docs.google.com/spreadsheets/d/1QYqg0b-_YK7eUhR78usiM4NspZoj5Rfk/edit?usp=sharing&ouid=100116750636203353695&rtpof=true&sd=true
我该怎么做?

options(shiny.maxRequestSize=30*1024^2)

lib <- c("DT","plotly","shiny","data.table", "readr", "openxlsx", "readxl", "tidyverse")

lapply(lib, require, character.only = TRUE)

plotly_stacked <- function(df, x_colName, cols){
  
  
  DF <- df[, cols] %>%
    tidyr::gather(variable, value, -x_colName ) %>%
    transform(id = as.integer(factor(variable)))
  
  DF$variable<- factor( DF$variable, levels = unique( DF$variable))
  names(DF) <- gsub("\\.", " ", names(DF))
  
  p <- plot_ly(data = DF, x = ~Time , y = ~value, color = ~variable, colors = "Dark2",
               yaxis = ~paste0( "y",sort(id, decreasing = F))
  ) %>%
    
    add_lines() %>%
    layout( 
      legend = list(orientation = "h",   # show entries horizontally
                    xanchor = "center",  # use center of legend as anchor
                    x = 0.5)) %>% 
    plotly::subplot(nrows = length(unique(DF$variable)), shareX = TRUE) 
  
  return(p)
  
}

# ui.R ------------------------------------------------------------

ui <-
  
  fluidPage(
    
    
    titlePanel(title = "test data"),
    fileInput("file", "Choose csv file",
              accept = c(".xlsx", ".xls", ".csv", ".txt")
    ),
    
    conditionalPanel(
      condition = "input.tabsetPanelID  == 'tab1'",
      uiOutput("ui_elements1"),
      uiOutput("sheet_elements1")
    ),
    conditionalPanel(
      condition = "input.tabsetPanelID == 'tab2'",
      uiOutput("ui_elements2"),
      uiOutput("sheet_elements2")
    ),
    
    mainPanel(
      tabsetPanel(id = "tabsetPanelID",
                  tabPanel("Tab1", value = "tab1",
                           plotlyOutput("plot1")
                  ) 
                  ,
                  tabPanel("Tab2", value = "tab2",
                           plotlyOutput("plot2"))
      )  
    ) 
  ) 

# server.R ------------------------------------------------------------
server <- function(input, output,session) {
  
  # ui_elementsUI------------------------------------------------------------
  
  ui_elementsUI <- function(x, y) {
    
    tagList(
      h4("Select X and Y datasets"),
      fluidRow(
        
        column(12,
               selectizeInput(inputId = x, label = "X data", choices = names( data()  ))
        )
      ),
      
      fluidRow(
        
        column(12,
               selectizeInput(inputId = y, label = "Y data", choices = names(data()), multiple = T, selected = names(data())[2])
        )
      )
      
    )
    
  }
  # sheet_elementsUI------------------------------------------------------------
  sheet_elementsUI <- function(sheet){
    
    tagList(
      
      fluidRow(
        
        column(12,
               selectInput(inputId = sheet, label = "Sheet", choices = excel_sheets(input$file$datapath))
               
        )
      )
    ) 
  }
  
  
  # Render sheet elements after file upload ------------------------------------
  
  output$sheet_elements1 <- renderUI({
    req(input$file)
    sheet_elementsUI("sheet1")
  })
  
  
  output$sheet_elements2 <- renderUI({
    req(input$file)
    sheet_elementsUI("sheet2")
  })
  
  
  # Render UI elements after file upload ------------------------------------
  
  
  output$ui_elements1 <- renderUI({
    if (has_file()) {
      
      ui_elementsUI("x1","y1")
    }
  })
  
  
  output$ui_elements2 <- renderUI({
    if (has_file()) {
      ui_elementsUI("x2","y2")
    }
  })
  
  plot_render <- reactive({
    validate(
      need(input$file != "", "Plots will display after choosing the csv file. Make sure all your csv files are in the 'data' folder in the main project tree
             ")
    )
    
    foo <-  function(x, y){
      plotly_stacked (df =  data(), x_colName = input[[x]] ,cols = c(input[[x]], input[[y]] ))
    }
    
  })
  
  output$plot1 <- renderPlotly({
    
    plot_render()("x1", "y1")
    
  })
  output$plot2<- renderPlotly({
    
    plot_render()("x2", "y2")
  })
  
  # Reactive expression to read in uploaded file ----------------------------
  
  data <- reactive({
    req(input$file)
    foo <- function(sheet){
      
      df <- read_excel(input$file$datapath, sheet = input[[sheet]])
      
      return(df)
      
    }
    if(  input$tabsetPanelID  == 'tab1'){
      result <- foo("sheet1")
    } else{
      result <- foo("sheet2")
      
    }
    
    return(result)
    
  })
  
  
  # Reactive expression to check if a file has been uploaded ----------------
  
  
  has_file <- reactive({
    !is.null(input$file) && !is.na(input$file$name)
  })
  
}

shinyApp( ui = ui, server = server)
dba5bblo

dba5bblo1#

用下面的代码替换data <- reactive(...),并分别对tab1tab2使用data1()data2()

foo <- function(sheet){
  df <- read_excel(input$file$datapath, sheet = input[[sheet]])
  return(df)
}

data1 <- reactive({
  req(input$file)
  foo("sheet1")
})

data2 <- reactive({
  req(input$file)
  foo("sheet2")
})

相关问题