【发布时间】: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