下载 R shiny 中的数据集

download the dataset in R shiny

我是 R shiny 的新手,我要下载我正在查看的数据集,但它显示“未使用的参数(mainPanel(dowloadButton(“downloadDatable”,“下载数据集”))))”。你能帮我吗?非常感谢您的帮助。

library(shiny)

# Read the data
temp <- tempfile()
download.file("http://archive.ics.uci.edu/ml/machine-learning-databases/00356/student.zip",
temp, mode="wb")
unzip(temp, "student-mat.csv")
mathdat <- read.table("student-mat.csv",sep= ";", header= T)
unlink(temp)

# UI part

shinyUI(navbarPage(
  title = 'DataTable Options',
  tabPanel('Display length',     DT::dataTableOutput('table.sub_1')),
  tabPanel('Length menu',        DT::dataTableOutput('table.sub_2')),
),
  mainPanel(
    dowloadButton("downloadDatable", "Download the Dataset"))
)

# Server part
shinyServer(function(input, output, session) {
  # display 10 rows initially
  output$table.sub_1 <- DT::renderDataTable(
    DT::datatable(mathdat, options = list(pageLength = 25))
  )
  
  # -1 means no pagination; the 2nd element contains menu labels
  output$table.sub_2 <- DT::renderDataTable(
    DT::datatable(
      mathdat, options = list(
        lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
        pageLength = 15
      )
    )
  )
  
  
  # Download the datatable
  output$downloadDatable <- downloadHandler(
    filename = function(){paste("Student Performance in Math.csv")},
    content = function(file){write.csv(mathdat(), file, row.names = FALSE)}
  )
  
  
  
})

mathdat() 应该是 write.csv(mathdat,file,row.names = FALSE) 中的 mathdat,除非您在其他地方定义反应式数据框。