Shiny Developer Series - Episode 2 - Follow-up Thread - Colin Fay on `golem` and Effective Shiny Development Methods

Hey @giocomai,
thanks for this question.

I think the cleaner way to do it is not through a global.R — if you have data to put inside your app, you can add them just as any data in a package. An app built with {golem} is a package, so anything you know about package applies there :slight_smile:

So basically, what we comonly do is :

  • Create a data-raw folder with usethis::use_data_raw()
  • In the newly created folder, you'll find (with newest version of usethis):
## code to prepare `DATASET` dataset goes here

usethis::use_data("DATASET")

So just do the read csv, some manipulation if needed, and launch the last line. It will add DATASET as an internal dataset for your package, with can then be used inside your app function.

For more info about data in a package :

If the data is not on your computer, I think there are several use cases :

  • data is on a database — you can create the connection in the app_server() function. Here is an example :
app_server <- function(input, output,session) {

  impala <- reactiveValues()
  mongo <- reactiveValues()

  observe({

    impala$con <- connect_base( "Cloudera ODBC Driver for Impala 64-bit")
    impala$products <- get_products(impala$con)
    impala$lots <- get_lot(impala$con)
    impala$ok <- TRUE
    mongo$db <- connect_mongo()
    mongo$has_db <- nrow(mongo$db$find('{}')) > 0
    progress$close()
  })
[...]
  • data is sent by the user: then there is shiny::downloadHandler() and friends, which create an uploader in the UI.

  • data is to be passed as an argument to run_app(), and is located in the package directory. Put the data in the inst folder of your golem, then you can use system.file("data.csv", package = "myapp") once the package is either installed or launched with pkgload::load_all(). For example, system.file(package = "stats") retrieve the path of the {stats} package on the local machine.

More on that :

By the way, the most recent version of {golem} has a new, unified way to pass argument to the run_app() function. You can read more about that here : https://rtask.thinkr.fr/blog/shinyapp-runapp-shinyappdir-difference/

Let me know if this answers your questions :slight_smile:

Colin