簡體   English   中英

運行閃亮的應用程序時出現錯誤“無法打開連接”

[英]error running shiny app “can not open connection”

我正在嘗試從計算機上的.RData文件加載數據,並且為此嘗試運行一個閃亮的應用程序。 我的代碼在下面,但是當我運行它時,出現錯誤“無法打開連接”。為什么會出現此錯誤?

library(shiny)

ui <- fluidPage(
  tableOutput("table")
)

server <- function(input, output, session) {
  dataset <- reactive({
    if (inFile == "")
      return(NULL)
    get(inFile$file1, load("E:/RProjects/Dashboard/gender1.RData"))
  })

 output$table <- renderTable({
    if (is.null(dataset()))
      return(NULL)
    head(dataset(), 10)
  })
}

shinyApp(ui, server)

樣本數據:

structure(list(Gender = c("Male", "Male", "Male", "Male", "Male", 
"Male", "Male", "Male", "Male", "Male"), Height = c(73.847017017515, 
68.7819040458903, 74.1101053917849, 71.7309784033377, 69.8817958611153, 
67.2530156878065, 68.7850812516616, 68.3485155115879, 67.018949662883, 
63.4564939783664), Weight = c(241.893563180437, 162.3104725213, 
212.7408555565, 220.042470303077, 206.349800623871, 152.212155757083, 
183.927888604031, 167.971110489509, 175.92944039571, 156.399676387112
), BMI = c(0.0443566151469252, 0.0343082174614673, 0.0387343292394288, 
0.0427654457094595, 0.0422547891767963, 0.033653156898047, 0.0388739862001733, 
0.0359564180086832, 0.039169072415755, 0.0388404008602306), probability = c(5.77831234737499e-06, 
0.605952546493327, 2.62595199514618e-05, 0.000362873417265588, 
0.00461190097404834, 0.911068673692331, 0.0496119303175197, 0.352335117615303, 
0.139124546478089, 0.343426515632885)), row.names = c(NA, 10L
), class = "data.frame")

正如Vishesh所說,我認為您可能需要使用readRDS而不是load ,但這是一個shiny應用程序,它允許所有三個:csv,rds或rda。

首先,快速調試設置,因此我們可以使用三種類型的文件進行測試:

write.csv(mtcars, file="mt.csv")
saveRDS(mtcars, file="mt.rds")
save(mtcars, file="mt.rda")

(生產應用程序肯定不需要。)

現在的應用程序:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV, rda, or rds File"),
      tags$hr(),
      checkboxInput("header", "Header (if CSV)", TRUE),
      uiOutput("rda_objname")
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  file1_ <- reactive({
    req(input$file1)
    # might also work with input$file1$type, which is something like
    # 'application/vnd.ms-excel', though for me in testing this was
    # blank for RDS/RDA ...
    a <- input$file1
    a$ext <- tolower(tools::file_ext(input$file1$name))
    # ... though length==1 since we did not do multiple = TRUE
    a$ext <- ifelse(a$ext == "rdata", "rda", a$ext)
    a
  })
  rawdat <- reactive({
    req(file1_())
    inFile <- file1_()
    # if we ever do fileInput(..., multiple = TRUE), this will need to
    # be on a vector of length > 1
    if ("csv" == inFile$ext) {
      return( read.csv(inFile$datapath, header = input$header) )
    } else if ("rds" == inFile$ext) {
      return( readRDS(inFile$datapath) )
    } else if (inFile$ext == "rda") {
      e <- new.env(parent = emptyenv())
      load(inFile$datapath, envir = e)
      return( e )
    } else return( NULL )    
  })
  output$rda_objname <- renderUI({
    # this only displays a select-input if the input file is valid and
    # an Rdata-looking file, otherwise the select-input is absent
    req(file1_())
    inFile <- file1_()
    if (inFile$ext == "rda") {
      obj <- isolate(ls(envir = rawdat()))
      selectInput("objname", "RDA object name",
                  choices = c("Select object name ...", obj))
    } else return( NULL )
  })
  dat <- reactive({
    req(rawdat())
    inFile <- isolate(file1_())
    if (inFile$ext == "rda") {
      req(input$objname, input$objname %in% ls(envir = rawdat()))
      return( get(input$objname, envir = rawdat()) )
    } else return( rawdat() )
  })
  output$contents <- renderTable({
    req(dat())
    dat()
  })
}

shinyApp(ui, server)

如果在fileInput選擇CSV或RDS文件,則它將自動呈現表。 如果它以.rda.rdata結尾(不區分大小寫),則它將創建一個選擇器來選擇rda文件中的哪個對象(因為它們實際上存儲帶有命名對象而不是單個對象的環境)。

演示:CSV或RDS:

帶有RDS文件的閃亮演示

使用RDA文件(其中具有單個對象mtcars

帶有RDA文件的閃亮演示

代碼中的其他一些更改:

  • 而不是使用if (is.null(...)) ,我使用的是更具shiny -esque req(...)方法; 當事情沒有按照您(開發人員)的預期進行時,它更加人性化;
  • 我故意isolate一些可能無法隔離的事物,但是我想要一條清晰的反應路徑。 如果A依賴於B而C同時依賴於A和B,則當A更新時,C將更新,然后B將更新,從而導致C重新更新……可能使人頭暈目眩,但這可能是多個依賴路徑的結果。
  • 因為這同時接受兩種類型的存儲(一個對象和多個對象),所以我需要兩步來檢索數據: rawdat()可能是環境(RDA)或實際的objecvt。 dat()將始終是對象或NULL (如果未選擇RDA和對象名稱)。
  • 我不需要output$rda_objnameelse return(NULL) ,在此示例中,我只是為了清楚和顯式代碼而已; 我可能不會在我的生產代碼中使用它。
  • 我在這里也經常使用return ; 從技術上講,在所有這些用途中都不需要它,對於這個示例,我還是要明確說明。

我建議使用readRDS讀取readRDS文件。 另外,您需要指定一個fileInput UI元素,用戶可以使用它來瀏覽到數據文件。

library(shiny)

ui <- fluidPage(
  fileInput("file", label = "Rdata"),
  tableOutput("table")
)

server <- function(input, output, session) {
  dataset <- reactive({
    req(input$file)
    inFile <- input$file
    readRDS(inFile$datapath)
  })

  output$table <- renderTable({
    if (is.null(dataset()))
      return(NULL)
    head(dataset(), 10)
  })
}

shinyApp(ui, server)

您在評論中提到的鏈接說明了req的用法,當應用加載並且用戶尚未選擇數據源時,它可以防止您的應用出錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM