keeping same height of row for DT table in output - r

I want to set the height of row constant or fixed for DT table output. for the table below you can see difference in height of rows.
so the scenario is when the number of character increases for eg second row in this case in first col then height should get adjust as same for all rows.
df <- data.frame(`quote` = c("the trader belongs to","trader have long ralationship withthe same market with my opinion on thaw its implemented mmnnhhh sdfghj fghj kjhgf tyui cvbnm",
"types of trads happens everytime when market slow","trades have leverage with",
"market is continious with the same platform trades"),
`share`= c(43,65,92,23,73),
`times` = c(86,98,55,12,08),
`parts`=c(4,7,4,8,3))
df<-datatable(df,
options = list(columnDefs =
list(list(className = 'dt-center',
targets = "_all"))),rownames = FALSE)
df
like in flextable i can do something like below but looking for fixed solution or any function for DT tables.
(ncols %in% c(4,5)) {
fl<-width(flxtable, width = d*0.3, j = 1)
fl<-width(flxtable, width = (d*0.7)/(ncols-1), j = 2:ncols)
d is left and right margin of docs template

I had success with this in the past:
DT::datatable(df) %>%
DT::formatStyle(names(df),lineHeight='80%')
Kudos to the post that helped me, however I fail to find it back now.

Related

How to have multiple font sizes in flextable header?

I have a flextable() where I want to make the header have two font sizes. One size for the main text (say, 20 pt font), and a smaller for the parenthetical text (12 pt font). Here's code that makes the entire header 20 pt.
Note: In the final solution, I'd prefer not to make the parenthetical text an entirely new row unless it can be as near to the original text as the below example shows.
library(flextable)
library(dplyr)
set_flextable_defaults(font.size = 20) #Works but makes all header size 20
flextable(test) %>%
set_header_labels(values = list(name = "Name", med_score = "Score (Median)", mean_score = "Score (Mean)")) %>%
align(align = "center", part = "header")
EDIT: Pictures added
Here's what it looks like currently:
And here's what I want it to look like:
Note: I made this goal table in PowerPoint. It looks different in many ways from the original one because I haven't formatted the original. The only difference that I'm trying to replicate is having (median) and (mean) being smaller than Score as well as them being in the same header cell.
You could use compose, as_paragraph and as_chunk to achieve it.
For reference: https://ardata-fr.github.io/flextable-book/cell-content-1.html#multi-content
library(flextable)
ft <- flextable(airquality[ sample.int(10),])
compose(
ft,
j = "Ozone",
part = "header",
value = as_paragraph(
"Ozone ",
as_chunk(
" (Median)",
props = fp_text_default(color = "#006699", font.size = 5)
)
)
)

How to hide digits from a rhandsontable element in Shiny

I understood that rhandsontable is based on the general handsontable JS libary itself using the numebro JS library.
However it seems that numebro does not allow to round the display in R of a figure in a hot table such as 13.4 to 13 and 13.6 to 14 (e.g. hidding digits, just in the display, not in the actual underlying data) which is a quite basic need and something DT can manage quite well. Is there a way to handle the hot display without modifying underlying data? I think this is the same question as this. Using the 'a' format is not a solution since for instance 1,154,521.5 will apear as 1.1m.
Below an example from jrowen's github official page:
DF = data.frame(int = 1:10, float = rnorm(10), cur = rnorm(10) * 1E5,
lrg = rnorm(10) * 1E8, pct = rnorm(10))
rhandsontable(DF, width = 550, height = 300) %>%
hot_col("float", format = "0.0") %>%
hot_col("cur", format = "$0,0.00") %>%
hot_col("lrg", format = "0a") %>%
hot_col("pct", format = "0%")
How to make the float 0.2 simply 0 until we edit the cell? Or the -13.18% simply -13%?

Best way to double space table with flextable?

Using package flextable, and reading from the different help pages, I could not find how to double (line) space an entire table, as APA requires.
There's the option hrule():
ft_1 <- flextable(head(iris))
ft_1 <- height(ft_1, height = 1, part = "body")
ft_1 <- height(ft_1, height = 1, part = "head")
ft_1 <- hrule(ft_1, rule = "exact", part = "body")
ft_1 <- hrule(ft_1, rule = "exact", part = "head")
ft_1
Or padding():
ft_1 <- flextable(head(iris))
ft_1 <- padding(ft_1, padding = 20, part = "all")
ft_1
But in both cases, when I check in Word, it doesn't correspond to double (line) spacing (it is still single spacing there).
Question
Is there a better option to make sure it 100% corresponds to double line spacing ("Line and Paragraph Spacing" in Word)?
I assume that using hrule() or padding could create various formatting problems later on, or editors might complain it doesn't correspond to the right kind of double spacing (e.g., it doesn't respond to changing spacing manually after so its harder to change through conventional means).
Or are we expected to manually change to double spacing in Word manually after exporting? Thank you.
I have created a new function named line_spacing (flextable >= 0.5.11). It should do what you want:
ft_1 <- qflextable(head(iris))
ft_1 <- line_spacing(ft_1, space = 1.5, part = "all")
ft_1

