我使用 Shiny 的应用程序编写了一个 R 脚本。该脚本应该读取任何输入 CSV 文件并将其显示在 Shiny 的用户界面上作为表格。我附上了我的代码。当我运行脚本时,我可以选择 s CSV 文件,但不显示输出表。你能帮我一下吗?
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.
最佳答案
principles reactivity
指定我们不需要向 output
添加 react 组件。对于内容
,我们只需将其设置为响应式
并在需要时访问它。
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)
关于r - 输出表未显示在 R Shiny 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70315436/