how to increase datatable height in R markdown - r

I'm facing this issue I want to see the first 10 rows but it gives scrolling by default.
datatable(data, options = list(), class = "display", fillContainer = T,
width = NULL, height = 1000, editable = FALSE)
Can anyone help, please?

To overcome this issue I set scrollY = "565px" in the options = list()

Related

Shiny DataTable - How to change filter size?

I've this Shiny app:
The filter size is too narrow so I can't see the whole words.
I want to make the filter box wider without changing the table/column size, just the filter box.
this is my code:
library(DT)
iris2 = iris[c(1:10, 51:60, 101:110), ]
# no clear buttons
datatable(iris2, options = list(autoWidth = TRUE), filter = list(
position = 'top', clear = FALSE
))
Is it possible?

Automatic line break in DT cell

I'm displaying a list in a cell of a DT table :
mylist <- paste0("name",1:20)
data <- data.frame(id = 1, members = I(list(mylist)))
DT::datatable(data)
The list appears on a single very long line:
I would like to have an automatic line break so that the list is split in many lines inside the cell, preferably according to the width of the cell.
[EDIT] this is the expected result, see accepted Answer :
I tried without success to set DT ColumnDefs options :
DT::datatable(data,options = list(
autoWidth= T,
columnDefs = list(list(width = "200" ))
))
Looks tricky as there seem to be an issue on width option
Thanks for your help
Instead of using a list you can use the string "name1, name2, ...".
mylist <- paste0("name",1:20)
data <- data.frame(id = 1, members = toString(mylist))
datatable(data, options = list(
columnDefs = list(
list(width = 200, targets = 2)
)
))

Is it possible to customize the size of rpivottable treemaps