Incrementing to new row with gs_edit_cells once previous row is populated

I'm making a shiny app that acts as a visitor counter. It takes user input from action buttons then updates a Googlesheet with a table of values (date,semester,game,count). I can populate the first value of the of the input, however I'm not quite sure how to "increment" gs_edit_cells() as it only lets me specify one specific anchor cell.
I've tried using gs_add_row(), however it doesn't agree with the dimensions of the input, as its say it needs to be of value 0? I've scoured here, as this seems like it should be a trivial issue to fix.
Server:
the variable data is of the form
data <- c("date","semester","games","num")
saveData <- function(data) {
# Grab the google sheet
table <- gs_title("Game_Table")
show(data)
gs_edit_cells(ss = table,
ws = 1,
input = data,
anchor = "A1",
byrow = TRUE,
col_names = FALSE,
trim = TRUE)
}
The first cell of the table updates as expected:
17905 Fall Table Tennis 7
but when I run it again, it overwrites the old data.
You can add new rows with either gs_add_row() or gs_edit_cells(), but in both cases you previously must have added at least one row and the header (for example, with gs_edit_cells()). In your case, they didn't work for different reasons:
1. gs_add_row
The documentation of the function specifies that:
At the moment, this function will only work in a sheet that has a proper header row of variable or column names and at least one pre-existing data row.
In your case, it didn't work probably because your spreadsheets didn't have a header. A solution is to create the first row and header information with gs_edit_cells() before you use gs_add_row().
2. gs_edit_cells
You used anchor = "A1" in gs_edit_cells(), so every time the spreadsheets was updated from the cell A1 (consequently, overwriting your previous content). The solution is to read the current content of the spreadsheets to know the number of rows and specify the cell where you need to start the update without overwriting past content (keep in mind that the header populates one row in the spreadsheets).
Example code (with both solutions):
data <- data.frame(date = 17905, semester = "Fall", games = "Table Tennis", num = 7)
new_data <- data.frame(date = 17905, semester = "Fall", games = "Table Tennis", num = 7)
# Grab the google sheet
table <- gs_title("Game_Table")
# save first row of data with header (run only one time)
gs_edit_cells(ss = table,
ws = 1,
input = data,
anchor = "A1",
byrow = TRUE,
trim = TRUE)
# solution 1: add new rows with gs_add_rows
table <- gs_title("Game_Table")
gs_add_row(ss = table, input = new_data)
# solution 2: add new rows with gs_edit_cells
table <- gs_title("Game_Table")
last_row <- nrow(gs_read(table))
gs_edit_cells(ss = table,
ws = 1,
input = new_data,
anchor = paste0("A",last_row+2),
byrow = TRUE,
col_names = FALSE,
trim = TRUE)

Unselecting columns in DT::formatRound in Shiny when the columns change position due to user selection

I am making a shiny app (https://joshmyers.shinyapps.io/WAINorms/). The third tab, "Normative Data", displays a datatable based on a couple of user inputs. My output code for the table is:
output$table = renderDataTable({
datatable(norms.df(), options = list(
lengthMenu = list(c(50, 100, 200, -1), c('50', '100', '200', 'All')),
pageLength = 100), rownames = FALSE, class = 'white-space: nowrap stripe hover')
%>% formatRound(columns = -c(1:2), digits = 2)
})
The formatRound function rounds all of the columns except the first two to 2 decimal places. The problem is that this also rounds the "Percentile" column, which I don't want.
The issue is that it switches places depending on the selection of "Ethnic Specific" - if "No" Percentile is the 3rd column, but if "Yes" it becomes the 4th column, as another column, "Ethnicity", is inserted.
How can I round all columns to 2 decimal places except for "Measure", "Age", "Percentile", and "Ethnicity"?
This wasn't exactly what I wanted, but I can live with it. You can chain formatRound functions together (https://rstudio.github.io/DT/functions.html) so I could have rounded the 'Percentile' column to one decimal place which would have been acceptable. I decided to use formatSignif instead, however.
datatable(norms.df()) %>%
formatSignif("Percentile", 3) %>%
formatRound( -c(1:2), 2)

Resources