没有提供错误行号如何调试?
How to Debug when the Line Number of Error is not Provided?
我正在使用 shiny
和 shinydashboard
创建仪表板。最小示例代码如下:
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)
当我运行这个App的时候,有一个错误信息:
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()
那样显示,但格式不同。它还允许您检查每个评估框架中的变量,这在本示例中可能没有帮助,但有时非常有帮助。
您可以改用 source()
:
(例如,我修改了您的代码,将错误的 ui 放入一个额外的文件 uiFile.R
。
然后激活回溯(见右图:Show Traceback
)。
然后你会看到:
它不是最佳选择,因为它没有显示第 10 行,但您至少可以单击蓝色突出显示的输出并导航到错误。
有关如何调试 shiny 应用程序的更多技术,此页面非常有帮助:https://shiny.rstudio.com/articles/debugging.html。
我正在使用 shiny
和 shinydashboard
创建仪表板。最小示例代码如下:
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)
当我运行这个App的时候,有一个错误信息:
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()
那样显示,但格式不同。它还允许您检查每个评估框架中的变量,这在本示例中可能没有帮助,但有时非常有帮助。
您可以改用 source()
:
(例如,我修改了您的代码,将错误的 ui 放入一个额外的文件 uiFile.R
。
然后激活回溯(见右图:Show Traceback
)。
然后你会看到:
它不是最佳选择,因为它没有显示第 10 行,但您至少可以单击蓝色突出显示的输出并导航到错误。
有关如何调试 shiny 应用程序的更多技术,此页面非常有帮助:https://shiny.rstudio.com/articles/debugging.html。