Home > Net >  Play Sound If Condition is Met
Play Sound If Condition is Met

Time:09-28

I installed beepr library and I need to run beep() only if condition is met, example:

library(beepr)

test=data.frame(a=1,b=2)
ifelse(test$b==2,beep(1),beep(2))

Error in rep(yes, length.out = len) : attempt to replicate an object of type 'externalptr'

CodePudding user response:

When using ifelse sound is generated along with the error. The issue with ifelse is that it returns the data of same type as the test value.

From ?ifelse -

A vector of the same length and attributes (including dimensions and "class") as test.

Since beep returns an output different than the test it returns an error.

Use if/else -

library(beepr)
test=data.frame(a=1,b=2)
if(test$b==2) beep(1) else beep(2)

CodePudding user response:

ifelse is not good here, it modified each value, but it needs to retain the same data type.

There for use ifelse:

if(2 %in% test$b) beep(1) else beep(2)

I used in since you probably need in here.

  •  Tags:  
  • r
  • Related