Reputation: 21
I wrote an R script by using shiny app. This script should read any input CSV file and shows it on the shiny user interface as the table. I attached my code. When I run the script, I can select s CSV file, but the output table does not display. Could you please help me?
library(shiny)
library(DT)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
# tableOutput('dto')
dataTableOutput('dto'),
)
)
)
server <- function(input,output){
# Reactive expression with the data, in this case iris
#----------------
output$contents <- reactive({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = input$header),
})
#----------------
#the extensions parameter coupled with the options list does the trick
output$dto <- renderDataTable(contents(), extensions = 'Buttons',
options = list(dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))
)
}
runApp(list(ui=ui,server=server), launch.browser=TRUE) #now runs by default in the external browser.
Upvotes: 2
Views: 829
Reputation: 13319
The principles of reactivity
specify that we do not need to add reactive components to output
. For contents
, we simply make it reactive
and access it whenever we need.
library(shiny)
library(DT)
?runApp
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
# tableOutput('dto')
dataTableOutput('dto'),
)
)
)
server <- function(input,output){
# Reactive expression with the data, in this case iris
#----------------
contents <- reactive({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
inFile <- input$file1
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath, header = input$header)
})
#----------------
#the extensions parameter coupled with the options list does the trick
output$dto <- renderDataTable(contents(), extensions = 'Buttons',
options = list(dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))
)
}
runApp(list(ui=ui,server=server), launch.browser=TRUE)
Upvotes: 2