Hello @jpcaico,
Welcome to the Great RStudio Community!!!
What you are trying to achieve is actually very possible; however, this is not really a shiny question. It is more like an R question, which can be implemented in Shiny (yeah, I know, Shiny is also part of the R ecosystem! ).
The answer to your question is the .rds
extension in R. Any R object (e.g. data frames, lists, vectors, models, functions, ...) can be saved to disk as an .rds
file (with saveRDS()
) and can be imported as is back into the global environment of your current R session (with readRDS()
). The readr
package has similar functions: write_rds()
and read_rds()
.
In the following example:
-
I train a linear model, which attempts to explain how a car's weight (wt
) impacts its gas consumption (mpg
). The dataset used is the (in)famous mtcars
dataset, which comes with R
-
I then save the model to disk as an .rds
file
-
Finally, I emulate a user of your app who imports the model and runs it on new data.
It's important to note that here I use the base lm()
and predict()
functions, but you may want to use functions from other packages (e.g. modelr
, parsnip
, caret
, mlr
, ...).
# Train a linear model of car weight (wt) on gas consumption (mpg)
mod <- lm(mpg ~ wt, data = mtcars)
mod
Call:
lm(formula = mpg ~ wt, data = mtcars)
Coefficients:
(Intercept) wt
37.285 -5.344
# Save the model to disk
saveRDS(mod, "linear_model.rds")
# Now, a user of your shiny app has the following dataset
dat <- data.frame(wt = runif(100, 1, 4))
# He/she imports the linear model to his/her R session runs it on the data
lin_mod <- readRDS("linear_model.rds")
predict(object = lin_mod, newdata = dat)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
27.32980 19.30143 25.38336 17.78287 16.86175 31.21023 23.47332 17.63213 23.09927 24.61956 16.59935 24.67216 21.07688 22.75939 30.29042
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
17.51339 27.99503 31.26630 26.68297 16.63670 17.67830 20.83265 21.67114 15.99911 21.42745 20.58049 23.21742 22.41453 27.30444 29.58192
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
16.50009 17.47372 20.86629 19.18660 31.54601 24.27995 19.77996 28.47090 26.83913 28.22690 29.65108 25.29406 25.30724 26.02680 29.49644
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
29.71512 28.20432 24.46969 27.67620 18.18675 31.20582 24.85068 19.13116 29.98619 22.94674 28.62925 29.89589 19.86256 17.59002 25.93674
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
21.27659 30.42003 25.78431 27.54135 18.87919 24.74941 18.95256 18.91528 19.20463 24.88865 19.84384 21.85209 20.55401 31.93064 24.31971
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
28.41140 25.85090 22.11584 26.30013 30.15877 28.03460 21.22944 25.24435 19.30318 30.29138 24.96784 16.14843 17.62200 17.72753 29.13396
91 92 93 94 95 96 97 98 99 100
29.84516 21.46920 26.43291 21.41058 26.80398 28.93133 19.39781 30.44001 24.45659 23.73948
The above may be a solution to your problem; however, you may want to look into the creation of an API with the plumber
package. It will be useful if you have trained models that you want others to access. If you have a bit of time, watching the following video will give you a fair idea:
Hope all this helps.