以下示例解决了该问题 . 它允许您上传所有 .RData 文件 .
感谢@Spacedman为我指出了一种更好的加载数据的方法:将文件加载到新环境中并从那里获取 .
对于示例“独立”的问题,我插入了将两个向量存储到磁盘的顶部,以便稍后加载和绘制它们 .
library(shiny)
# Define two datasets and store them to disk
x
save(x, file = "x.RData")
rm(x)
y
save(y, file = "y.RData")
rm(y)
# Define UI
ui
titlePanel(".RData File Upload Test"),
mainPanel(
fileInput("file", label = ""),
actionButton(inputId="plot","Plot"),
plotOutput("hist"))
)
)
# Define server logic
server
observeEvent(input$plot,{
if ( is.null(input$file)) return(NULL)
inFile
file
# load the file into new environment and get it from there
e = new.env()
name
data
# Plot the data
output$hist
hist(data)
})
})
})
# Run the application
shinyApp(ui = ui, server = server)