Reputation: 1168
I'm trying to input two files, to perform some transformations and to return a data table. It works fine if I hardcode the file path but not if I use fileInput
. I'm using obersveEvent
because the options and operations are a bit more complex of what I show in the reproducible example.
Any suggestions on what might be wrong with the code?
library(shiny)
library(shinydashboard)
library(data.table)
library(DT)
##
ui <- shinyUI(pageWithSidebar(
headerPanel("Test app"),
sidebarPanel(
fileInput("Env_db1", "Choose CSV File for DB1",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
fileInput("Env_db2", "Choose CSV File for DB2",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
)
,
actionButton(
inputId = "submit_loc",
label = "Submit")
),
mainPanel(
DT::dataTableOutput("table"))
)
)
##
server <- shinyServer(function(input, output) {
observeEvent(
eventExpr = input$submit_loc,
handlerExpr =
{
req(input$file1)
inFile1 <- input$file1
if(is.null(input$file1)) return(NULL)
Env_1<- read.csv(inFile1$datapath, header = TRUE, sep = ";")
req(input$file2)
inFile2 <- input$file2
if(is.null(input$file2)) return(NULL)
Env_2 <- read.csv(inFile2$datapath, header = TRUE, sep = ";")
Difference <-rbind(Env_1,Env_2)
output$table = DT::renderDataTable(server = TRUE,{
DT::datatable(Difference,
extensions=c("Buttons",'Scroller'),
options = list(dom = 'Bfrtip',
buttons = c('copy', 'csv',
'excel', 'pdf',
'print'),
scrollY = 500,
scroller = TRUE)
)
})
})
})
##
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 147
Reputation: 6325
Mistakenly you have used file1
in your server.R to refer the input file object which you are reading as Env_db1
Updated code:
library(shiny)
library(shinydashboard)
library(data.table)
library(DT)
##
ui <- shinyUI(pageWithSidebar(
headerPanel("DBC Comparison"),
sidebarPanel(
fileInput("Env_db1", "Choose CSV File for DB1",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
fileInput("Env_db2", "Choose CSV File for DB2",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
)
,
actionButton(
inputId = "submit_loc",
label = "Submit")
),
mainPanel(
DT::dataTableOutput("table"))
)
)
##
server <- shinyServer(function(input, output) {
observeEvent(
eventExpr = input$submit_loc,
handlerExpr =
{
req(input$Env_db1)
inFile1 <- input$Env_db1
if(is.null(input$Env_db1)) return(NULL)
Env_1<- read.csv(inFile1$datapath, header = TRUE, sep = ";")
req(input$Env_db2)
inFile2 <- input$Env_db2
if(is.null(input$Env_db2)) return(NULL)
Env_2 <- read.csv(inFile2$datapath, header = TRUE, sep = ";")
Difference <-rbind(Env_1,Env_2)
output$table = DT::renderDataTable(server = TRUE,{
DT::datatable(Difference,
extensions=c("Buttons",'Scroller'),
options = list(dom = 'Bfrtip',
buttons = c('copy', 'csv',
'excel', 'pdf',
'print'),
scrollY = 500,
scroller = TRUE)
)
})
})
})
##
shinyApp(ui = ui, server = server)
Upvotes: 1
Reputation: 7704
The name of your fileInput
variable are wrong instead of input$file1
and input$file2
it should be input$Env_db1
and input$Env_db2
. Replacing the variables as shown below, your code works fine.
library(shiny)
library(shinydashboard)
library(data.table)
library(DT)
##
ui <- shinyUI(pageWithSidebar(
headerPanel("DBC Comparison"),
sidebarPanel(
fileInput("Env_db1", "Choose CSV File for DB1",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
fileInput("Env_db2", "Choose CSV File for DB2",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
)
,
actionButton(
inputId = "submit_loc",
label = "Submit")
),
mainPanel(
DT::dataTableOutput("table"))
)
)
##
server <- shinyServer(function(input, output) {
observeEvent(
eventExpr = input$submit_loc,
handlerExpr =
{
req(input$Env_db1)
inFile1 <- input$Env_db1
if(is.null(input$Env_db1)) return(NULL)
Env_1<- read.csv(inFile1$datapath, header = TRUE, sep = ";")
req(input$Env_db2)
inFile2 <- input$Env_db2
if(is.null(input$Env_db2)) return(NULL)
Env_2 <- read.csv(inFile2$datapath, header = TRUE, sep = ";")
Difference <-rbind(Env_1,Env_2)
output$table = DT::renderDataTable(server = TRUE,{
DT::datatable(Difference,
extensions=c("Buttons",'Scroller'),
options = list(dom = 'Bfrtip',
buttons = c('copy', 'csv',
'excel', 'pdf',
'print'),
scrollY = 500,
scroller = TRUE)
)
})
})
})
##
shinyApp(ui = ui, server = server)
Hope it helps!
Upvotes: 1