0

I want to pass a predefined but empty data.frame to shiny/DT. Are there good practices?

What I found out is that

x <- data.frame(a=NA, b=NA)[!is.na(x$a),]
str(x)

produces the desired result.

'data.frame':   0 obs. of  2 variables:
 $ a: logi 
 $ b: logi 

But it looks clumsy. Is there a smarter way?

Karsten W.
  • 17,826
  • 11
  • 69
  • 103
  • 1
    `data.frame(a=NA,b=NA)[0,]`? BTW, your `[!is.na(x$a),]` pre-supposes that you created `x <- data.frame(..)` and after that assignment, the next expression references that. In other words, `x` does not exist in your expression above when that `[..]` is evaluated. – r2evans Dec 03 '21 at 12:58
  • So why does it work? – Karsten W. Dec 03 '21 at 17:16
  • 1
    Eval `rm(x)`, then run `x <- data.frame(a=NA, b=NA)[!is.na(x$a),]` again, you should receive the error `object 'x' not found`. – r2evans Dec 03 '21 at 17:37

0 Answers0