Kathleen1412
Kathleen1412

Reputation: 23

KableExtra and knitr: Error in parse(Text = x, srcfile = src): unexpected '='

I am working with a flat data set ("FSIflat") that has country (char), indicator (char), indicator code (char), time period (char: e.g. 2010Q3, 2010M12, 2010) and value (num) and time type ("Quarterly", "Monthly" or "Annual") and the goal is to produce tables in PDFs when users call a country. I tried to convert a chunk into a function but failed. The original chunk works fine and it produces PDF. But the function gave me Error in parse(Text = x, srcfile = src)::3:16: unexpected '='.

I am new to R and everything. I spent days on it and could not really figure out what went wrong. It would be great if someone could help. Thanks a lot!

The chunk

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)

The function

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

  }


@

Upvotes: 1

Views: 8561

Answers (1)

user2554330
user2554330

Reputation: 44977

The error message was Error in parse(Text = x, srcfile = src)::3:16: unexpected '='., and this happened in your function in the chunk starting <<LoadCountryx>>=. You can find the location of the problem using the 3:16 part of the message: that's line 3, column 16.

Line 3 of the chunk is

){if(ffrequency="Y"){

(This looks like line 4 in your posting because of wrapping; often line counting is a little tricky!)

Column 16 contains the equal sign. That's not allowed: like some other languages, R uses "=" for assignment, and "==" for testing equality. So that's the source of your error.

I don't see that particular line in the first block of code, so I think the fact that it's now a function is irrelevant.

Upvotes: 1

Related Questions