shiny-server:变量名限制在 10000 字节以内

shiny-server: variable names are limited to 10000 bytes

我在 运行 仅闪亮服务器中的代码时遇到错误。当我使用 runApp() 函数 运行 来自 R 控制台的相同代码时,它 运行 很好。请参阅下面的错误消息....

Warning: Error in assign: variable names are limited to 10000 bytes
Stack trace (innermost first):
    46: assign
    45: wrapFunctionLabel
    44: public_bind_env$initialize
    43: Observable$new
    42: reactive
    41: templateServer
    40: server [/home/shiny-apps/ACCPlantAnalysis/server.R#20]
     1: runApp
Error in assign(name, func, environment()) : 
  variable names are limited to 10000 bytes

第41、40行是我写的。但其他几行不是我写的;从任何参考图书馆调用。不知道是哪个图书馆的。

根据 ?name,您必须尊重以下事实:

Names are limited to 10,000 bytes (and were to 256 bytes in versions of R before 2.13.0).

并且规则在 ?make.names 中说:

A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as ".2way" are not valid, and neither are the reserved words.

经过一天的研究,我能够查明问题所在。我的代码中有一个很长的 if..else 块。有 10 个 if 块,每个 if 块至少有 6-8 行代码。当我评论一个 'if' 代码块(随机选择)时,代码 运行 非常棒......下面是我的 'if' 代码块......

  else if (selectedIndex=="Sectoral Leverage Ratio"){
        ## Calculated number of row of the matrix ##
        rowNum <- selectedMonth[2] - selectedMonth[1] + 1

        ## Filter data based on criteria ##
        filteredData <- subset(rawData,Year %in% selectedYear & Month %in% selectedMonth[1]:selectedMonth[2] & Plant %in% selectedPlant)

        LeverageTable <- filteredData[,c('sch1ExpLeverage','sch2ExpLeverage','sch3ExpLeverage','sch4ExpLeverage','sch5ExpLeverage','sch7ExpLeverage','sch10ExpLeverage','sch11ExpLeverage')]
        LeverageSum <- apply(LeverageTable,2,sum)

        ExpTable <- filteredData[,c('sch1Exp','sch2Exp','sch3Exp','sch4Exp','sch5Exp','sch7Exp','sch10Exp','sch11Exp')]
        ExpSum <- apply(ExpTable,2,sum)


        LeverageRatio <- as.matrix(LeverageSum / ExpSum)
        AvgLeverageRatio <- as.matrix((LeverageSum / ExpSum)/rowNum)

        LeverageRatioTable <- cbind(LeverageRatio,AvgLeverageRatio)
        colnames(LeverageRatioTable) <- c('Leverage Ratio','Avg.Leverage Ratio')


        LeverageRatioTable <- data.frame(Sector=c('Health & Sant.','Edn. & Voc.','Social Welfare','Envt. Sust.','Hrtg & Arts','Sports','Rural Dvpt.','Admin Cost'),LeverageRatioTable)

        as.data.frame(LeverageRatioTable)

  }

所有其他 'if' 块几乎相似(计算上有些差异)。

这是shiny-server的问题;不是'R'本身的问题。

如果有人想检查我的代码;我可以分享完整的代码。

终于找到解决办法了。我为每个 'if' 块编写函数,并将 'if' 块的所有代码放入函数中。然后调用函数。问题解决了。

SectoralLeverageRatio(){
...
...
}

if (selectedIndex=="Sectoral Leverage Ratio"){
  SectoralLeverageRatio()
}

结论是...如果您的 'if' 块足够大并且出现 'variable names are limited to 10000 bytes' 错误。用用户定义的函数替换 'if' 块中的代码。

这是shiny-server的问题;不是 'R' 的问题。如果您使用 运行App() 从 'R' 控制台 运行 您的代码,您将不会收到任何错误。但是如果你 运行 通过(生产环境)你可能会得到一个错误。