简体   繁体   English

R通过唯一名称闪亮保存文件

[英]R Shiny Save File by Unique Name

I have developed a Shiny app where users have the option to save machine learning models (to be able to use them later). 我开发了一个Shiny应用程序,用户可以选择保存机器学习模型(以后可以使用它们)。 These models get saved in the default shiny directory. 这些模型将保存在默认的闪亮目录中。

The issue is that, since the name of the model file being saved is not unique, the file can be overwritten when multiple users are using the app. 问题在于,由于要保存的模型文件的名称不是唯一的,因此当多个用户使用该应用程序时,该文件可能会被覆盖。

I want the files to be saved by a unique name and the users to be able to load their specific files back 我希望文件以唯一的名称保存,并且用户能够将其特定文件加载回去

Below is the code I am using 下面是我正在使用的代码

# Save model to be used later

   .jcache(m1$classifier)
    observeEvent(input$save, {
      #delete previous model if it exists in folder
      fn <- "m1"
      if (file.exists(fn)) file.remove(fn) 
      save(m1, file = "D:\\Dropbox\\Users\\Myname\\m1")
    })

#Load model saved earlier
load(file="m1")

There is a package called uuid that can help with this: 有一个名为uuid的软件包可以帮助解决这个问题:

install.packages("uuid")

# This function will create a unique string for you that you can use as your filename
fn <- uuid::UUIDgenerate()

So I suggest generating a new filename each time you want to save a model and storing it in a variable that can be referred back to when you want to reload the model. 因此,我建议每次要保存模型时都生成一个新文件名,并将其存储在一个变量中,当您要重新加载模型时,该变量可以被引用。

load(file=fn)

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

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