If all your files are in the R directory, any function will be in memory after creating the package assembly or Load_All . You may have problems if you have code in files that are not in the tho function.
R downloads files in alphabetical order.
This is usually not a problem, because functions are evaluated when they are called for execution, and not during loading (id. A function can refer to another function that is not yet defined, even in the same file).
But if you have code outside the function in model.R, this code will be executed immediately when the file is downloaded, and your package assembly will not normally be performed using
ERROR: lazy loading failed for package 'yourPackageName'
If so, put the sparse model.R code in the function so that you can call it later when the package is fully loaded, the external library too.
If this piece of code exists to initialize some value, consider use_data() so that R takes care of loading the data into the environment for you. If this piece of code is just interactive code written for testing and implementing the package itself, you should consider placing it in another place or in any case passing it to functions.
if you really need this code that will be executed at boot time or will really need a solution, then you must add the mapping string to the DESCRIPTION file, as already mentioned by Peter Humburg, to make R load the file. Roxygen2 can help you put code before
call roxygenize() and the sort string will be generated for you in the DESCRIPTION file.
But even so, an external library that you may depend on is not yet loaded by the package, which leads to a failure again during build.
In conclusion, you better not leave the code outside the functions in the .R file if it is inside the package.