Why not use `source`?

Can someone please explain why using source to bring in functions in other files in shiny isn't a good idea? I've heard it being described as not a best practive but I'm having a tough time rearticulating why.

My recollection is hazy on this, but I think it has something to do with keeping paths straight—probably needing to have fully qualified file names to make sure the file can be found.

I use source, its useful so as not to end up with a single huge file. I don't know a valid alternative aside that perhaps some functionality can be provided via supporting package(s), but I consider that rather heavyweight.

Since Shiny 1.5.0, by default all supporting R files in the R/ directory are loaded via loadSupport(). loadSupport under the hood doesn't use source(). Instead it uses the custom function sourceUTF8() which makes use of eval().

I guess the reason for this is source()'s UTF-8 handling on Windows.

Besides this corner case the only disadvantage I can see is the minimal overhead caused by loading another file (in my opinion negligible).

Just like nirgrahamuk I'm using source() to organize my apps and so far it never caused any problems. However, please mind the "local" argument.

Btw. source works just fine using relative paths in shiny.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.