我使用以下代码将excel文件上传到闪亮的R中:
Server.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
答案 0 :(得分:1)
它不起作用并且出现错误,因为正如MrFlick所说,您的write.xlsx(...)
函数必须处于被动对象中。我猜你正在使用ShinyApp
并下载data
你有一个downloadButton
,那么代码看起来就是这样:
ui.R:
downloadButton('download',label='Download data')
server.R:
output$download <- downloadHandler(
"probabilitymatrix.csv", content = function(file) {
write.xlsx(MDPData(),'www/probabilitymatrix.csv')})