0

目前我正在尝试读取模块内的 rds 文件。我正在使用该golem软件包。这是我现在的代码,我不知道如何解决这个问题。

目标:上传 rds 文件后,conditionalPanel()应该会出现。

问题:读取文件失败

代码

app_ui.R

#' The application User-Interface
#' 
#' @param request Internal parameter for `{shiny}`. 
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_ui <- function(request) {
  tagList(
    # Leave this function for adding external resources
    golem_add_external_resources(),
    # Your application UI logic 
    navbarPage(
      title = "scrnaseq_app", # Give the app a title
      id = "tabs",
      
      tabPanel(
        title = "Data Input",
        value = "dataInput",
        icon = icon("upload"),
        mod_dataInput_ui("dataInput_ui_1")
      )
    )
  )
}

#' Add external Resources to the Application
#' 
#' This function is internally used to add external 
#' resources inside the Shiny application. 
#' 
#' @import shiny
#' @importFrom golem add_resource_path activate_js favicon bundle_resources
#' @noRd
golem_add_external_resources <- function(){
  
  add_resource_path(
    'www', app_sys('app/www')
  )
 
  tags$head(
    favicon(),
    bundle_resources(
      path = app_sys('app/www'),
      app_title = 'scrnaseqExploreR'
    )
    # Add here other external resources
    # for example, you can add shinyalert::useShinyalert() 
  )
}

app_server.R

#' The application server-side
#' 
#' @param input,output,session Internal parameters for {shiny}. 
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
options(shiny.maxRequestSize = 10000*1024^2) #10GB

app_server <- function( input, output, session ) {

  mod_dataInput_server("dataInput_ui_1")
  
}

mod_dataInput.R

#' dataInput UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_dataInput_ui <- function(id){
  ns <- NS(id)
  tagList(
    fluidPage(
      fluidRow(
        column(
          width = 6,
          offset = 3,
          h3("1. Upload"),
          fileInput(
            width = "100%",
            inputId = ns("upload_rdsFile"),
            label = "Upload Seurat file: (.rds)",
            accept = ".rds",
            buttonLabel = "Browse",
            placeholder = "Please upload .rds file!"
          ),
          
          conditionalPanel(
            "output.fileUploaded == true",
            
            h3("2. Selection")
            # placeholder
          )
        )
      )
    )
  )
}
    
#' dataInput Server Functions
#'
#' @noRd 
mod_dataInput_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    
    sc <- reactive({
      inFile = input$upload_rdsFile
      if (is.null(inFile)) {
        tmp = NULL
      } else {
        tmp = readRDS(inFile$datapath)
      }
      return(tmp)
    })
    
    output$fileUploaded = reactive({
      return(!is.null(sc()))
    })
    outputOptions(output, 'fileUploaded', suspendWhenHidden = FALSE)
  })
}

错误信息:

Warning: Error in readRDS: error reading from connection
  117: readRDS
  116: <reactive:sc> [/Users/marius/GitHub/scrnaseqExploreR/R/mod_dataInput.R#54]
  100: sc
   99: <reactive:output$fileUploaded> [/Users/marius/GitHub/scrnaseqExploreR/R/mod_dataInput.R#61]
   83: output$dataInput_ui_1-fileUploaded
    4: runApp
    3: print.shiny.appobj
    1: source

4

1 回答 1

1

解决了!

问题出在readRDS(). 我猜这是由于namespacing使用模块时的不同造成的。

    rds <- reactive({
      validate(need(input$upload_rdsFile, message = FALSE))
      input$upload_rdsFile
    })
    
    sc <- reactive({
      readRDS(rds()$datapath)
    })
于 2021-08-09T14:28:40.277 回答