How to produce a margintable (in a LaTeX tufte-book) using kable or xtable or a combination - knitr

In an Rnw document with
\documentclass[twoside,nobib,ls,notoc,nofonts]{tufte-book}
<<>>=
library(tidyverse)
library(xtable)
library(kableExtra)
#
which is to be compiled with knitr followed by pdflatex.
I have a tibble:
tb <- tibble(measure = c("mean", "variance"), mean = c(24.4, 0.808), median = c(24.5, 4.37), efficiency = c(NA, 0.185))
I'm trying to produce a margintable:
\begin{margintable}
\caption[Results]{\label{tab:bootstrapEfficiency}Results of bootstrap}
\centering
\begin{tabular}[t]{lrrr}
\toprule
\multicolumn{1}{c}{} & \multicolumn{2}{c}{measure of location} & \multicolumn{1}{c}{} \\
\cmidrule(l{3pt}r{3pt}){2-3}
\rotatebox{45}{measure} & \rotatebox{45}{mean} & \rotatebox{45}{median} & \rotatebox{45}{efficiency}\\
\midrule
mean & 24.4 & 24.5 & NA\\
variance & 0.8 & 4.4 & 0.18\\
\bottomrule
\end{tabular}
\end{margintable}
kable doesn't produce what I hoped it would:
kable(bootTb,
format = "latex",
booktabs = TRUE,
caption = "Results of bootstrap",
caption.short = "Results",
label = "bootstrapEfficiency",
digits = c(0, 1, 1, 2)) %>%
add_header_above(c("", "measure of location" = 2, "")) %>%
kable_styling(full_width = FALSE, latex_table_env = "margintable") %>%
row_spec(0, angle = 45)
The output is:
\begin{table}
\caption[Results]{\label{tab:bootstrapEfficiency}Results of bootstrap}
\centering
\begin{margintable}{lrrr}
\toprule
\multicolumn{1}{c}{} & \multicolumn{2}{c}{measure of location} & \multicolumn{1}{c}{} \\
\cmidrule(l{3pt}r{3pt}){2-3}
\rotatebox{45}{measure} & \rotatebox{45}{mean} & \rotatebox{45}{median} & \rotatebox{45}{efficiency}\\
\midrule
mean & 24.4 & 24.5 & NA\\
variance & 0.8 & 4.4 & 0.18\\
\bottomrule
\end{margintable}
\end{table}
Which is not correct LaTeX. Of course removing latex_table_env = "margintable" produces a fine LaTeX result, but one that can't be wrapped in a margintable.
In contrast,
xtable(tb) %>%
print.xtable(type = "latex",
floating = FALSE,
booktabs = TRUE,
rotate.colnames = TRUE)
produces
\begin{tabular}{rlrrr}
\toprule
& \begin{sideways} measure \end{sideways} & \begin{sideways} mean \end{sideways} & \begin{sideways} median \end{sideways} & \begin{sideways} efficiency \end{sideways} \\
\midrule
1 & mean & 24.40 & 24.50 & \\
2 & variance & 0.81 & 4.37 & 0.18 \\
\bottomrule
\end{tabular}
which can be placed in a ```margintable`` environment, but the column names are not rotated 45 degrees, and there's no additional header above them.
I tried to use xtable2kable but couldn't see how to get it to produce the desired result.

Related

Remap key in Autohotkey but wrong tone mark

I useUnikey program to write Vietnamese, when i try to use autohotkey program to remap key, when writing letters, the tone mark is not displayed correctly, can anyone help.Ex:
1::Send, p
2::Send, o
3::Send, i
4::Send, u
5::Send, y
RESULT: caí, gaí, thaỏ...(wrong)-->cái, gái, thảo...(right)
I use code in this link, it's very well written, but I have to type the space key too much, so I was going to remap some keys to make it easier to type but got this error.
mirror_1 = 6
mirror_2 = 7
mirror_3 = 8
mirror_4 = 9
mirror_5 = 0
mirror_q = 1
mirror_w = 2
mirror_e = 3
mirror_r = 4
mirror_t = 5
mirror_a = `;
mirror_s = l
mirror_d = k
mirror_f = j
mirror_g = h
mirror_z = /
mirror_x = .
mirror_c = ,
mirror_v = m
mirror_b = n
mirror_6 = 5
mirror_7 = 4
mirror_8 = 3
mirror_9 = 2
mirror_0 = 1
mirror_y = 5
mirror_u = 4
mirror_i = 3
mirror_o = 2
mirror_p = 1
mirror_h = g
mirror_j = f
mirror_k = d
mirror_l = s
mirror_n = b
mirror_m = v
return
; This key may help, as the space-on-up may get annoying, especially if you type fast.
Control & Space::Suspend
; These keys are optional, but they may help if you are typing on the left-hand side.
CapsLock::Send, {BackSpace}
Space & CapsLock::Send, {Enter}
; If spacebar didn't modify anything, send a real space keystroke upon release.
space::
Send {space}
return
space & 1::
space & 2::
space & 3::
space & 4::
space & 5::
space & q::
space & w::
space & e::
space & r::
space & t::
space & a::
space & s::
space & d::
space & f::
space & g::
space & z::
space & x::
space & c::
space & v::
space & b::
space & `;::
space & ,::
space & .::
space & /::
space & 6::
space & 7::
space & 8::
space & 9::
space & 0::
space & y::
space & u::
space & i::
space & o::
space & p::
space & h::
space & j::
space & k::
space & l::
space & n::
space & m::
; Determine the mirror key, if there is one:
if A_ThisHotkey = space & `;
MirrorKey = a
else if A_ThisHotkey = space & ,
MirrorKey = c
else if A_ThisHotkey = space & .
MirrorKey = x
else if A_ThisHotkey = space & /
MirrorKey = z
else ; To avoid runtime errors due to invalid var names, do this part last.
{
StringRight, ThisKey, A_ThisHotkey, 1
StringTrimRight, MirrorKey, mirror_%ThisKey%, 0 ; Retrieve "array" element.
if MirrorKey = ; No mirror, script probably needs adjustment.
return
}
Modifiers =
GetKeyState, state1, LWin
GetKeyState, state2, RWin
state = %state1%%state2%
if state <> UU ; At least one Windows key is down.
Modifiers = %Modifiers%#
GetKeyState, state1, Control
if state1 = D
Modifiers = %Modifiers%^
GetKeyState, state1, Alt
if state1 = D
Modifiers = %Modifiers%!
GetKeyState, state1, Shift
if state1 = D
Modifiers = %Modifiers%+
Send %Modifiers%{%MirrorKey%}
return
1::p
2::o
3::i
4::u
5::y
return

