【问题标题】:KableExtra and knitr: Error in parse(Text = x, srcfile = src): unexpected '='KableExtra 和 knitr:解析错误(文本 = x,srcfile = src):意外'='
【发布时间】:2018-09-26 02:06:06
【问题描述】:

我正在使用具有国家 (char)、指标 (char)、指标代码 (char)、时间段 (char: 例如 2010Q3、2010M12、2010) 和值 (num) 的平面数据集 (“FSIflat”) ) 和时间类型(“Quarterly”、“Monthly”或“Annual”),目标是在用户呼叫某个国家/地区时以 PDF 格式生成表格。我试图将一个块转换为一个函数但失败了。原始块工作正常,它产生 PDF。但是这个函数给了我Error in parse(Text = x, srcfile = src)::3:16: unexpected '='

我是 R 和一切的新手。我花了几天的时间,无法真正弄清楚出了什么问题。如果有人可以提供帮助,那就太好了。非常感谢!

newdf<-FSIflat%>%
  filter(Country.Name == "Australia", timetype == "Annual")%>%
  select(1:6,-2,-3)
All.Periods <- c(unique(newdf$Time.Period))
All.Periods <- tail(sort(All.Periods),10)
newdf<-newdf%>%
  filter(Time.Period %in% c(All.Periods))%>%
  spread(key=Time.Period, value=Value)%>%
  inner_join(indicatortable, by=("Indicator.Code"="Indicator.Code"))%>%
  arrange(Set, Level)%>%
  select(Set, Indicator, c(All.Periods))

kable(newdf, "latex", longtable = T, booktabs = T, align = c("c","l",rep("r",10)), caption="c(ftitle)", digits = 1)%>%
column_spec(1, width = "1em")%>%
column_spec(2,width = "16em")%>%
collapse_rows(columns = 1:2, latex_hline = "major", valign = "top", row_group_label_position = 'stack')%>%
kable_styling(latex_options = c("striped", "repeat_header"), full_width = T)

函数

<<LoadCountryx>>=
LoadCountryx<-function(
fcountry, fdata, ffrequency, ftitle = paste("Latest ratios available for ", 
fcountry)
){if(ffrequency="Y"){
    newdf<-fdata%>%
      filter(Country.Name == fcountry, timetype == "Annual")%>%
      select(1:6,-2,-3)
    All.Periods <- c(unique(fdata$Time.Period))
    All.Periods <- tail(sort(All.Periods),10)
newdf<-newdf%>%
  filter(Time.Period %in% c(All.Periods))%>%
  spread(key=Time.Period, value=Value)%>%
  inner_join(indicatortable, by=("Indicator.Code" = "Indicator.Code"))%>%
  arrange(Set, Level)%>%
  select(Set, Indicator, c(All.Periods))

}else if(ffrequency="Q"){
    newdf<-filter(fdata,Country.Name == fcountry, timetype == "Quarterly")%>%
    select(1:6,-2,-3)
  All.Periods <- c(unique(fdata$Time.Period))
  All.Periods <- tail(sort(All.Periods),10)
newdf<-newdf%>%
  filter(Time.Period %in% c(All.Periods))%>%
    spread(key=Time.Period, value=Value)%>% 
    inner_join(indicatortable, by=("Indicator.Code" = "Indicator.Code"))%>%
    arrange(Set, Level)%>%
    select(Set, Indicator, c(All.Periods))
}else if(ffrequency="M"){
    newdf<-filter(fdata,Country.Name == fcountry, timetype == "Monthly")%>%
    select(1:6,-2,-3)
  All.Periods <- c(unique(fdata$Time.Period))
  All.Periods <- tail(sort(All.Periods),10)
newdf<-newdf%>%
  filter(Time.Period %in% c(All.Periods))%>%
  spread(key=Time.Period, value=Value)%>%
  inner_join(indicatortable, by=("Indicator.Code" = "Indicator.Code"))%>%
  arrange(Set, Level)%>%
  select(Set, Indicator, c(All.Periods))
}

kable(newdf, "latex", longtable = T, booktabs = T, align = c("c","l",rep("r",10)), caption=c(ftitle), digits = 1)%>%
column_spec(1, width = "1em")%>%
column_spec(2,width = "16em")%>%
collapse_rows(columns = 1:2, latex_hline = "major", valign = "top", row_group_label_position = 'stack')%>%
kable_styling(latex_options = c("striped", "repeat_header"), full_width = T)

  }


@

【问题讨论】:

    标签: r knitr kable kableextra


    【解决方案1】:

    错误消息是Error in parse(Text = x, srcfile = src)::3:16: unexpected '='.,这发生在您的函数中以&lt;&lt;LoadCountryx&gt;&gt;= 开头的块中。您可以使用消息的3:16 部分找到问题的位置:即第 3 行第 16 列。

    块的第 3 行是

    ){if(ffrequency="Y"){
    

    (由于换行,这看起来像您帖子中的第 4 行;通常行计数有点棘手!)

    第 16 列包含等号。这是不允许的:像其他一些语言一样,R 使用“=”进行赋值,使用“==”来测试相等性。所以这就是你的错误的根源。

    我没有在第一段代码中看到那一行,所以我认为它现在是一个函数这一事实是无关紧要的。

    【讨论】:

    • 非常感谢!完美解决了问题!抱歉问了这么愚蠢的问题。
    猜你喜欢
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多