आप commandArgs
फ़ंक्शन का उपयोग उन सभी विकल्पों को प्राप्त करने के लिए कर सकते हैं जो वास्तविक आर इंटरप्रेटर के लिए रुपयेस्क्रिप्ट द्वारा पारित किए गए थे और उन्हें खोज रहे थे --file=
। यदि आपकी स्क्रिप्ट पथ से लॉन्च की गई थी या यदि इसे पूर्ण पथ के साथ लॉन्च किया गया था, तो script.name
नीचे एक के साथ शुरू होगा '/'
। अन्यथा, यह सापेक्ष होना चाहिएcwd
और पूर्ण पथ प्राप्त करने के लिए आप दो रास्तों को प्राप्त कर सकते हैं।
संपादित करें: ऐसा लगता है कि आपको केवल script.name
ऊपर और पथ के अंतिम घटक को बंद करने की आवश्यकता होगी । मैंने अनावश्यक cwd()
नमूने को हटा दिया है और मुख्य स्क्रिप्ट को साफ कर दिया है और अपनी पोस्ट की है other.R
। बस इस स्क्रिप्ट और other.R
स्क्रिप्ट को एक ही डायरेक्टरी में chmod +x
सेव करें, उन्हें, और मुख्य स्क्रिप्ट को रन करें।
मुख्य रु ।:
#!/usr/bin/env Rscript
initial.options <- commandArgs(trailingOnly = FALSE)
file.arg.name <- "--file="
script.name <- sub(file.arg.name, "", initial.options[grep(file.arg.name, initial.options)])
script.basename <- dirname(script.name)
other.name <- file.path(script.basename, "other.R")
print(paste("Sourcing",other.name,"from",script.name))
source(other.name)
अन्य ।:
print("hello")
आउटपुट :
burner@firefighter:~$ main.R
[1] "Sourcing /home/burner/bin/other.R from /home/burner/bin/main.R"
[1] "hello"
burner@firefighter:~$ bin/main.R
[1] "Sourcing bin/other.R from bin/main.R"
[1] "hello"
burner@firefighter:~$ cd bin
burner@firefighter:~/bin$ main.R
[1] "Sourcing ./other.R from ./main.R"
[1] "hello"
मेरा मानना है कि डेहमान की तलाश है।