I want to import 2 Rdata files into my shinyapp, then display the tables in the mainpanel, I tried the following code but it gives me a character output:
What should i add to my code to have the dataframes ? how can I allow to my app to import also csv files ?
This is my code :
server.R :
shinyServer(function(input,output){
#lecture de la table panel
panel <- reactive({
req(input$pan)
sessionEnvir <- sys.frame()
if (!is.null(input$pan)) {
p<-load(input$pan$datapath, sessionEnvir)
}
p
})
#lecture dela table achat
achats<- reactive({
req(input$achat)
sessionEnvir <- sys.frame()
if (!is.null(input$achat)){
a<-load(input$achat$datapath, sessionEnvir)
}
a
})
output$t <- renderTable(
panel()
)
output$t2 <- renderTable(
achats()
)
}
)
UI.R :
ui <- fluidPage(
titlePanel("Shiny app"),
sidebarLayout(
sidebarPanel(
#Import des tables panel et achat
fileInput("pan", label = "Import panel table",
multiple = FALSE,
buttonLabel = "Browse",
placeholder = "No file selected",
accept = c('.RData')),
fileInput("achat", label = "Import purchase table",
multiple = FALSE,
buttonLabel = "Browse",
placeholder = "No file selected",
accept = c('.RData')),
),
mainPanel(
tableOutput('t'),
tableOutput('t2'),
)
)
)
any help please ?
Thanks
It's perfectly normal. Rdata
files may contain multiple objects. The function load
return the names of the loaded objects, so your panel
and achats
reactive will return the names of the stored objects. This is not what you want ! Instead return the object stored in the provided environment. I would also not store that object in the current frame but instead in a new temporary environment (new.env()
). Expect also a problem if the given rdata
file contains multiple object !
If you want single object file, you'd better use RDS files. You then could use p<-readRDS(dataPath)
to load and store it !
...
if (!is.null(input$pan)) {
tmp_env <- new.env()
p <- load(input$pan$datapath, tmp_env )
if(length(p)==1)
p <- tmp_env[[p]]
else
p <- NULL # better generate error !
}
p