pfmc-assessments / sa4ss

Generate a stock assessment document from Stock Synthesis output
https://pfmc-assessments.github.io/sa4ss/
Other
12 stars 7 forks source link

SPR table with percent symbol in colnames won't knit #22

Closed mkapur closed 3 years ago

mkapur commented 3 years ago

I am working with the auto-generated .tex tables, namely file d_SPR_ES.tex. If I copy and paste the file as-is, I receive the following error,

! Misplaced \noalign.
\toprule ->\noalign 
                    {\ifnum 0=`}\fi \@aboverulesep =\abovetopsep \global \@b...
l.428 ...rcent intervals.\}\textbackslash{} \toprule
                                                   Year \& (1-SPR)/(1-SPR 45...

which I tracked down to there being a "%" in the column titles. I attempted inserting LaTeX syntax into the colnames but the issue persisted; I was only able to get the table to render if I changed the column header to (1-SPR)/(1-SPR 45).

Not sure if there is a workaround/additional package that would fix this, but at minimum wanted to document for folks who might encounter the same.

chantelwetzel-noaa commented 3 years ago

I have had similar issues in the past and likely tried similar solutions as you @mkapur. For my tables I opted to either not include the percent symbol (e.g. 1-SPR) and use language in the caption saying the it was a percentage (used "percent" rather than "%" in the caption) or just use the word percent any place in the table that I would use "%".

Also, if you are creating tables using the SSexecutivesummary function for your sa4ss document it is better to use the sa4ss::read_model function which automatically does some of the settings to assist with these issues. Here is an example call to that function that will read the model located at directory pointed to by the "base" object.

read_model( mod_loc = base, create_plots = FALSE, fecund_mult = 'mt', save_loc = file.path(base, "tex_tables"), verbose = TRUE)

mkapur commented 3 years ago

Thanks @chantelwetzel-noaa. I indeed am using the sa4ss::read_model() function which auto-generated these tables. Not sure if my version is slightly out of date. Good to know this is common and I think folks can survive without the "%". It could be worth the time to simply truncate it from the function (if it isn't already) so less latex-friendly users don't end up with a broken document. Thanks!

chantelwetzel-noaa commented 3 years ago

If you update your r4ss package and still encounter this issue let me know. I am sure there is a clever way to get around this issue to be able to include % in our tables. I will leave this issue open for a while so we will remember to try to find a smarter or better solution to this issue.

mkapur commented 3 years ago

Sure -- FWIW I updated R4SS over the weekend so I'm pretty sure I'm running the latest-ish version.

chantelwetzel-noaa commented 3 years ago

@mkapur That is good to know. It is really helpful that you specified which table seemed to be the issue. It may be occurring due to your specific SPR type. Can you let me know what is returned from replist[["SPRratioLabel"]] where the replist is the object created by SS_output? Also, can you tell me what SPR type is specified in the starter file (this is towards the bottom)? I have not encountered this issue but I am likely using a different SPR type I suspect.

mkapur commented 3 years ago

Sure:

>replist["SPRratioLabel"]
$SPRratioLabel
[1] "(1-SPR)/(1-SPR_45%)"

in starter: 1 # SPR_report_basis: 0=skip; 1=(1-SPR)/(1-SPR_tgt); 2=(1-SPR)/(1-SPR_MSY); 3=(1-SPR)/(1-SPR_Btarget); 4=rawSPR

chantelwetzel-noaa commented 3 years ago

@mkapur Thank you. That is really helpful. I suspect I have done all my recent sa4ss testing on models that are using option 4 in the starter file which sets the SPRratioLabel as "1-SPR" without the SPR percent indicated. I will get a fix to the code near the end of the week so you won't have to make hand edits to the tex files.

aaronmberger-nwfsc commented 3 years ago

Just noting that for hake we use the paste0 function to insert % symbols into tables (see below), though I'm not familiar with the underlying tools used for building tables here. The below code is used with xtable(). @kellijohnson-NOAA may be able to add more specifics and/or shed light if this approach is even feasible within sa4ss.

tab.filt <- cbind(yrs, f(df$smed weight.factor), paste0(f(df$dmed 100, digits), "\%"), f(tot_bm / weight.factor), f(age2plus_bm / weight.factor), f(df$rmed weight.factor), paste0(f(df$pmed 100, digits), "\%"), paste0(f(df$fmed * 100, digits), "\%"))

image

aaronmberger-nwfsc commented 3 years ago

I notice that github is auto-adjusting the inserted code in my previous comment. There should be two backslashes prior to the % (not one) within the quotes.

chantelwetzel-noaa commented 3 years ago

That is really helpful to know @aaronmberger-nwfsc. In previous versions of my rmarkdown assessment documents I was not able to get that trick to work (in the caption or the table headers I think). However, I was using a different table creation approach. The current approach uses kableExtra to create tex tables. I will see if this approach allows for percent symbols in the tex tables compiled by rmarkdown.

kellijohnson-NOAA commented 3 years ago

@mkapur can you please provide a few lines of code so I can wrap my head around things a bit more. First, what function was used to produce the .tex table file? Second, how are you bringing the table into the .Rmd file for the assessment? Sorry that I cannot recall (1) I think it is in the executive summary functions in another package right?

mkapur commented 3 years ago

Hi All Welp, always good to put together a reprex for problem solving. In my initial post I had tried brute latex syntax (wrapping the percent symbol in dollar signs, e.g., $SPR_{45%}$, as well as via paste), which brought about the same error.

Just now I attempted the other syntax which preceeds the percent symbol by a backslash and was able to knit successfuly.

If possible, there should be an ifelse statement during the construction of table d_SPR_ES.tex which inserts this symbol before the percentage if the user has a percent symbol in replist["SPRratioLabel"] .

Header which worked for me:

\begin{longtable}[t]{r>{\centering\arraybackslash}p{1.57cm}>{\centering\arraybackslash}p{1.57cm}>{\centering\arraybackslash}p{1.57cm}>{\centering\arraybackslash}p{1.57cm}>{\centering\arraybackslash}p{1.57cm}>{\centering\arraybackslash}p{1.57cm}}
\caption{\label{tab:exploitES}Estimated recent trend in the spawning potential ratio (SPR), (1-SPR)/(1-SPR 45\%) , the exploitation rate, along with the 95 percent intervals.}\\
\toprule
Year & (1-SPR)/(1-SPR 45\%) & Lower Interval & Upper Interval & Exploitation Rate & Lower Interval & Upper Interval\\
\midrule
\endfirsthead
\caption[]{Estimated recent trend in the spawning potential ratio (SPR),(1-SPR)/(1-SPR 45\%), the exploitation rate, along with the 95 percent intervals. \textit{(continued)}}\\
\toprule
Year & (1-SPR)/(1-SPR 45\%) & Lower Interval & Upper Interval & Exploitation Rate & Lower Interval & Upper Interval\\
\midrule
\endhead

Detailed history below:

My starter file is using option 1 for the SPR_report_basis and my replist["SPRratioLabel"] has the percent symbol: (1-SPR)/(1-SPR_45%)

[1] r4ss_1.42.0    sa4ss_0.0.0.9010 rmarkdown_2.7.13  bookdown_0.21

The tables were generated via a call to sa4ss::read_model() which saved all the tex files individually into my directory. When I pasted table d_SPR_ES.tex into 52tables.Rmd (or another chapter, such as 01executive.RMD) and ran bookdown::render_book() I got the noalign error indicated at the beginning of this issue. The default tex table does not have any wrapping around the percent symbol.

kellijohnson-NOAA commented 3 years ago

Thanks @mkapur that was perfect.

@chantelwetzel-noaa I think we can change the table function to use escape = TRUE but not 100% that it will work fully within the workflow. It did create the following

table_format(x = as.data.frame(tab),
                   caption = df$caption[i],
                   label = df$label[i],
                   digits = c(0, rep(2, n)),
                   longtable = TRUE,
                   col_names = col_names,
                   align = c('r',rep('c', n)),escape=TRUE)
\begingroup\fontsize{10}{12}\selectfont
\begingroup\fontsize{10}{12}\selectfont

\begin{longtable}[t]{r>{\centering\arraybackslash}p{2.2cm}>{\centering\arraybackslash}p{2.2cm}>{\centering\arraybackslash}p{2.2cm}>{\centering\arraybackslash}p{2.2cm}}
\caption{\label{tab:exploitES}Estimated recent trend in the (1-SPR)/(1-SPR_40%) where SPR is the spawning potential ratio the exploitation rate, and the  95 percent intervals.}\\
\toprule
Year & (1-SPR)/(1-SPR 40\%) & Interval & Exploitation Rate & Interval\\
\midrule
\endfirsthead
\caption[]{Estimated recent trend in the (1-SPR)/(1-SPR_40%) where SPR is the spawning potential ratio the exploitation rate, and the  95 percent intervals. \textit{(continued)}}\\
\toprule
Year & (1-SPR)/(1-SPR 40\%) & Interval & Exploitation Rate & Interval\\
\midrule
\endhead

\endfoot
\bottomrule
\endlastfoot
2011 & 0.99 & 0.76–1.23 & 0.20 & 0.1490–0.2467\\
2012 & 0.79 & 0.57–1.01 & 0.07 & 0.0470–0.0876\\
2013 & 0.78 & 0.58–0.97 & 0.09 & 0.0630–0.1160\\
2014 & 0.74 & 0.53–0.94 & 0.09 & 0.0643–0.1197\\
2015 & 0.58 & 0.38–0.78 & 0.07 & 0.0477–0.0907\\
2016 & 0.88 & 0.64–1.11 & 0.10 & 0.0687–0.1400\\
2017 & 0.92 & 0.64–1.19 & 0.17 & 0.1064–0.2389\\
2018 & 0.89 & 0.61–1.17 & 0.15 & 0.0808–0.2176\\
2019 & 0.89 & 0.61–1.18 & 0.16 & 0.0724–0.2399\\
2020 & 0.81 & 0.52–1.10 & 0.18 & 0.0664–0.2939\\*
\end{longtable}
\endgroup{}
\endgroup{}
chantelwetzel-noaa commented 3 years ago

As always, @kellijohnson-NOAA has come up with a simpler and more elegant solution compared to what I envisioned. The one item in the example table that may cause an issue is the % sign in the caption. I will be able to test this out tomorrow and provide an update to this issue.

chantelwetzel-noaa commented 3 years ago

@mkapur I think I have fixed this issue. Th e fix required two changes: 1) set escape = TRUE as the default setting sa4ss::table_format function and 2) added some logic in r4ss:SSexecutivesummary to check to % symbols in the SPR type and to add the needed number of backslashes if present. To get the corrections please update your sa4ss and r4ss packages.