-
Notifications
You must be signed in to change notification settings - Fork 977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.internal.selfref warning when using ..x notation #4876
Comments
Thank you for reporting. I tested solution in #4304 and it seems to address your use case well. lapply(d[, .(.k), env=list(.k=k)][, l], f)
lapply(d[, .k, env=list(.k=k)], f) Of course it does not qualify to close this issue. |
I don't quite get what you're trying to do. Your reprex still has the column name hard coded. And it actually doesn't do anything. I would argue that sticking Note that most of the ways you've tried actually don't do what I think you want it to do. The original lapply(d[, l], f)
d[1, l]
#[[1]]
# x t
#1: 1 1
lapply(d[, ..k][, l], f)
d[1, l]
#[[1]]
# x
#1: 1 You can currently use lapply(d[[k]], f)
d[1, l]
#[[1]]
# x t
#1: 1 1 I still maintain that this isn't a good idea and the fact that this does work might still be considered a bug. |
Well, in the sake of getting a reprex I boiled the code down to a pretty useless example, which, however, reproduced the error.
I was creating a (not so minimal) example which resembles a bit more the real use case and I saw the major flaw with my approach. As highlighted by @tlapak the code w/o the This whole exercise of creating a minimal reproducible example did definitely help to gain a better understanding of how and when data is copied in However, I tend to disagree that a Thanks for the fruitful discussion. I agree that this is not a bug, but by design and due to my not so complete understanding of how the internals of library(shiny)
library(data.table)
library(DT)
library(purrr)
dt_module_ui <- function(id, some_ui_param) {
ns <- NS(id)
tagList(h3(some_ui_param),
actionButton(ns("add"), "Add random column"),
actionButton(ns("reset"), "Reset to Default"),
DT::dataTableOutput(ns("dat")))
}
dt_module_server <- function(id, default) {
moduleServer(
id,
function(input, output, session) {
my_dat <- reactiveValues(data = NULL, refresh = 0)
observeEvent(input$reset, {
## we definitely should create a copy of the data.table
## this avoids the warning alltogether
my_dat$data <- copy(default)
}, ignoreNULL = FALSE)
observeEvent(input$add, {
## need the refresh trigger b/c shiny would not detect by ref changes
my_dat$refresh <- my_dat$refresh + 1
new_col <- paste0("col_", input$add)
my_dat$data[, (new_col) := runif(.N)]
})
output$dat <- DT::renderDataTable({
my_dat$refresh
datatable(my_dat$data, options = list(dom = "t"), class = "compact display")
})
}
)
}
my_module_params <- data.table(id = paste0("dat", 1:3),
some_ui_param = paste("Data Example", 1:3),
default = list(
data.table(x = 1:10, src = "Data 1"),
data.table(x = 11:20, src = "Data 2"),
data.table(x = 21:30, src = "Data 3")
))
args_ui <- intersect(formalArgs(dt_module_ui),
names(my_module_params))
args_server <- intersect(formalArgs(dt_module_server),
names(my_module_params))
ui <- fluidPage(
pmap(my_module_params[, ..args_ui], dt_module_ui)
)
server <- function(input, output, session) {
module_handlers <- pmap(my_module_params[, ..args_server], dt_module_server)
}
shinyApp(ui, server) |
Issue
Consider the following
reprex
:will raise the following warning
Same results for
while the following code does not issue a warning but has the severe consequence that I have to hard code the column names:
Use Case
In my real code I want to use
purrr::pmap
on adata.table
, but thedata.table
contains more columns than the function has arguments. I could add...
to capture the unnecessary columns (which I will do as a workaround now). But my attempt to filter thedata.table
via..necessary_cols
upfront, failed and it took me frankly quite a while to hunt the error down.Thus, overall I am looking for a solution where I can select a dynamic subset of columns (
..x
would do the trick but does throw the warning) in conjunction with alapply
alike function.Session Info
The text was updated successfully, but these errors were encountered: