Reputation: 142
I have several data files that my Shiny app is supposed to load. To achieve this, I try to use the ShinyFiles
package. From ui.R
:
shinyFilesButton('file', 'Load Dataset', 'Please select a dataset', FALSE)
However, I am unsure about what to put in server.R
to load the file. I know how to get the file path and all, but where do I put the load()
command?
This is what I try now: (from server.R
):
observeEvent(input$file, {
inFile <- parseFilePaths(roots=roots, input$file)
load(as.character(inFile$datapath), envir=.GlobalEnv)
})
The files are data files as saved by save.image()
and contains some data frames, matrices and lists produced by another R script. In my Shiny app, I want to use the data mainly for graphics, so I need them to get loaded while the app is running.
Upvotes: 3
Views: 5635
Reputation: 7871
It s hard to understand what means "Shiny seems not to use the contents"
See example ( -- I have object "y" in my data.)
shinyUI(
fluidPage(
shinyFilesButton('file', 'Load Dataset', 'Please select a dataset', FALSE),
textOutput("txt")
)
)
shinyServer(function(input, output,session) {
shinyFileChoose(input,'file', session=session,roots=c(wd='.'))
observeEvent(input$file, {
inFile <- parseFilePaths(roots=c(wd='.'), input$file)
load(as.character(inFile$datapath), envir=.GlobalEnv)
})
output$txt=renderPrint({
input$file
if(exists("y")) y})
})
Text changed from data.
for simplisity you can use reactiveValues
like
shinyServer(function(input, output,session) {
shinyFileChoose(input,'file', session=session,roots=c(wd='.'))
envv=reactiveValues(y=NULL)
observeEvent(input$file, {
inFile <- parseFilePaths(roots=c(wd='.'), input$file)
load(as.character(inFile$datapath))
envv$y=y
})
output$txt=renderPrint({envv$y})
})
Both variants work but second better if you need different data in sessions.
Upvotes: 7