如何确定哪个R包包含一个函数?

时间:2016-03-09 09:04:48

标签: r

在Python中,可以编写

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:text="New Text" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:text="New Text" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="New Text" />

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="New Text" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"

                android:gravity="center"
                android:text="New Text" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="New Text" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

要明确函数random.randn来自numpy。

当我从网上下载R代码时,例如下面的代码,我想知道从每个库导入哪些功能。有没有办法做到这一点,除了评论每个&#34;库&#34;声明并查看代码中断的位置?

import numpy as np
print np.random.randn(10)

1 个答案:

答案 0 :(得分:5)

如果您的搜索路径中没有包,则可以使用一些有用的功能来查找getAnywheresos包之类的对象。此外,可以使用解析数据尝试以编程方式执行,但它不会获得所有内容,例如在函数作为字符串或其他奇怪情况传递的情况下。无论如何,如果您将代码存储在&#34; test.R&#34;中,这是一个快速示例。 (正如@javK所说,需要加载包,因此getAnywhere可以在这个例子中找到它们。)

src <- parse(file="test.R", keep.source = TRUE)
tokens <- getParseData(src)
funs <- tokens[tokens$token == "SYMBOL_FUNCTION_CALL",]
res <- lapply(funs$text, function(x) getAnywhere(x)$where)

## Return a list of the functions along with the environment they in
## truncated...
setNames(res, funs$text)
# ...
# $c
# [1] "package:base"   "namespace:base"
# $approx_entropy
# [1] "package:pracma"   "namespace:pracma"
# $sd
# [1] "package:stats"   "namespace:stats"
# $rnorm
# [1] "package:stats"   "namespace:stats"
# ...
相关问题