最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - How to Properly Use Internal Data from Another Package in a Golem Shiny App? - Stack Overflow

programmeradmin0浏览0评论

I am currently developing a Golem Shiny app. Since it relies on a massive amount of raw data that needs transformation before use, I have separated this part of the workflow into a dedicated package for data preparation.

The preparation package is almost complete, and I can export all transformed data as a data.rda file, which contains everything required by the Shiny app.

I have read Section 7.2 (Internal Data) from R Packages (2e) by Hadley Wickham many times. While I am comfortable using usethis::use_data(internal_this, internal_that, internal = TRUE) within a single package, I am unsure how to handle a situation like mine—where the data.rda file comes from another package but is meant to be used internally in the Shiny app package, and the shiny package itself also have an internal .rda for intra-package purposes.

My current approach is:

  1. Migrate the data.rda file to the R/ folder of the Shiny app package.
  2. Create a Load_data.R file that contains only one line of code load(file = file.path("R", "data.rda")) to load objects from data.rda.

This seems to work, as the app can access the data. However, is this the correct approach? If not, what would be a more robust solution?

Any comment would be greatly appreciated!

I am currently developing a Golem Shiny app. Since it relies on a massive amount of raw data that needs transformation before use, I have separated this part of the workflow into a dedicated package for data preparation.

The preparation package is almost complete, and I can export all transformed data as a data.rda file, which contains everything required by the Shiny app.

I have read Section 7.2 (Internal Data) from R Packages (2e) by Hadley Wickham many times. While I am comfortable using usethis::use_data(internal_this, internal_that, internal = TRUE) within a single package, I am unsure how to handle a situation like mine—where the data.rda file comes from another package but is meant to be used internally in the Shiny app package, and the shiny package itself also have an internal .rda for intra-package purposes.

My current approach is:

  1. Migrate the data.rda file to the R/ folder of the Shiny app package.
  2. Create a Load_data.R file that contains only one line of code load(file = file.path("R", "data.rda")) to load objects from data.rda.

This seems to work, as the app can access the data. However, is this the correct approach? If not, what would be a more robust solution?

Any comment would be greatly appreciated!

Share Improve this question edited Mar 12 at 10:02 Grasshopper_NZ asked Mar 12 at 6:35 Grasshopper_NZGrasshopper_NZ 9195 silver badges15 bronze badges 11
  • if you have data in one package that is intended to be used by your golem shiny app, why not properly export that data in the first place? you can then do firstpkg::my_dataset without anything fancy like trying to export sysdata.rda internal-only data – r2evans Commented Mar 12 at 12:32
  • lacking that, instead of writing a function that calls load(.), why not just have it return the internal data itself? inside the data-package, the "internal data" is fully exposed anyway ... though this is about the same amount of work as my first comment but without the side-step of "this data is internal, but I'll export it another way". – r2evans Commented Mar 12 at 12:34
  • Thanks for your input! The R preparation package is built for internal use only, not for public release or CRAN. The data is meant to power the Golem Shiny app’s functionality and need to remain invisible and inaccessible outside the app’s internal logic. That’s why I’m leaning toward sysdata.rda—it keeps the data hidden within the package’s environment. Exporting it as firstpkg::my_dataset would expose it in the public namespace, which I’m trying to avoid. – Grasshopper_NZ Commented Mar 12 at 12:44
  • I understand. If the package is never to be published, then why are you splitting hairs on whether the data is exported properly (section 7.1) or held quasi-privately (section 7.2)? It is far simpler for writing and maintaining packages when you adhere to the intended mechanisms instead of trying to dance around them by writing an accessor function that sneaks around the "internalness" of the data and exports it. – r2evans Commented Mar 12 at 12:45
  • Said differently: I also maintain a number of packages that are never for public use, not on CRAN (proprietary, blah blah blah). Some of them have datasets. I started with the intention of keeping some of their datasets as "internal" (sysdata.rda), but maintenance and devtools::, and trying to dance around other internal-packages that really would benefit from the data ... I found that trying to do the dance you're doing was not worth the effort, and introduced more fragile components. My suggestion: if the package is already private, just export data the usual way. – r2evans Commented Mar 12 at 12:48
 |  Show 6 more comments

1 Answer 1

Reset to default 1

According to Writing R Extensions, section 1.1.5:

The R subdirectory contains R code files, only.

There's an exception for sysdata.rda, and .in files, but nothing else. That would indicate that using R/data.rda would not be acceptable.

Instead, you could export the prepared data (or the data preparation function) from your private data preparation package, and bake the results into sysdata.rda for your primary package.

For reproducibility, write a script for those steps. In data-raw/sysdata.R:

internal_this <- ...
internal_that <- ...

data("prepared_data", package = "rpackageforpreparingdata")
# Or: prepared_data <- rpackageforpreparingdata::prepare_data()

usethis::use_data(internal_this, internal_that, prepared_data, internal = TRUE)
发布评论

评论列表(0)

  1. 暂无评论