首页 文章

无法读取.RData文件输入

提问于
浏览
4

我想用fileInput导入.RData文件,但它不起作用,我有这样的错误消息:

my.data中的错误$ TYPE_DE_TERMINAL:$运算符对原子向量无效

dt <- reactive({

    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

   load(inFile$datapath)
  })






  GetData <- reactive({
    my.data <- dt()

当我尝试使用.RData手动导入我的应用程序时它运行良好(我直接使用我的目录中的数据框重新填充 dt() )...

1 回答

  • 4

    以下示例解决了该问题 . 它允许您上传所有 .RData 文件 .

    感谢@Spacedman为我指出了一种更好的加载数据的方法:将文件加载到新环境中并从那里获取 .

    对于示例“独立”的问题,我插入了将两个向量存储到磁盘的顶部,以便稍后加载和绘制它们 .

    library(shiny)
    
    # Define two datasets and store them to disk
    x <- rnorm(100)
    save(x, file = "x.RData")
    rm(x)
    y <- rnorm(100, mean = 2)
    save(y, file = "y.RData")
    rm(y)
    
    # Define UI
    ui <- shinyUI(fluidPage(
      titlePanel(".RData File Upload Test"),
      mainPanel(
        fileInput("file", label = ""),
        actionButton(inputId="plot","Plot"),
        plotOutput("hist"))
      )
    )
    
    # Define server logic
    server <- shinyServer(function(input, output) {
      observeEvent(input$plot,{
        if ( is.null(input$file)) return(NULL)
        inFile <- isolate({input$file })
        file <- inFile$datapath
        # load the file into new environment and get it from there
        e = new.env()
        name <- load(file, envir = e)
        data <- e[[name]]
    
        # Plot the data
        output$hist <- renderPlot({
          hist(data)
        })
      })
    })
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

相关问题