在 R
中工作时,我通常将函数存储在文件夹 ./R/
中
为了将这些函数带到工作区,我求助于 here::here()
函数。对于一个小脚本,我的代码将像这样开始:
library(here)
source(here::here("R", "custom_function1.R"))
source(here::here("R", "custom_function2.R"))
以这种方式在同事之间共享项目非常简单。
我正在为一个项目使用 Azure-Databricks Notebooks
。当使用 here::here()
时,函数指向服务器。
有没有办法在 Azure-Databricks Notebooks
中定义相对路径,以保持功能与 Notebook 本身分离?
最佳答案
如果我没看错你的问题,你可以使用相对路径。
警告:我不确定您是否正在寻找与 here
等价的东西图书馆。我还没有看到与之等效的东西,但相对路径确实有效。
参见:
https://docs.azuredatabricks.net/user-guide/notebooks/notebook-use.html#link-to-other-notebooks
从另一个笔记本运行一个笔记本
您可以使用 %run <notebook>
从另一个笔记本运行笔记本魔法命令。这大致相当于本地计算机上 Scala REPL 中的 :load 命令或 Python 中的 import 语句。中定义的所有变量都将在您当前的笔记本中可用。
%run 必须单独位于一个单元格中,因为它以内联方式运行整个笔记本。
Note You cannot use %run to run a Python file and import the entities defined in that file into a notebook. To import from a Python file you must package the file into a Python library, create an Azure Databricks library from that Python library, and install the library into the cluster you use to run your notebook.
例子
假设您有 notebookA 和 notebookB。 notebookA 包含一个具有以下 Python 代码的单元格:
x = 5
即使您没有在 notebookB 中定义 x,您也可以在运行 %run notebookA 后访问 notebookB 中的 x。
%run /Users/path/to/notebookA
print(x) # => 5
相对路径
要指定相对路径,请在其前面加上 ./或 ../。例如,如果 notebookA 和 notebookB 在同一目录中,您也可以从相对路径运行它们。
%run ./notebookA
print(x) # => 5
因此
%run ../someDirectory/notebookA # up a directory and into another
print(x) # => 5
有关笔记本之间更复杂的交互,请参阅 Notebook Workflows .
关于r - 如何在 Databricks 中使用 R Notebook 的相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56889000/