使用 fileInput() 將 .RData 檔案上傳到閃亮

該示例允許你上載 .RData 檔案。使用 loadget 的方法允許你將載入的資料分配給你選擇的變數名稱。對於示例獨立的問題,我插入了將兩個向量儲存到磁碟的頂部,以便稍後載入和繪製它們。

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