gpt4 book ai didi

r - 未提供错误行号时如何调试?

转载 作者:行者123 更新时间:2023-12-01 23:33:14 40 4
gpt4 key购买 nike

我正在使用 shinyshinydashboard 创建仪表板。最小示例代码如下:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
dashboardHeader(title = "test"),
dashboardSidebar(
sidebarMenu(
menuItem(text = "Tab One",tabName = "tab1"),
menuItem(text = "Tab Two",tabName = "tab2"),
id = "sidebar"), # an extra comma here!
),
dashboardBody()
)


server <- function(input,output){}


shinyApp(ui,server)

当我运行此应用程序时,出现错误消息:

Error in tag("section", list(...)) : argument is missing, with no default

我知道我收到此错误是因为第 10 行末尾有一个额外的逗号。但问题是:

我的应用程序中也有类似的错误,但该应用程序包含 20 多个不同的互相引用的 R 文件以及 2000 多行代码。我不可能检查每个文件并尝试找出在哪里放置了额外的逗号。

我的问题是:

是否有更简单的方法让 R 打印带有行号和文件源的错误消息?或者是否有更好的方法来调试此类未提供详细信息的错误?谢谢!

<小时/>

理想情况下,我希望错误消息与此类似:

Error in source: <folder>/<file.R> 9:10: argument is missing, with no default
9: menuItem(text = "Tab Two",tabName = "tab2"),
10: id = "sidebar"), # an extra comma here!
^

最佳答案

获取主文件而不是运行它,并在错误发生后运行

traceback()

在控制台中。当我用你的简单例子来做到这一点时,我看到了这个:

> traceback()
11: tag("section", list(...))
10: tags$section(id = "sidebarItemExpanded", class = "sidebar", `data-disable` = if (disable) 1 else NULL,
list(...))
9: tag("aside", list(...))
8: tags$aside(id = "sidebarCollapsed", class = "main-sidebar", `data-collapsed` = dataValueString,
custom_css, tags$section(id = "sidebarItemExpanded", class = "sidebar",
`data-disable` = if (disable) 1 else NULL, list(...)))
7: dashboardSidebar(sidebarMenu(menuItem(text = "Tab One", tabName = "tab1"),
menuItem(text = "Tab Two", tabName = "tab2"), id = "sidebar"),
)
6: tagAssert(sidebar, type = "aside", class = "main-sidebar")
5: dashboardPage(dashboardHeader(title = "test"), dashboardSidebar(sidebarMenu(menuItem(text = "Tab One",
tabName = "tab1"), menuItem(text = "Tab Two", tabName = "tab2"),
id = "sidebar"), ), dashboardBody()) at .active-rstudio-document#4
4: eval(ei, envir)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("~/.active-rstudio-document", echo = TRUE)

请注意,标记为 5: 的表达式具有行号信息:.active-rstudio-document#4。这是 RStudio 在获取之前保存的文件,#4 部分表示问题出在第 4 行。第 4 行是对 dashboardPage 的重要调用。除非您将代码分解为更小的表达式,否则您不会获得比这更精细的细节。这会感觉非常不自然,希望没有必要,但您可以将原始写为

library(shiny)
library(shinydashboard)

header <- dashboardHeader(title = "test")
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem(text = "Tab One",tabName = "tab1"),
menuItem(text = "Tab Two",tabName = "tab2"),
id = "sidebar"), # an extra comma here!
)
body <- dashboardBody()
ui <- dashboardPage(
header,
sidebar,
body
)

server <- function(input,output){}

shinyApp(ui,server)

当我这样做时,traceback() 包含这一行:

5: dashboardSidebar(sidebarMenu(menuItem(text = "Tab One", tabName = "tab1"), 
menuItem(text = "Tab Two", tabName = "tab2"), id = "sidebar"),
) at .active-rstudio-document#5

它告诉我问题来自哪里。

编辑添加:查看回溯的另一种方法是运行

options(error = recover)

在采购所有东西之前。运行将终止,并显示类似 traceback() 显示的内容,但格式不同。它还允许您检查每个评估框架中的变量,这在此示例中可能没有帮助,但有时非常有用。

关于r - 未提供错误行号时如何调试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55975949/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com