呈现包含 sparklyr
代码的 R Markdown 文件时失败Rendering an R markdown file containing sparklyr
code fails
问题Problem
在 Azure Databricks 环境中安装并配置好 RStudio 之后,启动 RStudio 并单击“编织”按钮以编织一个包含 sparklyr
上下文初始化代码的 Markdown 文件时,呈现失败并出现以下错误:After you install and configure RStudio in the Azure Databricks environment, when you launch RStudio and click the Knit button to knit a Markdown file that contains code to initialize a sparklyr
context, rendering fails with the following error:
failed to start sparklyr backend:object 'DATABRICKS_GUID' not found Calls: <Anonymous>… tryCatch -> tryCatchList-> tryCatchOne -> <Anonymous> Execution halted
原因Cause
如果尝试使用类似以下内容的代码在 Markdown 笔记本中初始化 sparklyr
上下文,则 Markdown 页将无法呈现,因为 knitr
进程会生成缺少 'DATABRICKS_GUID'
全局变量的新命名空间。If you try to initialize a sparklyr
context in a Markdown notebook with code similar to the following, the Markdown page fails to render because the knitr
process spawns a new namespace that is missing the 'DATABRICKS_GUID'
global variable.
case library(sparklyr)
sc <- spark_connect(method = "databricks")
解决方案Solution
请使用以下脚本来呈现 Markdown 页,而不是单击 R Markdown 控制台中的“编织”按钮:Instead of rendering the Markdown page by clicking the Knit button in the R Markdown console, use the following script:
rmarkdown::render("your_doc.Rmd")
使用 R 控制台呈现 Markdown 文件,然后就可以在 RStudio“文件”选项卡中访问该文件。Render the Markdown file using the R console, and then you can access the file in the RStudio Files tab.