Dimitri Petrenko Dimitri Petrenko - 1 month ago 8
R Question

Cant read an .RData fileInput with shiny

I want to import a .RData file with fileInput but It doesnt work, I have this error message :


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()


When I try my application with a .RData imported manually it works well (I remplaced dt() directly with a dataframe in my directory) ...

Thank you in advance !

Answer

The following example solved my problem. It allows you to upload all .RData files. The approach with load and get allows you to assign the loaded data to a variable name of your choice. For the matter of the example being "standalone" I inserted the top section that stores two vectors to your disk in order to load and plot them 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 and get it's name
    data <- load(file)
    # assign the loaded data to the variable data
    data <- get(data)
    output$hist <- renderPlot({
      hist(data)
    })
  })
})

# Run the application 
shinyApp(ui = ui, server = server)