2017-02-28 30 views
0

我上传的Excel文件到光泽ř使用以下代码:转换数据表到csv文件中的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

您不想将HTML表格写入CSV文件,数据存储在'MDPData()'中。使用'write.xlsx(MDPData(),'www/probabilitymatrix.csv')',尽管目前还不清楚你试图在哪里运行它。它需要在反应性物体可用的地方。 – MrFlick

+0

谢谢,但是当我写了MDPData,它给了我另一个错误:警告:.getReactiveEnvironment()中的错误$ currentContext:没有活动的反应上下文不允许操作。 (您试图做一些只能从被动表达式或观察者内部完成的操作。) .getReactiveEnvironment()中的错误$ currentContext(): 没有活动的被动上下文,不允许操作。 (你试图做一些只能从被动表达或观察者内部完成的事情。) – user

回答

1

它不工作,出现错误,因为作为MrFlick说,你功能必须在反应对象。我猜你正在使用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')})