R解析器为何将do.call参数中的knitr::kable解析为SYMBOL而非SYMBOL_FUNCTION_CALL?
knitr::kable is Parsed as SYMBOL Instead of SYMBOL_FUNCTION_CALL in do.call? Great question! Let’s break down exactly why R’s parser treats kable this way:
Syntactic context determines token type
R’s parser assigns token types based on how code is structured, not what the code does at runtime. TheSYMBOL_FUNCTION_CALLtoken is only used for symbols that are directly invoked as functions. For example, in your code,do.callgets this token because it’s the function being called right there.knitr::kableis an argument, not a direct call
When you passknitr::kableas the first argument todo.call, the parser sees it as a nested expression (made up ofknitr(SYMBOL_PACKAGE),::(NS_GET), andkable(SYMBOL)), not a function being executed. Sincekableisn’t the starting point of a function call in this context, it doesn’t get theSYMBOL_FUNCTION_CALLlabel.Even direct
pkg::fun()uses different tokens
As a quick side note: if you wroteknitr::kable(my_data),kablestill wouldn’t be marked asSYMBOL_FUNCTION_CALL. That’s becausepkg::funis syntactic sugar for calling the::function (yes,::is a real function!). In that case,::would be theSYMBOL_FUNCTION_CALL, withknitrandkableas itsSYMBOLarguments.
Looking at your parsed output confirms this behavior:
- The
do.calltoken (line 1) is correctly tagged asSYMBOL_FUNCTION_CALL—it’s the function being invoked. - The
knitr::kableexpression (lines 4-6) is split into three separate tokens, withkableas a plainSYMBOLbecause it’s not the function being called in this syntactic position.
To sum it up: the parser doesn’t check if a symbol refers to a function at runtime—it only cares about where the symbol appears in the code’s structure. That’s why kable gets the SYMBOL tag here instead of SYMBOL_FUNCTION_CALL.
内容的提问来源于stack exchange,提问作者yonicd

