简体   繁体   English

使用Shiny下载.RData文件

[英]Downloading .RData files with Shiny

I'm creating a Shiny app and one of my outputs is best saved as a .RData file for the user. 我正在创建一个Shiny应用程序,最好将输出之一保存为用户的.RData文件。

I can download data in various other formats but I'm not sure how to work with .RData. 我可以下载其他各种格式的数据,但不确定如何使用.RData。 An alternate method to save R objects would be fine here too. 另一种保存R对象的方法在这里也可以。 Some dummy code on the server side would look like: 服务器端的一些伪代码如下所示:

# Make widget
widget <- 1:42

# Download widget
output$widget <- downloadHandler(
  filename=paste0("widget_", Sys.Date(), ".RData"), 
  content=function(file){
    save(widget), file=file)
  }
)

I can click the download button fine and it refreshes my window but no items are put in the download queue. 我可以单击“下载”按钮,然后它刷新我的窗口,但没有任何项目放入下载队列中。

I tried to save a random Forest Model in .RData format. 我试图以.RData格式保存随机森林模型。 Below code worked for me. 下面的代码为我工作。 Hope the same will work for you. 希望同样能为您服务。

ui.R ui.R

downloadButton('downloadModel', 'Download RF Model', class="dlButton") downloadButton('downloadModel','Download RF Model',class =“ dlButton”)

server.R server.R

Step1. 第1步。 Create a reactiveValue to save the reactive function, in my case the random forest model rf1() 创建一个reactValue来保存反应函数,在我的例子中是随机森林模型rf1()

# Create a reactive value rf2 to store the random forest model rf1().
rf2 <- reactiveValues()
observe({
  if(!is.null(rf1()))
  isolate(
    rf2 <<- rf1()
  )
})

Step2. 第2步。 Save the reactiveValue in the downloadHandler as you have done. 完成后,将reactactiveValue保存在downloadHandler中。

# Download Random Forest Model
  output$downloadModel <- downloadHandler(
    filename <- function(){
      paste("RF Model.RData")
    },

    content = function(file) {
      save(rf2, file = file)
    }
  )

Hope this works for you. 希望这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM