Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improved discussion of source()#289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
hadley wants to merge1 commit intorstudio:master
base:master
Choose a base branch
Loading
fromhadley:patch-1
Open

Improved discussion of source()#289

hadley wants to merge1 commit intorstudio:masterfromhadley:patch-1

Conversation

@hadley
Copy link
Member

  • Don't mention sys.source() as people shouldn't be using it anyway
  • Uselocal = TRUE which is simpler and still correct
  • Precisely describe the problem caused the default behaviour ofsource()

* Don't mention sys.source() as people shouldn't be using it anyway* Use `local = TRUE` which is simpler and still correct* Precisely describe the problem caused the default behaviour of `source()`
Copy link
Contributor

@yihuiyihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

  • Don't mention sys.source() as people shouldn't be using it anyway

I don't understand that.

  • Uselocal = TRUE which is simpler and still correct

Simpler, true. Correct, I don't think it is guranteed.

  • Precisely describe the problem caused the default behaviour ofsource()

Yes, I agree this needs more clarification.

Thank you!


````md
```{r, include=FALSE}`r ''`
source("your-script.R", local = knitr::knit_global())
Copy link
Contributor

@yihuiyihuiOct 20, 2020
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

But this is more robust thanlocal = TRUE. It works anywhere (e.g. inside a function). It's an absolute environment, andlocal = TRUE is a relative environment, which is subject to your current environment, and could be confusing when it is not called directly in a code chunk but from another function, e.g.,

load_scripts<-function(files) {for (finfiles) source(f,local=TRUE)}load_scripts(c('a.R','b.R'))

Bothlocal = TRUE andFALSE have their usefulness. There isn't an absolutely correct choice, so I didn't strongly emphasize one or another. Perhaps the distinction between the two choices should be explained more clearly in this section. In general, I feelsource("your-script.R", local = knitr::knit_global()) is still the safest choice.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, if you want to runsource() inside a function and have it impact code outside of the function, you're going to have to do something special. As a general principle, I don't think functions should mutate the global environment in this way, so I do believe thatlocal = TRUE is the correct recommendation here. (And then mentionlocal = knitr::knitr_global() as a special case if youreally want to do that)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

But this is now really about advice for writing functions that call source, not about callingsource() in knitr chunks.

````

Werecommend that you use the argument`local` in`source()` or`envir` in`sys.source()`explicitly to make sure the codeisevaluated in the correct environment, i.e.,`knitr::knit_global()`\index{knitr!knit\_global()}. The default valuesforthem may not be the appropriate environment: you may end up creating variables in the wrong environment, and being surprised that certain objects are not found in later code chunks.
[^sys-source]:Wedon't recomend ever using`sys.source()`as itisdesigned specificallyforinternal use by base R.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We don't recommend ever usingsys.source() as it is designed specifically for internal use by base R.

I haven't heard of this before and am surprised to learn it. Could you point out the source of this claim? The help page?sys.source didn't say it was designed only for internal use by base R. I mentionedsys.source() because it does a much simpler job thansource(), i.e., it basically doeseval(parse()), whereassource() does several other things that are irrelevant to evaluating the code.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think it's implied by theenvir argument which defaults tobaseenv() — there is no case in which a user would want this. Do you have any evidence to suggest that people are usingsys.source() in the wild?

[^sys-source]:Wedon't recomend ever using`sys.source()`as itisdesigned specificallyforinternal use by base R.

Next intheR Markdown document, you can use objects createdinthese scripts (e.g., data objects or functions). This way willnot only make your R Markdown document cleaner, but also make it more convenient for you to develop R code (e.g., debugging R code is often easier with pure R scripts than R Markdown).
We recommend usingthe`local = TRUE` argument to`source()`. The default behaviour is to evaluate the codeinthe global environment, which can cause confusion. Usually, knitr willevaluate code in a child of the global environment, so any assignments in the source file will be overridden by assignments in**earlier** chunks.`local = TRUE` forces`source()` to evaluate in the correct environment, avoiding any problems.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Usually, knitr will evaluate code in a child of the global environment

Usuallyknitr evaluates code just in the global environment, instead of a child of the global environment.knitr::knit() andrmarkdown::render() haveenvir = parent.frame() by default, which points toglobalenv() if you call them in the global environment.

so any assignments in the source file will be overridden by assignments inearlier chunks

I'm confused: did you mean earlier or later? If you havex <- 1 in the source file, after yousource() it, the assignment will be overridden byx <- 2 in a later chunk. Similarly, if you havex <- 0 in an earlier chunk,source() will override it withx <- 1.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I read the following paragraph to imply that knitr usually evaluates in some special environment:

The default values for them may not be the appropriate environment: you may end up creating variables in the wrong environment, and being surprised that certain objects are not found in later code chunks.

If knitr usually evaluates in the global environment, then callingsource() with no arguments will usually be safe.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@yihuiyihuiyihui left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@hadley@yihui

[8]ページ先頭

©2009-2025 Movatter.jp