KableExtra和Knitr:解析错误(Text = x,srcfile = src):意外的'='

时间:2018-09-26 02:06:07

标签: r knitr kable kableextra

我正在使用一个具有国家(字符),指标(字符),指标代码(字符),时间段(字符:例如2010Q3、2010M12、2010)和值(数字)的平面数据集(“ FSIflat”) )和时间类型(“季度”,“每月”或“年度”),目标是在用户致电某个国家/地区时以PDF格式生成表格。我试图将一个块转换成一个函数,但是失败了。原始块工作正常,并生成PDF。但是函数给了我解析错误(Text = x,srcfile = src):: 3:16:意外的'='

我是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)

  }


@

1 个答案:

答案 0 :(得分:0)

错误消息为Error in parse(Text = x, srcfile = src)::3:16: unexpected '='.,并且此错误发生在函数中<<LoadCountryx>>=开始的代码块中。您可以使用消息的3:16部分找到问题的位置:第3行,第16列。

该块的第3行是

){if(ffrequency="Y"){

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

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

在第一段代码中没有看到该特定行,因此我认为它现在是一个函数这一事实是无关紧要的。