【发布时间】:2022-01-15 18:58:51
【问题描述】:
我使用闪亮的应用程序编写了一个 R 脚本。该脚本应该读取任何输入的 CSV 文件并将其作为表格显示在闪亮的用户界面上。我附上了我的代码。当我运行脚本时,我可以选择 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.
【问题讨论】:
-
contents不应该放在output中,它是反应式的意思是你总是在没有改变的情况下访问它,它不会被写在任何地方。 -
谢谢您,我根据您的评论更正了代码。它还不起作用。你能写出更正的代码作为答案吗?谢谢
-
添加了下面的样子
标签: r shiny shinydashboard