运行闪亮的应用程序时出错“无法打开连接”

I_m_LeMarque

我正在尝试从我的计算机上的 .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")
r2evans

正如 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)

如果您在 中选择 CSV 或 RDS 文件fileInput,则它会自动呈现表格。如果它以.rdaor结尾.rdata(不区分大小写),那么它将创建一个选择器来选择 rda 文件中的哪个对象(因为它们确实存储了具有命名对象的环境,而不是单个对象)。

演示:使用 CSV 或 RDS:

带有 RDS 文件的闪亮演示

使用 RDA 文件(其中有一个对象,mtcars

带有 RDA 文件的闪亮演示

您的代码中的一些其他更改:

  • if (is.null(...))没有使用而是使用了更shiny风格化的req(...)方法;当事情不像您(开发人员)预期的那样进行时,它会更加用户友好;
  • 我故意设计了isolate一些可能不会被孤立的东西,但我想要一条清晰的反应路径;如果 A 依赖于 B 并且 C 依赖于 A 和 B,则有可能,当 A 更新时,C 将更新,然后 B 将更新,导致 C 再次更新......可能令人眼花缭乱,但这是多个依赖路径的结果.
  • 因为这接受两种类型的存储(一个对象和多个),所以我需要两步来检索数据:rawdat()可能是环境 (RDA) 或实际对象;dat()将始终是对象或NULL(如果未选择 RDA 和对象名称)。
  • 我不需要else return(NULL)in output$rda_objname,我只是为了在这个例子中清晰和明确的代码而把它放在那里;我可能不会在我的生产代码中使用它。
  • return这里也用了很多;从技术上讲,在任何这些用途中都不需要它,同样我只是在这个例子中明确说明。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在设备上运行Beta iOS应用程序时出错

来自分类Dev

Ember-CLI-运行默认应用程序时出错

来自分类Dev

运行应用程序时出错:“项目不可用”

来自分类Dev

在Bluemix上运行Spring Boot应用程序时出错

来自分类Dev

找不到类运行应用程序时出错

来自分类Dev

使用-jar运行Spring Boot应用程序时出错

来自分类Dev

运行Babel应用程序时出错

来自分类Dev

运行Javafx应用程序时出错

来自分类Dev

运行冗长的闪亮应用程序时网络超时

来自分类Dev

运行Spring Boot应用程序时出错

来自分类Dev

jreLeakPrevention.gcDaemonFail运行GWT应用程序时出错

来自分类Dev

我收到“无法打开APK:I / O错误”。在运行Xamarin表单应用程序时

来自分类Dev

我收到“无法打开APK:I / O错误”。在运行Xamarin表单应用程序时

来自分类Dev

读取多个文件时,ggplot图形未显示在闪亮的应用程序中-无法打开连接

来自分类Dev

退出闪亮的应用程序时如何关闭连接

来自分类Dev

运行MVC应用程序时出错

来自分类Dev

运行简单的Phonegap 3应用程序时出错

来自分类Dev

通过设备运行Android应用程序时出错

来自分类Dev

UILocalNotification-从通知打开应用程序时出错

来自分类Dev

在Visual Studio中运行Cordova应用程序时出错

来自分类Dev

找不到类,运行应用程序时出错

来自分类Dev

运行SOLR应用程序时出错

来自分类Dev

运行Ruby应用程序时出错

来自分类Dev

在 Cloud 9 上运行 Sinatra 应用程序时出错

来自分类Dev

使用 GeckoFX 打开应用程序时出错

来自分类Dev

Shiny Server 无法打开与任何闪亮应用程序的连接

来自分类Dev

运行 CodeIgniter 应用程序时出错

来自分类Dev

首次运行应用程序时出错无法投射问题

来自分类Dev

构建闪亮的应用程序时 fread 函数出错

Related 相关文章

  1. 1

    在设备上运行Beta iOS应用程序时出错

  2. 2

    Ember-CLI-运行默认应用程序时出错

  3. 3

    运行应用程序时出错:“项目不可用”

  4. 4

    在Bluemix上运行Spring Boot应用程序时出错

  5. 5

    找不到类运行应用程序时出错

  6. 6

    使用-jar运行Spring Boot应用程序时出错

  7. 7

    运行Babel应用程序时出错

  8. 8

    运行Javafx应用程序时出错

  9. 9

    运行冗长的闪亮应用程序时网络超时

  10. 10

    运行Spring Boot应用程序时出错

  11. 11

    jreLeakPrevention.gcDaemonFail运行GWT应用程序时出错

  12. 12

    我收到“无法打开APK:I / O错误”。在运行Xamarin表单应用程序时

  13. 13

    我收到“无法打开APK:I / O错误”。在运行Xamarin表单应用程序时

  14. 14

    读取多个文件时,ggplot图形未显示在闪亮的应用程序中-无法打开连接

  15. 15

    退出闪亮的应用程序时如何关闭连接

  16. 16

    运行MVC应用程序时出错

  17. 17

    运行简单的Phonegap 3应用程序时出错

  18. 18

    通过设备运行Android应用程序时出错

  19. 19

    UILocalNotification-从通知打开应用程序时出错

  20. 20

    在Visual Studio中运行Cordova应用程序时出错

  21. 21

    找不到类,运行应用程序时出错

  22. 22

    运行SOLR应用程序时出错

  23. 23

    运行Ruby应用程序时出错

  24. 24

    在 Cloud 9 上运行 Sinatra 应用程序时出错

  25. 25

    使用 GeckoFX 打开应用程序时出错

  26. 26

    Shiny Server 无法打开与任何闪亮应用程序的连接

  27. 27

    运行 CodeIgniter 应用程序时出错

  28. 28

    首次运行应用程序时出错无法投射问题

  29. 29

    构建闪亮的应用程序时 fread 函数出错

热门标签

归档