无法读取 .RData 文件输入

Can't read an .RData fileInput

我想用 fileInput 导入一个 .RData 文件,但它不起作用,我收到以下错误消息:

Error in my.data$TYPE_DE_TERMINAL : $ operator is invalid for atomic vectors

 dt <- reactive({

    inFile <- input$file1

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

   load(inFile$datapath)
  })






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

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

下面的例子解决了这个问题。它允许您上传所有 .RData 个文件。

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

关于示例 "standalone" 我将存储两个向量的顶部部分插入到您的磁盘中,以便稍后加载和绘制它们。

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)