कुछ सरल आदेशों के लिए कोष्ठक का उपयोग करने से बचने के लिए मेरी निरंतर खोज में, मैंने एक नई ग्राफिक्स विंडो बनाने के लिए निम्न ऑपरेटर लिखा है। मेरा सवाल है: क्या मुझे अपने चर "newdev" पर "नहीं" फ़ंक्शन निष्पादित करने में स्पष्ट अक्षमता के अलावा, आर में कुछ भी "तोड़ने" का खतरा है?आर यूनरी ऑपरेटर अधिभार: जोखिम?
# function to overload "!" for one purpose only
#this is adapted from the sos package code for "???", credited to Duncan Murdoch.
# Example of how to create a specialized unary operator that doesn't require
# parentheses for its argument. So far as I can tell,
#the only way to do this is to overload an existing function or
# operator which doesn't require parentheses. "?" and "!" meet this requirement.
`!` <- function (e1, e2) {
call <- match.call()
# match.call breaks out each callable function in argument list (which was "??foo" for the sos package "???",
# which allows topicExpr1 to become a list variable w/ callable function "!" (or "?" in sos)
original <- function() {
call[[1]]<-quote(base::`!`)
return(eval(call, parent.frame(2)))
}
# this does preclude my ever having an actual
# variable called "newdev" (or at least trying to create the actual NOT of it)
if(call[[2]] =='newdev') {
windows(4.5,4.5,restoreConsole=T)
}else{
return(original()) # do what "!" is supposed to do
}
}
क्या आप सीधे इन कमांड प्रॉम्प्ट का उपयोग कमांड प्रॉम्प्ट पर कर रहे हैं (यानी, उन्हें फ़ंक्शंस या व्हाट्नॉट में एम्बेड नहीं करना)? यदि ऐसा है, तो आप एक नई कक्षा बना सकते हैं और उस वर्ग के लिए एक प्रिंट विधि परिभाषित कर सकते हैं जो आप चाहते थे। – BenBarnes
मुझे लगता है कि एक अच्छा मौका है कि मैं इन आदेशों को किसी फ़ंक्शन के अंदर कॉल करूंगा, अगर मैं चार्ट का संग्रह बनाना चाहता हूं, उदा। –