Is there a way to change the categorical variables in tbl_summary from displaying as {p}% to proportions?

Is there a way to change the categorical variables in tbl_summary from displaying as {p}% to proportions?
An example, instead of showing 48%, it will be 0.48. I tried style_percent and style number without any look. I also tried datasummary but it didn't work and I couldn't display the standard deviation under the average in parentheses.
tbl_summary(data = CPS, by = "Type",
include = c(Female, Hispanic,
age,
DadGradCollege,
MomGradCollege,
ftotval_def
),
statistic = list(all_continuous() ~ "{mean} \n({sd})",
all_categorical() ~ "{p}%"),
label = list(
Female~ "Female",
Hispanic ~ "Hispanic",
age ~ "Age",
DadGradCollege ~ "Fathers with \n \t College",
MomGradCollege ~ "Mothers with \n \t College",
ftotval_def ~ "Total Family Income \n \t (1999 dollars)"
),
missing = "no")
Yep, you can use the digits= argument to pass functions that will style/round/format the statistics. Example below!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.6.2'
trial |>
tbl_summary(
include = grade,
statistic = all_categorical() ~ "{p}",
digits = all_categorical() ~ function(x) style_number(x, digits = 2)
) %>%
as_kable() # convert to kable for SO
Characteristic
N = 200
Grade
I
0.34
II
0.34
III
0.32
Created on 2022-11-04 with reprex v2.0.2

How to get the UTF-8 code from a single character in VBScript