This code works and the pivot table is drawn but the treemap is huge and I need it to render to about 50% of the current size, 500px would be fine for now. It seems to ignore the size settings. One pivot table overlaps the other when drawn.
output$pivot3 <- renderRpivotTable({
rpivotTable(data = dotsData,
rows="county",
cols="shortBreath",
width="500px",
height="500px",
aggregatorName = "Count",
aggregators = list(
Percentage = htmlwidgets::JS('$.pivotUtilities.aggregators["Count as Fraction of Columns"]'),
Count = htmlwidgets::JS('$.pivotUtilities.aggregators["Count"]')),
sorters="function(attr) {
var sortAs = $.pivotUtilities.sortAs;
if (attr == \"county\") {
return sortAs;
}
}")
})
I left a comment not that long ago of a method in which you could control the size by changing the JS. However, I have an R-based solution now.
I forked the Cran version of this package and modified it. My version of this package does allow you to control the size of the Treemap.
To install my modified packaged, use the following:
devtools::install_github("fraupflaume/rpivotTable")
You'll call the library the same way you did with the Cran version. To modify the treemap, you need to know that this is a D3 graph. All of the other graphs in this package are C3.
The following example reflects how you would indicate a specific size for either D3 or C3. (I've included several controllable options for C3 graphs, as well.)
library(rpivotTable)
rpivotTable(mtcars, rows = c("mpg", "am"), cols = "cyl",
width = "90%", height = "40%",
rendererOptions = list(
c3 = list(legend = list(show = FALSE),
data = list(labels = TRUE),
options = list(responsive = TRUE,
maintainAspectRatio = FALSE),
size = list(width = "600",
height = "500")),
d3 = list(size = list(width = "500", height = "500"))))
I expanded the viewer pane so that you could see it no longer fills the viewer.
This is the same view, but with the D3 controls removed.

DT::datatable – Format selected column?

Can you please help me with DT::datatable column formatting? I have for example this table:
DT::datatable(iris,
class = 'row-border stripe hover compact',
rownames = F,
autoHideNavigation = T,
options = list(pageLength = nrow(summary.month),
searching = F,
paging = F,
info = F))
I need to set:
1st column: bold, aligned left
3rd coumn: bold, aligned right
I found, that I should use columns.ClassName, but how to set the class styles in R?
The html output of datatable will be used in R markdown document then.
It has been a while since this question was initially asked, but I just had this same problem. Here is a simpler solution that doesn't require editing the source data or calling JS, but instead uses functions within the DT package itself.
DT::datatable(iris,
class = 'row-border stripe hover compact',
rownames = F,
autoHideNavigation = T, escape =FALSE) %>%
formatStyle(columns = c("Sepal.Length"), fontWeight = 'bold', `text-align` = 'left') %>%
formatStyle(columns = c("Petal.Length"), fontWeight = 'bold', `text-align` = 'right')
So far the only way I can get it to work is by manually setting the HTML tags first, and then using escape = FALSE
Here we wrap Sepal.Length in the bold HTML tag:
iris$SepalLength2 <- paste0("<b>", iris$Sepal.Length, "</b>")>
Then use escape = FALSE so that the HTML tags are parsed.
datatable(iris,
class = 'row-border stripe hover compact',
rownames = F,
autoHideNavigation = T, escape =FALSE)
Edit:
For align left/right, you can wrap in a <p align ="left"></p>
So: iris$SepalLength2 <- paste0('<p align ="right"><b>', iris$Sepal.Length, '</b></p>')
Note that I am neither an HTML guru, nor an expert on this particular library, but this seems like one way to get your desired result.
You don't need to modify the contents of your data. Instead, you can use the rowCallback option:
library(DT)
rowCallback <- c(
"function(row, data, index){",
" $(this.api().cell(index, 0).node())",
" .css('text-align', 'left')",
" .css('font-weight', 'bold');",
" $(this.api().cell(index, 2).node())",
" .css('text-align', 'right')",
" .css('font-weight', 'bold');",
"}"
)
DT::datatable(iris,
class = 'row-border stripe hover compact',
rownames = FALSE,
autoHideNavigation = TRUE,
options = list(pageLength = 5,
searching = FALSE,
paging = TRUE,
info = FALSE,
rowCallback = JS(rowCallback))
)

R Shiny set DataTable column width

I am trying to set the width of columns in a DataTable rendered in Shiny and am not able to implement it using the aoColumnDefs options. Has anyone tried this before ? My table has 1 text followed by 3 numeric columns. The numeric columns need to be narrower and the 1st column (text) wider.
output$result <- renderDataTable({
z <- as(dataInput(), "data.frame")
setnames(z, c("Rules", "Support", "Confidence", "StatDep"))
z
}, options = list(aLengthMenu = c(5, 30, 50), iDisplayLength = 5, bSortClasses = TRUE,
aoColumnDefs = list(sWidth = "50px", aTargets = list(1))))
Thanks,
Raj.
** Update ** This seems to be working, but there might be other options to do this as well.
output$result <- renderDataTable({
z <- as(dataInput(), "data.frame")
setnames(z, c("Rules", "Support", "Confidence", "StatDep"))
z
}, options = list(aLengthMenu = c(5, 30, 50), iDisplayLength = 5, bSortClasses = TRUE,
bAutoWidth = FALSE,
aoColumn = list(list(sWidth = "150px", sWidth = "30px",
sWidth = "30px", sWidth = "30px"))
))
Try this
#OUTPUT - dtdata
output$table <- DT::renderDataTable({
data.frame(a=c(1,2,3,4,5),b=c("A","B","C","D","E"))
},
options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '200px', targets = "_all"))
))
Sets the width of all columns to 200px.
To set width of selected columns, change targetsto a number or vector.
targets = c(1,3)
By the way, in case you're like me and never used DataTables before version 1.10 came out -- The examples above confused me at first, because they use the notation that was used in version 1.9 but 1.10 introduces new notation:
http://datatables.net/upgrade/1.10-convert
I've been using the new syntax, i.e.,
columnDefs instead of aoColumnDefs
http://datatables.net/reference/option/columnDefs
width instead of sWidth
http://datatables.net/reference/option/columns.width
etc.

Resources