[英]The output table did not display in R Shiny app
我使用 shiny 應用程序編寫了 R 腳本。 此腳本應讀取任何輸入 CSV 文件,並將其作為表格顯示在 shiny 用戶界面上。 我附上了我的代碼。 當我運行腳本時,我可以 select s CSV 文件,但 output 表不顯示。 請你幫助我好嗎?
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.
reactivity
原則規定我們不需要向output
添加反應性組件。 對於contents
,我們只需使其具有reactive
並在需要時訪問它。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.