I would like to get the UTF-8 Code of a character, have attempted to use streams but it doesn't seem to work:
Example: פ should give 16#D7A4, according to https://en.wikipedia.org/wiki/Pe_(Semitic_letter)#Character_encodings
Const adTypeBinary = 1
Dim adoStr, bytesthroughado
Set adoStr = CreateObject("Adodb.Stream")
adoStr.Charset = "utf-8"
adoStr.Open
adoStr.WriteText labelString
adoStr.Position = 0
adoStr.Type = adTypeBinary
adoStr.Position = 3
bytesthroughado = adoStr.Read
Msgbox(LenB(bytesthroughado)) 'gives 2
adoStr.Close
Set adoStr = Nothing
MsgBox(bytesthroughado) ' gives K
Note: AscW gives Unicode - not UTF-8
The bytesthroughado is a value of byte() subtype (see 1st output line) so you need to handle it in an appropriate way:
Option Explicit
Dim ss, xx, ii, jj, char, labelString
labelString = "ařЖפ€"
ss = ""
For ii=1 To Len( labelString)
char = Mid( labelString, ii, 1)
xx = BytesThroughAdo( char)
If ss = "" Then ss = VarType(xx) & " " & TypeName( xx) & vbNewLine
ss = ss & char & vbTab
For jj=1 To LenB( xx)
ss = ss & Hex( AscB( MidB( xx, jj, 1))) & " "
Next
ss = ss & vbNewLine
Next
Wscript.Echo ss
Function BytesThroughAdo( labelChar)
Const adTypeBinary = 1 'Indicates binary data.
Const adTypeText = 2 'Default. Indicates text data.
Dim adoStream
Set adoStream = CreateObject( "Adodb.Stream")
adoStream.Charset = "utf-8"
adoStream.Open
adoStream.WriteText labelChar
adoStream.Position = 0
adoStream.Type = adTypeBinary
adoStream.Position = 3
BytesThroughAdo = adoStream.Read
adoStream.Close
Set adoStream = Nothing
End Function
Output:
cscript D:\bat\SO\61368074q.vbs
8209 Byte()
a 61
ř C5 99
Ж D0 96
פ D7 A4
€ E2 82 AC
I used characters ařЖפ€ to demonstrate the functionality of your UTF-8 encoder (the alts8.ps1 PowerShell script comes from another project):
alts8.ps1 "ařЖפ€"
Ch Unicode Dec CP IME UTF-8 ? IME 0405/cs-CZ; CP852; ANSI 1250
a U+0061 97 …97… 0x61 a Latin Small Letter A
ř U+0159 345 …89… 0xC599 Å� Latin Small Letter R With Caron
Ж U+0416 1046 …22… 0xD096 Ð� Cyrillic Capital Letter Zhe
פ U+05E4 1508 …228… 0xD7A4 פ Hebrew Letter Pe
€ U+20AC 8364 …172… 0xE282AC â�¬ Euro Sign

Date function to display the date as mm/dd/yy

I'm trying to get the date in mm/dd/yy format using VB Script.
But I couldn't find any function to do the requirement , can some help me here please?
I like to use the .NET stringbuilder, because I can switch formats on the fly by just adapting the format specifier instead of using string manipulation:
wscript.echo CreateObject("system.text.stringbuilder").AppendFormat("{0:MM}/{0:dd}/{0:yy}", now).ToString()
A one-line alternative that doesn't require .NET:
d = Right("0" & Month(Date), 2) & "/" & Right("0" & Day(Date), 2) & "/" & Right(Year(Date), 2)
FormatDateTime function formats and returns a valid date:
FormatDateTime(date,2)
For locale independent formatting:
function mmddyyyy(input)
dim m: m = month(input)
dim d: d = day(input)
if (m < 10) then m = "0" & m
if (d < 10) then d = "0" & d
mmddyyyy = m & "/" & d & "/" & right(year(input), 2)
end function

Excel, VB - Serialize an 8 digit date to mm/dd/yy

