使用R Shiny读取用户指定的Excel工作表不工作

xjreopfe  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(88)

我有一个包含多个工作表的Excel文件。我将readxl包与Shiny一起使用,这样用户就可以指定他们想要打开的工作表并分析该工作表中的相应数据。下面是示例代码。我的问题是,当用户选择第一个工作表以外的任何其他工作表时,它不起作用,UI恢复到第一个工作表。我该如何解决此问题?

options(shiny.maxRequestSize=30*1024^2) # upload file upto 30 MB 
        #Load required packages
        
        library("readxl")
        library("shiny")
        
        
             DataImportUI <- tagList(
                  
                  fileInput("file", "Choose csv file",
                            accept = c(".xlsx", ".xls", ".csv", ".txt")
                  ),
                  uiOutput("ui_elements")
                )
            
        
        
    # ui.R
       
    
         ui <- 
              fluidPage(
                titlePanel(title = "Plant data visualization"),
                
                sidebarLayout(
                  
                  sidebarPanel(
                    
                    DataImportUI
                    
                  ),
                  
                  mainPanel(
                    tabsetPanel(type = "tabs",
                                tabPanel("Data Wrangle", value = 2
                                         
                                ) 
                    ) 
                  ) 
                )
              ) 
        
        
   #server.R
        
       
    
         server <- function(input, output,session) {
         
              data <- reactive({
                
                req(input$file)
                
                
                df <- read_excel(input$file$datapath, sheet = input$sheet)
                
                return(df)
              })
              
         
              
              has_file <- reactive({
                !is.null(input$file) && !is.na(input$file$name)
              })
            
              
              output$ui_elements <- renderUI({
            
                
                if (has_file()) {
                  # Display additional UI elements here
                  
                  
                  tagList(
                    h4("Select X and Y datasets"),
                    
                    fluidRow(
                      
                      column(12,
                             selectInput(inputId = "sheet", label = "Sheet", choices = excel_sheets(input$file$datapath))
                             
                      )
                    )
                    ,
                    
                    
                    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])
                      )
                    ),
                    
                    
                    
                    tags$hr(style="border-color: grey;")
                    
                  )
                }
              })
              
              
            }
        
        shinyApp( ui = ui, server = server)
rwqw0loc

rwqw0loc1#

我认为你有一个循环依赖关系-output$ui_elements依赖于data(),但data()也依赖于output$ui_elements,因为input$sheets
我尝试通过为工作表创建一个新的renderUI来分离依赖关系。以mtcarsiris数据集为例。

library(readxl)
library(shiny)

# Example data
# writexl::write_xlsx(dplyr::lst(mtcars, iris), "temp.xlsx")

DataImportUI <- tagList(
  fileInput("file", "Choose file",
            accept = c(".xlsx", ".xls", ".csv", ".txt")
  ),
  uiOutput("sheet_elements"),
  uiOutput("ui_elements")
)

# ui.R

ui <- 
  fluidPage(
    titlePanel(title = "Plant data visualization"),
    sidebarLayout(
      sidebarPanel(DataImportUI),
      mainPanel(
        tabsetPanel(type = "tabs",
                    tabPanel("Data Wrangle", value = 2
                    ) 
        ) 
      ) 
    )
  ) 

#server.R

server <- function(input, output,session) {
  data <- reactive({
    req(input$sheet)
    df <- read_excel(input$file$datapath, sheet = input$sheet)
    return(df)
  })
  
  output$sheet_elements <- renderUI({
    req(input$file)
    tagList(
      h4("Select X and Y datasets"),
      fluidRow(
        column(12,
               selectInput(inputId = "sheet", label = "Sheet", choices = excel_sheets(input$file$datapath))
        )
      )
    )
  })
  
  output$ui_elements <- renderUI({
    req(data())
      # Display additional UI elements here
      tagList(
        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])
          )
        ),
        tags$hr(style="border-color: grey;")
      )
  })
}

shinyApp( ui = ui, server = server)

相关问题