0

我使用以下代码将 excel 文件上传到闪亮的 R 中:

服务器.r

#reative function to read a table
MDPData<-reactive({
  file1<-input$MDPhistorical
  if(is.null(file1)){return()}
  z<-as.data.frame(read.xlsx(file1$datapath,sheetName = 'Probability Matrix'))      
})

#file info  
output$MDPDataTable<-DT::renderDataTable({
  if(is.null(MDPData())){return()}
  MDPData()
  DT::datatable(MDPData(),extensions = 'Responsive', options = list(pageLength=3), class = 'cell-border stripe', selection = "single")
})

ui.r 的相关代码:

fluidRow(
  box(
    title = "Historical Data of different conditions", status = "primary", solidHeader = TRUE,
    collapsible = TRUE,
    DT::dataTableOutput("MDPDataTable")
   )
)

然后我想将 MDPDataTable 保存为 csv 文件。我尝试使用 write.csv :

write.xlsx(MDPDataTable,'www/probabilitymatrix.csv')

错误如下:

Warning: Error in is.data.frame: object 'MDPDataTable' not found
Stack trace (innermost first):
    42: is.data.frame
    41: write.xlsx
    40: server [C:\Users\foad\Desktop\DSS Journal\NSS Protorype\Nss-28-2-17/server.R#902]
     1: shiny::runApp
Error in is.data.frame(x) : object 'MDPDataTable' not found
4

1 回答 1

1

它不起作用并且出现错误,因为正如 MrFlick 所说,您的write.xlsx(...)函数必须在反应对象中。我猜你正在使用ShinyApp并下载data你有一个downloadButton,那么代码将如下所示:

ui.R:

downloadButton('download',label='Download data')

服务器.R:

output$download <- downloadHandler( "probabilitymatrix.csv", content = function(file) { write.xlsx(MDPData(),'www/probabilitymatrix.csv')})

于 2017-02-28T09:46:15.020 回答