ISSUE
I am trying to convert a 8 digit number into a date while in an array. Examples of the entries are 12282009 or 12202007. There are other malformed entries in the field including dates entered as strings. I want the 8 digit number to be formatted as 12/28/09 or 12/20/07 respectively. I keep getting a type mismatch error on the third to last line below. How do I do this??
CODE
Dim del()
ReDim del(1 To importwsRowCount, 1 To 1)
del = Range("AH1:AH" & importwsRowCount).Value
Dim delChars As Long
Dim delType As String
For i = LBound(del, 1) To UBound(del, 1)
delChars = Len(del(i, 1)) 'Determine length of entry
If IsNumeric(del(i, 1)) = True Then 'Determine datatype of entry
delType = "Numeric"
del(i, 1) = Abs(del(i, 1))
Else
delType = "String"
del(i, 1) = UCase(del(i, 1))
End If
If delType = "Numeric" Then
If delChars = 8 Then
del(i, 1) = DateSerial((Right(del(i, 1), 4)), (Left(del(i, 1), 2)), (Mid(del(i, 1), 3, 2))) '<-- TYPE MISMATCH ERROR
End If
End If
ENTRY TEMPLATES
SEPT. 25, 20 (No year, no year! Delete.)
SEPT (No year, useless, delete.)
N/A (Rubbish! Deleted.)
LONG TIME AG (What moron thought this was a good idea, delete.)
JUNE 30, 200 (Apparently the field will only hold 12 characters, delete.)
CHARGED OFF (Useless, delete.)
94 DAYS (Take all characters preceding space and subtract from other field containing order date to obtain delinquent date.)
94 DPD (DPD in someones bright mind stands for Days Past Due I believe. Same as above.)
2008-7-15 12 (Not sure what additional number is, take all characters before space and transform.)
INVALID (Delete.)
BLANK (Do nothing.)
4/2/4/09 (Malformed date, delete.)
1/1/009 (Same as above.)
12282009 (Use nested LEFT and RIGHT and CONCATENATE with / in between.)
9202011 (Add leading zero, then same as above.)
92410 (Add leading zero, this will transform to 09/24/10)
41261 (Days since 31/12/1899, this will transform to 12/08/12)
1023 (Days since delinquent, subtract from ORDER DATE to get delinquent date.)
452 (Same as above.)
12 (Same as above.)
1432.84 (Monetary value, mistakenly entered by low IQ lackey. Delete.)
Right(Left(del(i, 1), 2), 6) is nonsensical.
The Left(del(i, 1), 2) part happens first and returns a 2-character string. If you then apply Right(..., 6) to that 2-character string you get an error.
The Mid function is needed here: Mid(del(i, 1), 3, 2)
Running the Abs function earlier changed the array entry from being a Variant with subtype String to being a Variant with subtype Double. This shouldn't necessarily affect the Left/Mid/Right functions but try:
del(i, 1) = CStr(del(i, 1))
del(i, 1) = DateSerial((Right(del(i, 1), 4)), (Left(del(i, 1), 2)), (Mid(del(i, 1), 3, 2)))
We need to identify what the actual value causing the error is so:
If delType = "Numeric" Then
If delChars = 8 Then
On Error Goto DateMismatchError
del(i, 1) = DateSerial((Right(del(i, 1), 4)), (Left(del(i, 1), 2)), (Mid(del(i, 1), 3, 2))) '<-- TYPE MISMATCH ERROR
On Error Goto 0
End If
End If
' at the end of your Sub or Function - I'm assuming Sub here
Exit Sub
DateMismatchError:
MsgBox "Date mismatch: error number " & Err.Number & ", " & Err.Description & _
" caused by data value: |" & del(i, 1) & "| at row " & i & ". Original data " & _
"value is |" & Range("AH" & i).Value2 & "|, displayed value is |" & _
Range("AH" & i).Text & "|, number format is |" & Range("AH" & i).NumberFormat & "|"
End Sub
You can use this shorter code to replace your array elements with formatted dates
It cuts down the amount of testing inside the loop to two IFs. If numeric test is run first - there is no point running a longer lenint test for strings that are not 8 characters
The string functions Left$, Mid$ etc are much quicker than their variant cousins Left, Mid etc
I have made a substituion for your importwsRowCount variable in the code below
Updated code to handle and dump results, now handles string tests and non-compliantnumbers as per barrowc comments
The code below puts the new dates into a second array, skipping the invalid dates
The second array is then dumped at `AI``
Sub ReCut2()
Dim del()
Dim X()
Dim lngCnt As Long
del = Range("AH1:Ah10").Value2
ReDim X(1 To UBound(del, 1), 1 To UBound(del, 2))
Dim delChars As Long
Dim delType As String
For lngCnt = LBound(del, 1) To UBound(del, 1)
If IsNumeric(del(lngCnt, 1)) Then
If Len(Int((del(lngCnt, 1)))) = 8 Then X(lngCnt, 1) = DateSerial(Right$(del(lngCnt, 1), 4), Left$(del(lngCnt, 1), 2), Mid$(del(lngCnt, 1), 3, 2))
End If
Next
[ai1].Resize(UBound(X, 1), UBound(X, 2)).Value2 = X
End Sub