← Back to R Course | Chapter 12: Best Practices | Lesson 6 of 6

Source Files क्या हैं

source आपकी current session के अंदर एक दूसरी R file चलाता है, एक दूसरी notebook से एक page ऐसे पढ़ने की तरह जैसे यह आपकी अपनी हो। यह आपको बड़े projects को कई files में split करने देता है।

In this page:

  1. Source Files
Syntax
r
source("file.R")

Source Files

source("file.R") current session में एक दूसरी R script चलाता है, इसके functions और variables available बनाते हुए, एक module import करने जैसा।

यह आपको एक बड़े project को एक लंबी script की बजाय organized, reusable files में split करने देता है। Source files आमतौर पर helper functions को main analysis script से अलग रखने के लिए इस्तेमाल होती हैं।

Note: Reusable functions को एक separate helpers.R file में रखें और अपनी main analysis script से source("helpers.R") करें।

उदाहरण: Source Files

markup
# Simulating a sourced helper file inline for this example
helper_code <- 'square <- function(x) x^2\ncube <- function(x) x^3'
writeLines(helper_code, "helpers.R")

source("helpers.R")

cat("Square of 4:", square(4), "\n")
cat("Cube of 3:", cube(3), "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक अलग working directory से source("helpers.R") call करना, इसलिए R file नहीं ढूंढ पाता।
  2. ऐसी file source करना जो plots चलाती है या print करती है, जब output हर बार source होने पर appear होता है।
  3. यह उम्मीद करना कि variables separate रहेंगे, जब source() इन्हें global environment में डालता है और existing objects overwrite कर सकता है।
चैप्टर सारांश
  • Clean R code लिखना और loops की बजाय vectorization इस्तेमाल करना code को clearer और faster बनाता है।
  • is.na() handle करना और tryCatch() इस्तेमाल करना missing values और errors deal करते हैं।
  • which() और subset() के बीच चुनना और source files इस्तेमाल करना काम organize करने में मदद करते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.