简体   繁体   中英

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). 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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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