How can I print a stargazer table when summary=F and a character column has elements containing &? summary=F prints the data table verbatim which is what I want.
Here's a case where it prints as expected:
> stargazer::stargazer(
+ data.frame(ur1=c('hot','tamale'),yum='!')
+ ,type='text',summary=F)
============
ur1 yum
------------
1 hot !
2 tamale !
------------
And here the & is split into two columns.
> stargazer::stargazer(
+ data.frame(ur1=c('hot & tamale'),yum='!')
+ ,type='text',summary=F)
============
ur1 yum
------------
1 hot tamale
------------
The same behavior happens in html and latex modes as well. In latex it's easy to hack a fix for a character by commenting out the & a la gsub(x,pattern='&',replacement='\\&',fixed=T) but that fix doesn't work for html and replacing it with the entity & still causes the split. While it would be easy to use kable or simple markdown to print the table, I want my this kind of table to have the same style as the stargazer regression tables.
I hope someone can help! Also, I couldn't find a development repo to report it if it's a bug.
I do have the latest version of stargazer:
> devtools::session_info()
Session info ------------------
setting value
version R version 3.4.4 (2018-03-15)
system x86_64, linux-gnu
ui RStudio (1.1.419)
language (EN)
collate en_US.UTF-8
tz Etc/UTC
date 2018-09-14
Packages ----------------------
stargazer 5.2.2 2018-05-30 CRAN (R 3.4.4)
It's a very hacky solution, but you could insert a tab (\t) and a backspace (\b) escape sequence right before the &. This seems to work for type = "text" and type = "html", but not type = "latex".
dat <- data.frame(ur1=c('hot & tamale', 'hot & taco') ,yum='!')
dat[, 1] <- gsub("&", "\t\b&", dat[, 1])
stargazer::stargazer(dat, type = "text", summary=F)
====================
ur1 yum
--------------------
1 hot & tamale !
2 hot & taco !
--------------------
Related
I'm using the papaja package to make a reproducible manuscript using R and Rmarkdown. One thing I'd like to do for my tables is have symbols using mathmode. For example, I'm trying to make a column of variables that are comparisons of two z\mathcalc{M}symbols (e.g.,$\mathcal{M}_1 - \mathcal{M}_2$`, which looks like this in Rmarkdown:
However, when I try to use these symbols as cells within a data.frame, R spits out a warning:
table1 <- c("$\mathcal{M}_1 - \mathcal{M}_3$",
"$\mathcal{M}_1 - \mathcal{M}_3$",
"$\mathcal{M}_1 - \mathcal{M}_4$",
"$\mathcal{M}_1 - \mathcal{M}_5$",
"$\mathcal{M}_2 - \mathcal{M}_3$",
"$\mathcal{M}_2 - \mathcal{M}_4$",
"$\mathcal{M}_2 - \mathcal{M}_5$",
"$\mathcal{M}_3 - \mathcal{M}_4$",
"$\mathcal{M}_3 - \mathcal{M}_5$",
"$\mathcal{M}_4 - \mathcal{M}_5$")
table2 <- c(49.273540, 198.383423, 198.956909, 198.846716, 198.933951, 198.200322, 197.778849, 11.361870, 14.627681, 7.231055)
table <- data.frame(table1,table2)
Error: '\m' is an unrecognized escape in character string starting ""$\m"
From what I've found elsewhere about using mathmode in R, one solution is to use two slashes, which would look like this:
table1 <- c("$\\mathcal{M}_1 - \\mathcal{M}_3$",
"$\\mathcal{M}_1 - \\mathcal{M}_3$",
"$\\mathcal{M}_1 - \\mathcal{M}_4$",
"$\\mathcal{M}_1 - \\mathcal{M}_5$",
"$\\mathcal{M}_2 - \\mathcal{M}_3$",
"$\\mathcal{M}_2 - \\mathcal{M}_4$",
"$\\mathcal{M}_2 - \\mathcal{M}_5$",
"$\\mathcal{M}_3 - \\mathcal{M}_4$",
"$\\mathcal{M}_3 - \\mathcal{M}_5$",
"$\\mathcal{M}_4 - \\mathcal{M}_5$")
table2 <- c(49.273540, 198.383423, 198.956909, 198.846716, 198.933951, 198.200322, 197.778849, 11.361870, 14.627681, 7.231055)
table <- data.frame(table1,table2)
So far, so good. No error messages. However, when I try and format this with the papaja package into a latex table using the apa_table function, I get this:
> apa_table(table)
\begin{table}[tbp]
\begin{center}
\begin{threeparttable}
\begin{tabular}{ll}
\toprule
table1 & \multicolumn{1}{c}{table2}\\
\midrule
\$\textbackslash{}mathcal\{M\}\_1 - \textbackslash{}mathcal\{M\}\_3\$ & 49.27\\
\$\textbackslash{}mathcal\{M\}\_1 - \textbackslash{}mathcal\{M\}\_3\$ & 198.38\\
\$\textbackslash{}mathcal\{M\}\_1 - \textbackslash{}mathcal\{M\}\_4\$ & 198.96\\
\$\textbackslash{}mathcal\{M\}\_1 - \textbackslash{}mathcal\{M\}\_5\$ & 198.85\\
\$\textbackslash{}mathcal\{M\}\_2 - \textbackslash{}mathcal\{M\}\_3\$ & 198.93\\
\$\textbackslash{}mathcal\{M\}\_2 - \textbackslash{}mathcal\{M\}\_4\$ & 198.20\\
\$\textbackslash{}mathcal\{M\}\_2 - \textbackslash{}mathcal\{M\}\_5\$ & 197.78\\
\$\textbackslash{}mathcal\{M\}\_3 - \textbackslash{}mathcal\{M\}\_4\$ & 11.36\\
\$\textbackslash{}mathcal\{M\}\_3 - \textbackslash{}mathcal\{M\}\_5\$ & 14.63\\
\$\textbackslash{}mathcal\{M\}\_4 - \textbackslash{}mathcal\{M\}\_5\$ & 7.23\\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{center}
\end{table}
Which doesn't produce my desired table:
So in summary, how can I get $\mathcal{M}_1 - \mathcal{M}_2$ simply into my table besides just pasting this in manually? I have to do this often so I'd like a simpler way of assigning the values.
apa_table escapes your strings to be literally transcribed in LaTeX. This is usually what you want but in your case you can pass escape = FALSE to apa_table.
Unfortunately the default parameter for this argument is inconsistent between different S3 methods of this generic function. This is probably a bug.
This example will knit on my laptop but not my desktop. Both are using the same RStudio and R versions. I have tried uninstalling R and RStudio, changing R and RStudio versions, changing 32/64 bit R, trying to install a variety of pandoc versions, setting the path to pandoc manually with Sys.setenv()... I am sort of at a loss here.
Both machines have the following:
> rmarkdown::pandoc_version()
[1] ‘1.19.2.1’
> version
_
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 4.3
year 2017
month 11
day 30
svn rev 73796
language R
version.string R version 3.4.3 (2017-11-30)
nickname Kite-Eating Tree
Here is a minimum non/working example. Again, it works on the laptop but not the desktop.
---
title: "Pandoc testing sandbox"
output:
html_document:
df_print: paged
number_sections: yes
toc: yes
toc_float:
collapsed: no
smooth_scroll: no
---
Here is a chunk of code to test:
```{r, results = "asis", echo = FALSE, message = FALSE}
tex2markdown <- function(texstring) {
writeLines(text = texstring,
con = myfile <- tempfile(fileext = ".tex"))
texfile <- knitr::pandoc(input = myfile, format = "html")
cat(readLines(texfile), sep = "\n")
unlink(c(myfile, texfile))
}
textable <- "
\\begin{table}[]
\\centering
\\begin{tabular}{l | c | c}
& without replacement & with replacement \\\\ \\hline
order matters & permutation: $\\frac{n!}{(n-k)!}$ & $n^k$ \\\\ \\hline
order does not matter & combination: ${n\\choose k}=\\frac{n!}{k!(n-k)!}$ & (\\textit{special case}) \\hline
\\end{tabular}
\\end{table}
"
tex2markdown(textable)
```
The error text on the desktop machine is:
Line 17 Error in knitr::pandoc(input = myfile, format = "html") :
Please install pandoc first: http://pandoc.org
Calls: <Anonymous> ... withVisible -> eval -> eval ->
tex2markdown -> <Anonymous> Execution halted
Any help with this would be greatly appreciated!
===
Edit: I did a bit more testing and ran:
rmarkdown::render("pandoctest.Rmd", "html_document")
And there was an option to "Rerun with Debug", so I did that, and the error is happening in "Function pandoc (namespace:knitr)" The line of code where the break happens in what I suppose is the pandoc function in the knitr package is:
...
if (Sys.which("pandoc") == "")
stop("Please install pandoc first: http://pandoc.org")
...
So it seems like pandoc is checking to see if it is installed itself? Or somehoe this is a pandoc function in the knitr package that is calling an actual pandoc program from somewhere else? SO it seems I need to get knitr to somehow see that I have pandoc installed.
For some reason, the optparse usage in this script breaks:
test.R:
#!/usr/bin/env Rscript
library("optparse")
option_list <- list(
make_option(c("-n", "--name"), type="character", default=FALSE,
dest="report_name", help="A different name to use for the file"),
make_option(c("-h", "--height"), type="numeric", default=12,
dest = "plot_height", help="Height for plot [default %default]",
metavar="plot_height"),
make_option(c("-w", "--width"), type="numeric", default=10,
dest = "plot_width", help="Width for plot [default %default]",
metavar="plot_width")
)
opt <- parse_args(OptionParser(option_list=option_list), positional_arguments = TRUE)
print(opt)
report_name <- opt$options$report_name
plot_height <- opt$options$plot_height
plot_width <- opt$options$plot_width
input_dir <- opt$args[1] # input directory
I get this error:
$ ./test.R --name "report1" --height 42 --width 12 foo
Error in getopt(spec = spec, opt = args) :
redundant short names for flags (column 2).
Calls: parse_args -> getopt
Execution halted
However, if I remove the "-h" from this line:
make_option(c("--height"), type="numeric", default=12,
dest = "plot_height", help="Height for plot [default %default]"
It seems to work fine;
$ ./test.R --name "report1" --height 42 --width 12 foo
$options
$options$report_name
[1] "report1"
$options$plot_height
[1] 42
$options$plot_width
[1] 12
$options$help
[1] FALSE
$args
[1] "foo"
Any ideas what might be going on here?
I am using R 3.3.0 and optparse_1.3.2 (getopt_1.20.0)
The -h flag is reserved by optparse (which is described as a feature of optparse which is not in getopt, from the getopt.R source file on Github):
Some features implemented in optparse package unavailable in getopt:
2. Automatic generation of an help option and printing of help text when encounters an "-h"
Therefore, when the user specifies -h, the sanity check for uniqueness of flags fails. The issue tracker does not seem to have any mention of the need to create a better error message for this case, however.
Finally, note that optparse seems to be invoking getopt, as they have the same author.
Very strange error on a value being not null, but is claimed to be null, even though it evaluates to FALSE on is.null() test. See below. In this case, pid seems to be null, but the test fails, causing me all sorts of 'next step' problems in the code.
> pid <- system2('ps', args = "-ef | grep 'ssh -f' | grep -v grep | tr -s ' ' | \ cut -d ' ' -f 3", stdout = TRUE)
> pid
character(0)
> is.null(pid)
[1] FALSE
> if(!is.null(pid) && nchar(pid)) {cat('got some pid')}
Error in if (!is.null(pid) && nchar(pid)) { :
missing value where TRUE/FALSE needed
> if(!is.null(pid)) {cat('got some pid? Really?')}
got some pid? Really?
What does folks think is happening here? Here is my version information of R:
> version
_
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 2.2
year 2015
month 08
day 14
svn rev 69053
language R
version.string R version 3.2.2 (2015-08-14)
nickname Fire Safety
Full version of the OS:
Linux rserver 3.16.0-44-generic #59~14.04.1-Ubuntu SMP Tue Jul 7 15:07:27 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
In the end, I simply want this code to run:
> if (nchar(pid) > 0) {
+ cat('do something\n')
+ }
Error in if (nchar(pid) > 0) { : argument is of length zero
The fact that you have an empty character variable doesn't mean that it's NULL. Here's an example:
pid <- character()
> pid
character(0)
> is.null(pid)
[1] FALSE
> pid <- NULL
> pid
NULL
> is.null(pid)
[1] TRUE
I've a plain text file.
> Input: इंजेक्शन इंटरनॅशनल इंटिग्रेटेड इंटिरिअर इंडस्ट्री
All words are separated by one or more spaces. I want to collect all unique chars from the text file. I'm looking for a unix command; the order of the result chars is not important.
> Expected result: इं जे क्श न ट र नॅ श ल इ्रे टे ड टि रिअ र ड स्ट्री
With the command Klaus has provided
cat <file>|sed -e 's/\(.\)/\1\n/g'|sort -u|tr -d '\n'
Result comes as:
ं अ इ क ग ज ट ड न र ल श सिीॅे्
I don't want to separate horizontal or vertical conjuncts or dependent vowels from its base character.
I just want to separate complete characters in a word from each other.
Can we achieve this with UNIX commands?
"base character" + "dependent vowel" = "complete character"
- क ा का
- क ि कि
Klaus's command works for English text only. But, It doesn't work with indic languages such as Hindi.
Input: hi1 hello-2 how!3 "are4 ?you5
result: h i e l o w a r y u 1 2 3 4 5 - ! "
Note:- You have to install Indic support in your OS.
Also, download Mangal font from http://hindi-fonts.com/fonts/Mangal
Try this:
cat <file>|sed -e 's/\(.\)/\1\n/g'|sort -u|tr -d '\n'
or simplified ( stolen from fedorqui comment, thanks! Never seen & before in the replacement part. Good to learn something new! )
sed 's/./&\n/g' <file> | sort -u | tr -d '\n'