位于代码中的长代码行中意外的“{”与许多 if 和 if-else 结构混合在一起?

Unexpected "{" in a long code line that is located in a code with many if's and if-else structures are mixed together?

当尝试编写一个函数 fromNdpTo10 将标准化双精度数(在内存中)转换为十进制数时,我收到“unexpected '{' in:”错误,但我不应该收到该错误。我关心括号、大括号、方括号的开始和结束数量是否相等。看:

SubstringHoldingLeading0s <- function(x) { 
  x <- formatC(x, flag="0", width=11, format="d") 
  substr(x, 1, 11) 
}
SubstringHoldingLeading0s(00100101101) # "00100101101"

from2to10 <- function(binaryNumber) {
  # Via SubstringHoldingLeading0s, the loss is prevented when converting string (holded as numeric) to character
  sapply(strsplit(SubstringHoldingLeading0s(binaryNumber), split = ""), function(x) sum(as.numeric(x) * 2^(rev(seq_along(x) - 1))))}
from2to10(00100101101) # 301

fromNdpTo10 <- function(NdpNumber) {
  NdpNumber <- as.character(NdpNumber)
  out <- list()

  # Handle special cases (0, Inf, -Inf)
  If (NdpNumber %in% c(
    "0000000000000000000000000000000000000000000000000000000000000000",
    "0111111111110000000000000000000000000000000000000000000000000000",
    "1111111111110000000000000000000000000000000000000000000000000000")) {
    # special cases
    If (NdpNumber == "0000000000000000000000000000000000000000000000000000000000000000") { out <- "0" }
    If (NdpNumber == "0111111111110000000000000000000000000000000000000000000000000000") { out <- "Inf" }
    If (NdpNumber == "1111111111110000000000000000000000000000000000000000000000000000") { out <- "-Inf" }
  } else { # if NdpNumber not in special cases, begins

    signOfNumber <- "+" # initialization
    If (substr(NdpNumber, 1, 1) == 0) { signOfNumber <- "+"
    } else { signOfNumber <- "-" }

    # From BiasedExponent to RealExponent (Biased Exponent=Real exponent +1023;  Real exponent=Biased Exponent-1023)
    BiasedExponent <- substr(NdpNumber, 2, 12)
    BiasedExponent <- from2to10(BiasedExponent)
    RealExponent <- BiasedExponent - 1023


    # Significand
    Significand <- substr(NdpNumber, 13, 64)
    Significand <- from2to10(Significand)

    out <- paste0(c(signOfNumber, Significand, "e", RealExponent), collapse = '')
  } # if NdpNumber not in special cases, ends

  out
}

错误是:

Error: unexpected '{' in:
"        "0111111111110000000000000000000000000000000000000000000000000000",
        "1111111111110000000000000000000000000000000000000000000000000000")) {"

问题好像是单行代码太长导致的。单行字符有限制吗?知道如何解决这个问题吗?

这里有一个语法错误:

If (substr(NdpNumber, 1, 1) == 0) { signOfNumber <- "+"

'if' 以小写开头 'i'.

作为一条建议,请尽量遵循常见的编码风格约定(首字母小写的变量,...)