Cannot read file .RData fileInput

I want to import a .RData file with fileInput, but it doesn't work, I have this error message:

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

 dt <- reactive({

    inFile <- input$file1

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

   load(inFile$datapath)
  })






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

      

When I try to execute my application with manually imported .RData, it works well (I copied dt () directly using the dataframe in my directory) ...

+3


source to share


1 answer


The following example solves the problem. It allows you to download all files .RData

.

Thanks to @Spacedman for pointing out a better approach to loading data: Upload the file to a new environment and get it from there.



As for the example, which is "standalone", I have inserted the top section where the two vectors are stored on your disk for loading later.

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)

      

+3


source







All Articles