Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

r - Can't read an .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 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) ...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Thanks to @Spacedman for pointing me to a better approach of loading the data: Load the file into a new environment and get it from there.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...