Continuing with the answer to a previous SO question here, the resulting table is occupying the entire width of a slidy_presentation despite the command kable_styling(full_width=F) being utilized. As I am still new to the kableExtra package and RMarkdown as a whole, I have a feeling it might be due to the long footnote at the bottom of the table.
As a result, I am trying to utilize the HTML "popover" feature as suggested in the R vignette Awesome Table in HTML. In particular, I am trying to create a "Note" field in the last row of the x column from the following .RMD file.
RMD file:
---
title: "Untitled"
output: slidy_presentation
---
```{r}
library(kableExtra)
library(dplyr)
```
Include the HTML script from vignette here.
```{r echo=FALSE}
prob.success <- sample( seq(.5,.99,.01), size=1 )
conf.alpha <- sample( seq(.5,.99,.01), size=1 )
tab1 <- data.frame( x=0:5, f=round(dbinom(0:5,5,prob.success),3) ) %>%
mutate( pe=x/5, lcl=qbeta(1-conf.alpha,x+0.5,5-0:5+0.5) ) %>%
mutate( lcl=pmin(pe,lcl) ) %>%
mutate( delta=pe-lcl ) %>%
mutate( f_delta=f*delta )
exp.expr <- "$\\sum_x f(x)*\\left ( pe(x) - lcl(x) \\right )$"
exp.delta <- format( round(sum( tab1$f_delta ),4), nsmall=4 )
tab2 <- tab1 %>%
mutate( x=as.character(x), f=format(round(f,4),nsmall=4) ) %>%
mutate( pe=format(round(pe,4),nsmall=4) ) %>%
mutate( lcl=format(round(lcl,4),nsmall=4) ) %>%
mutate( delta=format(round(delta,4),nsmall=3) ) %>%
mutate( f_delta=format(round(f_delta,4),nsmall=4) ) %>%
rbind( ., data.frame(x="", f="", pe="Exp", lcl="Diff", delta="=", f_delta=exp.expr) ) %>%
rbind( ., data.frame(x="",f="",pe="",lcl="",delta="=",f_delta=exp.delta) )
tab.cols <- c( "x", "f(x)", "pe(x)", "lcl(x)", "pe(x)-lcl(x)",
"$f(x)\\times\\left(pe(x)-lcl(x)\\right)$" )
kable( tab2, format="html", escape=FALSE, align="c", col.names=tab.cols ) %>%
kable_styling( "striped", full_width = F, position="center" ) %>%
footnote( general="Given x successes out of n trials, the holistic Jeffreys $100*(1-\\alpha)\\%$ Lower *Credible* Limit is the value $p$ such that $\\int_0^p \\frac{t^{x+0.5-1}(1-t)^{n-x+0.5-1}}{B(x+0.5,n-x+0.5)} dt = \\alpha$ where B(a,b) is the Beta function given by $\\int_0^1 t^{(x-1)}(1-t)^{(y-1)} dt$.",
general_title="Note:", footnote_as_chunk=TRUE, escape=FALSE )
My attempt thus far
With the only change to the R chunk above being with the variable tab2, here is what I have tried.
tab2 <- tab1 %>%
mutate( x=as.character(x), f=format(round(f,4),nsmall=4) ) %>%
mutate( pe=format(round(pe,4),nsmall=4) ) %>%
mutate( lcl=format(round(lcl,4),nsmall=4) ) %>%
mutate( delta=format(round(delta,4),nsmall=3) ) %>%
mutate( f_delta=format(round(f_delta,4),nsmall=4) ) %>%
rbind( ., data.frame(x="", f="", pe="Exp", lcl="Diff", delta="=", f_delta=exp.expr) ) %>%
# Change is only with the last line
rbind( ., data.frame(x=cell_spec("Note",popover=spec_popover(content="Test",title=NULL,position="bottom")),
f="",pe="",lcl="",delta="=",f_delta=exp.delta) )
and the popover feature doesn't work.
Related
I'm trying to count the number of times the word "text" appears per row in Power BI. I've done a lot of google searching and seen examples like this:
Formula :=
CALCULATE (
COUNTROWS ( FILTER ( 'TestData', FIND ( "text", 'TestData'[Description],, 0 ) > 0 ) ),1=1
)
but it isn't quite getting me there. How can I get for row 1, a result of 1 and row 2, a result of 3.
CREATE TABLE [dbo].[TestData](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Description] [varchar](100) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[TestData] ON
GO
INSERT [dbo].[TestData] ([ID], [Description]) VALUES (1, N'this is my demo text')
GO
INSERT [dbo].[TestData] ([ID], [Description]) VALUES (2, N'text text demo text')
GO
SET IDENTITY_INSERT [dbo].[TestData] OFF
GO
Expected Result
ID Description Text Word Count
1 this is my demo text 1
2 text text demo text 3
Calculated Column:
=
VAR MySearchText = "text"
RETURN
DIVIDE(
LEN( Table1[Description] )
- LEN( SUBSTITUTE( Table1[Description], MySearchText, "" ) ),
LEN( MySearchText )
)
Measure:
=
VAR MySearchText = "text"
VAR ThisDescription =
MIN( Table1[Description] )
RETURN
DIVIDE(
LEN( ThisDescription )
- LEN( SUBSTITUTE( ThisDescription, MySearchText, "" ) ),
LEN( MySearchText )
)
though note that both of these will return positive counts where MySearchText is found within other words: a description of "this is textual", for example, will return 1.
I don't believe DAX has a textsplit function, but you can do something like this to ensure you don't pick up words of which text is a substring.
Text Count (DAX) =
VAR pad = SUBSTITUTE(" " & [Description] & " ","text","~text~")
VAR lenPad = LEN(pad)
VAR lenText = LEN("~text~")
VAR lenRemText = LEN(SUBSTITUTE(pad,"~text~",""))
RETURN (lenPad-lenRemText)/lenText
```
i have a table , which has two columns id and text. This is what data i have:
id text
765 hi how are you
876 John made $57.
743 apple is my favourite fruit.
435 mango is not my favorite fruit.
892 this is my favourite movie
have input column as slicer,in which i can choose any one id.for eg: 743
-765
-876
-**743**
-435
-892
I need the output, in which it would show the id ,text,number of words matching of the selected id from slicer with all the other ids:
id text matching words
743 apple is my favourite fruit. 5
765 hi how are you 0
876 John made $57. 0
435 mango is not my favorite fruit. 4
892 this is my favourite movie 3
OK, I have a solution but I'm not convinced it is the most elegant.
Thanks to the following resource for a text.split function in DAX https://www.excelnaccess.com/text-split-using-dax/.
Create a base table named Table as follows:
Create 2 calculated tables with the following code.
New Table1 =
VAR myvalues =
ADDCOLUMNS ( 'Table', "Paths", TRIM( SUBSTITUTE ( SUBSTITUTE( [text],".","") , " ", "|" ) ))
RETURN
SELECTCOLUMNS (
GENERATE (
myvalues,
ADDCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( [Paths] ) ),
"#word", PATHITEM ( [Paths], [Value], TEXT )
)
),
"id", [id],
"word", [#word],
"text", [text]
)
New Table2 =
VAR myvalues =
ADDCOLUMNS ( 'Table', "Paths", TRIM( SUBSTITUTE ( SUBSTITUTE( [text],".","") , " ", "|" ) ))
RETURN
SELECTCOLUMNS (
GENERATE (
myvalues,
ADDCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( [Paths] ) ),
"#word", PATHITEM ( [Paths], [Value], TEXT )
)
),
"id", [id],
"word", [#word],
"text", [text]
)
Drag New Table 1 [id] to a slicer
Drag New Table 2 [id] and [text] to a table.
Create a measure as follows and drag it into the table.
Matching Words = IF(ISFILTERED('New Table1'[id]), COUNTROWS('New Table2')+0, 0)
Create a relationship between word and word as follows:
That should be everything.
I have two columns
CustomerCode | Segmentation
AU656 | abc
AU765 | cdf
AU563 | abc
AU235 | abc
AU324 | opr
AU908 | opr
AU123 | pqr
AU234 |pqr
I have to find a distinct count of CustomerCode where segmentation is "abc" and "cdf" and "pqr" and divide it by the total number of CustomerCodes (all).
I created a measure -
#RSP =
CALCULATE (
DISTINCTCOUNT ( 'Table'[CustomerCode] ),
FILTER ( ALL ( 'Table' ), 'Table'[Segmentation] = "abc" ),
'Table'[Segmentation] = "cdf",
'Table'[Segmentation] = "opr"
)
However, this shows no value. Am I using the filters wrong?
How do I calculate this?
Your measure fails because Segmentation cannot be multiple values simultaneously. Try this instead:
#RSP =
CALCULATE (
DISTINCTCOUNT ( 'Table'[CustomerCode] ),
'Table'[Segmentation] IN { "abc", "cdf", "opr" }
)
Ratio = DIVIDE ( [#RSP], DISTINCTCOUNT ( 'Table'[CustomerCode] ) )
In your data You don't have a customer with that specific condition: segmentation is "abc" and "cdf" and "pqr" (Any of the rows doesn't match this). You should use IN (also you can use OR)
#RSP =
CALCULATE(DISTINCTCOUNT('Table'[CustomerCode]),FILTER(ALL('Table'),'Table'[Segmentation] in ("abc","cdf","opr")))
IF you want to find customers, that have rows with more than one segmentation code:
#RSP_2 =
var __custSeg1 = filter('Table'[CustomerCode], TREATAS({"abc"},
'Table'[Segmentation])
var __custSeg2 = filter('Table'[CustomerCode], TREATAS({"cdf"},
'Table'[Segmentation])
var __custSeg2 = filter('Table'[CustomerCode], TREATAS({"opr"},
'Table'[Segmentation])
return
calculate(DISTINCTCOUNT('Table'[CustomerCode]), __custSeg1 ,__custSeg2
,__custSeg2 )
I've got this data:
Then have this measure:
amount = SUM( play[amount] )
Then I've tried to use the ASC/DESC arguments of the TOPN function in these two measures:
Top 2 customer per category ASC =
VAR rnk = VALUES( play[customer] )
RETURN
CALCULATE(
[amount],
TOPN(
2,
ALL( play[customer] ),
[amount],
ASC
),
RNK
)
Top 2 customer per category DESC =
VAR rnk = VALUES( play[customer] )
RETURN
CALCULATE(
[amount],
TOPN(
2,
ALL( play[customer] ),
[amount],
DESC
),
RNK
)
Now if I use these two measures it looks like the following:
What is going on?
Why is the measure Top 2 customer per category ASC showing nothing?
How do I amend that measure so that it shows values for the bottom two values of each category?
The problem here is that the second argument of TOPN should be a table, not an unfiltered column.
Regardless of what the category is, ALL(play[customer]) returns the table:
customer
--------
xx
yy
zz
jj
qq
ff
The measure [amount] is still evaluated within the category filter context though so for category = "a" you get
customer [amount]
------------------
xx 10
yy 12
zz 13
jj
qq
ff
and for category = "b" you get
customer [amount]
------------------
xx
yy
zz
jj 15
qq 16
ff 9
These blanks are considered smaller than any number so they are what gets selected when you sort ASC.
Try this slightly modified measure instead:
Top 2 customer per category ASC =
VAR rnk = VALUES ( play[customer] )
RETURN
CALCULATE (
[amount],
TOPN ( 2, CALCULATETABLE ( play, ALL ( play[customer] ) ), [amount], ASC ),
RNK
)
Using CALCULATETABLE, the category filter context gets preserved.
P.S. To generate the tables above you can write a new calculated table like this:
Top2Table =
CALCULATETABLE (
ADDCOLUMNS ( ALL ( play[customer] ), "amount", [amount] ),
play[category] = "a" <or "b">
)
i am trying to build a simple shiny application with the following layout, 2X2 layout
Data1 Data2
Stats1 stats2
tabBox(title = "Data Review",id= "ttabs", width = 20, height = "420px",
tabPanel("Data",
column(3, dataTableOutput("data1"))
, column(3, verbatimTextOutput("sum1")),
, column(3, offset = 1,dataTableOutput("data2"))
, column(3, offset = 1, verbatimTextOutput("sum2"))
)
data 2 and stats2 appear as expected (one below the other), however data1 and stat1 appear side by side. Not sure how to update the code to get the expected result
How about use fluidRow()?
ui <- fluidPage(
tabBox(title="Data Review", id="ttabs", width=20, height=420,
tabPanel("Data",
fluidRow(
column(6, dataTableOutput("data1")),
column(6, dataTableOutput("data2")) ),
fluidRow(
column(6, verbatimTextOutput("sum1")),
column(6, verbatimTextOutput("sum2")) )
)
)
)
I hope this code help you.