2

次の形式のソース ファイルを考えてみましょう。

# initialize the function
f = function() {
 # ...
}

# call the function
f()

Python では、import関数はファイルをロードして実行します。ただし、R では、sourceコマンドソース ファイルで定義された関数を初期化しますが、ファイルで関数が呼び出された場合は関数を呼び出しません。

ファイルをインポートして実行するR コマンド (またはオプション)はありますか?

ご協力いただきありがとうございます。

4

1 に答える 1

7

?source状態:

Description:

     ‘source’ causes R to accept its input from the named file or URL
     or connection.  Input is read and ‘parse’d from that file until
     the end of the file is reached, then the parsed expressions are
     evaluated sequentially in the chosen environment.

したがって、

  1. source あなたが探している機能です
  2. 私はあなたの主張に反論します -sourceドキュメントに従って私のために働きます
  3. 文書化された動作が見られない場合は、報告されていない別の問題があるはずです。

    それがあなたのをsource実行していないことをどのように推測していますfか?

あなたが気付いていないかもしれない 1 つのシナリオを考えることができます。これは に記載されてい?sourceます。

Details:

     Note that running code via ‘source’ differs in a few respects from
     entering it at the R command line.  Since expressions are not
     executed at the top level, auto-printing is not done.  So you will
     need to include explicit ‘print’ calls for things you want to be
     printed (and remember that this includes plotting by ‘lattice’,
     FAQ Q7.22).

名前で呼び出されたオブジェクトからの出力が表示されない、またはグリッド グラフィックス ベースのプロットが表示されない場合は、評価が最上位で行われていないため、自動印刷がアクティブになっていないことを示しています。print()Lattice や ggplot2 の図などのグリッドベースのグラフィックスを含め、明示的にオブジェクトを作成する必要があります。

print.eval引数にも注意してください。デフォルトは

> getOption("verbose")
[1] FALSE

これはあなたにも役立つかもしれません。

于 2013-04-03T20:40:26.217 に答える