是的,我知道R项目不适合在网络驱动器中创建,我一定会因为下面描述的这个问题而学习如何正确使用Git。但是避免问题本身很容易;我想知道它的原因,如果可能的话,为什么一起使用 RStudio 和 Google Drive Desktop 是危险的。
在 R 4.3.0 中,为本地管道 (
|>
) 占位符 (_
) 添加了“实验性功能”:
作为一项实验性功能,占位符 _ 现在也可以用在正向管道 |> 表达式的右侧,作为提取调用中的第一个参数,例如 _$coef。更一般地,它可以用作提取链的头部,例如 _$coef[[2]]。 (链接)
除非在闪亮的应用程序中使用,否则此语法运行良好。下面是一个没有实际用途的示例代码:
library(shiny)
ui <- fluidPage(
selectizeInput('col', 'Select column to view', choices = names(mtcars)),
tableOutput('table')
)
server <- function (input, output, session) {
output$table <- renderTable(mtcars |> _[[input$col]][1:10])
}
shinyApp(ui = ui, server = server)
通过单击 RStudio 中的“运行应用程序”启动应用程序,然后将焦点更改回 RStudio,应用程序将立即关闭 (*)。在位于网络驱动器(e.g.,在我的例子中是 Google 驱动器桌面)中的 R 项目中尝试相同的操作,可以观察到其他异常情况:单击“运行应用程序”可能导致应用程序成功启动,但没有任何反应,或出现无响应的灰色窗口。
这种现象不会发生...
mtcars[[input$col]]
,另外,如上所述,当应用程序位于本地文件夹时,只会出现现象(*),看不到更奇特的异常。
避免问题很容易;我只需要停止使用 RStudio 访问远程项目,或者简单地解释相关代码部分。但我想知道到底发生了什么。
我听说将 RStudio 与 Google Drive Desktop 一起使用是不好的做法,但一直不明白为什么;到目前为止,我在以这种方式工作时没有遇到任何困难。 Shiny 和新的占位符语法的特殊组合肯定会突出 RStudio、Google Drive Desktop 之间的一些不兼容性。我想知道它是什么。
此外,即使在本地项目中也可以重现现象(*)。虽然无伤大雅,但肯定不一般。所以我怀疑使用远程 RStudio 项目并不是问题的全部原因,Shiny 和新的占位符语法之间存在一些真正的不匹配。
问题不依赖于正在执行的相关代码。将以下代码放在上面示例的
renderTable()
中会产生相同的现象。
if (TRUE) {
mtcars[[input$col]][1:10]
} else {
mtcars |> _[[input$col]][1:10]
}
现象变化极大。我试过
set.seed()
但按下“运行应用程序”的结果似乎并不取决于种子。经过多次迭代,我设法哄出以下错误信息,但无法理解其含义。
Error in base::suppressWarnings(base::try("_"[[input$col]], silent = TRUE)) :
invalid use of pipe placeholder (<input>:1:0)
Warning: stack imbalance in '<-', 37 then 32
Warning: stack imbalance in '{', 33 then 28
Listening on http://127.0.0.1:6972
> sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)
Matrix products: default
locale:
[1] LC_COLLATE=Korean_Korea.utf8 LC_CTYPE=Korean_Korea.utf8 LC_MONETARY=Korean_Korea.utf8
[4] LC_NUMERIC=C LC_TIME=Korean_Korea.utf8
time zone: Asia/Seoul
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_1.7.4
loaded via a namespace (and not attached):
[1] digest_0.6.31 later_1.3.1 R6_2.5.1 httpuv_1.6.11 fastmap_1.1.1 magrittr_2.0.3
[7] cachem_1.0.8 memoise_2.0.1 htmltools_0.5.5 lifecycle_1.0.3 promises_1.2.0.1 cli_3.6.1
[13] xtable_1.8-4 sass_0.4.6 jquerylib_0.1.4 withr_2.5.0 compiler_4.3.0 rstudioapi_0.14
[19] tools_4.3.0 bslib_0.4.2 ellipsis_0.3.2 mime_0.12 Rcpp_1.0.10 jsonlite_1.8.4
[25] rlang_1.1.1