I have a shiny app that is currently working and I would like to add the lenghtMenu option in the datatable. It seems it is not working. I am not so sure if I place it in the wrong place in the R code. Thank you for looking into this.
Here is my code:
output$sbirx.view <- DT::renderDataTable(
{
input$submit1
if (input$submit1==0) return()
isolate({
datatable(dataset.filter(),
rownames=FALSE,
extensions = c("FixedColumns", "FixedHeader", "Scroller"),
options = list(searching=TRUE,
autoWidth=TRUE,
scroller=TRUE,
scrollX=TRUE,
#scrollY="500px",
scrollY=paste0(factor*nrow(dataset.filter()),"px"),
fixedHeader=TRUE,
class='cell-border stripe',
lengthMenu = c(5, 30, 50), pageLength = 5,
fixedColumns=list(leftColumns=2,heightMatch='none')
)
)
})
})
According to your comments this might help you:
The factor you have to choose according to your needs i guess.
shinyApp(
ui = fluidPage(
fluidRow(
column(12,
dataTableOutput('table')
)
)
),
server = function(input, output) {
output$table <- renderDataTable(iris,
rownames=FALSE,
extensions = c("FixedColumns", "FixedHeader", "Scroller"),
options = list(searching=TRUE,
# autoWidth=TRUE,
scroller=TRUE,
scrollX=TRUE,
scrollY=paste0(4*nrow(iris),"px"),
fixedHeader=TRUE,
class='cell-border stripe',
fixedColumns=list(leftColumns=2,heightMatch='none')
)
)
}
)
Related
This is my code, I want copy or print all, but it only print current page, thank you!
library(shiny)
library(DT)
ui <- fluidPage(
DT::dataTableOutput("tt")
)
server <- function(input, output, session) {
iris2 = head(iris, 20)
output$tt <- DT::renderDataTable(
iris2,
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons = c('copy', 'print')
)
)
}
shinyApp(ui, server)
The easiest way is to use the option server = FALSE.
output$tt <- renderDT({
datatable(
iris2,
extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons = c('copy', 'print')
)
)
}, server = FALSE)
I have a requirement to show 2 datatables in 2 different tabs in a shiny application. One is a simple datatable whereas the other uses the rowgroup extension of DT package. When I'm using tabs to display both the datatables, the rowgroup function doesnt seem to work.
library(shiny)
library(DT)
library(shinyjs)
ui <- fluidPage(
dashboardBody(
fluidRow(
tabBox(width = "100%",
tabPanel("Table 1",
fluidRow(
column(12, withSpinner(DTOutput("my_table2",height="600px")))
)
),
tabPanel("Table 2",
fluidRow(
column(12, withSpinner(DTOutput("my_table1",height="600px")))
)
)
)
)
)
)
server <- function(input, output) {
output$my_table1<-DT::renderDataTable({
datatable(mtcars[1:15,1:5],
extensions = 'RowGroup',
options = list(rowGroup = list(dataSrc=c(3)),
pageLength = 20),
callback = JS("
$('#DataTables_Table_0 tbody').on('click', 'tr.group', function () {
var rowsCollapse = $(this).nextUntil('.group');
$(rowsCollapse).toggleClass('hidden');
});"))
})
output$my_table2<-DT::renderDataTable({
datatable(mtcars[1:15,1:5],rownames = FALSE,
options = list(
order=list(list(1,'asc'),list(2,'asc')),
scrollX = TRUE,
pageLength = 100,
lengthMenu = c(10, 20, 100),
searching = TRUE)) %>%
formatStyle(0, target= 'row',color = 'black', backgroundColor = 'silver',
fontWeight ='normal', lineHeight='100%')
})
}
shinyApp(ui = ui, server = server)
But if I display the datatable with rowgroup in 1st tab, its working fine. But my requirement is to display the table with rowgroup in Tab 2.
Can someone please help
I am currently developing a shiny app and I face an issue with the dynamic alignment of the data table.
The code used is
ui.R
shinyUI(fluidPage(
dashboardPage(
dashboardBody(
tabItems(
tabItem(tabName = "view_id",
sidebarLayout(
sidebarPanel(width = 2, checkboxGroupInput("sidebar", "People Viewer",
sidebar_content)
),
mainPanel(width=10,style ="background-color:RGB(255,255,255); border-color:RGB(255,255,255);align:left;",
wellPanel(DTOutput("tab") )
)
)
)
)
)))
server.R
shinyServer(function(input, output) {
output$tab <- {
renderDT(datatable(pep_view[ ,input$sidebar, drop = FALSE ], filter = 'top', extensions = 'FixedColumns',
options = list(scrollX = TRUE,scrollY = "400px" ,fixedColumns = TRUE, pageLength = 10, autoWidth = TRUE
), class = 'cell-border stripe')
)
}
})
The output obtained is
Can anyone resolve this issue? Thanks in advance!!
When using the FixedColumns extension in DT, the rows_selected doesn't register any selections in Sepel.Length
Please see the below example...
Any suggestions on how to get around this would be appreciated.
library(DT)
library(shiny)
ui=shinyUI(
fluidPage(
DT::dataTableOutput("x3")
)
)
server=shinyServer(function(input, output) {
output$x3 = DT::renderDataTable(
DT:::datatable(
iris, rownames=FALSE,
extensions = c('FixedColumns'),
options = list(
fixedColumns = list(leftColumns = 1),
scrollX = TRUE)
))
observe({
s = input$x3_rows_selected
print(s)
})
})
shinyApp(ui=ui,server=server)
I'm asking here because I searched elsewhere and couldn't find an answer.
I'm wondering if/how you can reorder the rows of a datatable using input from R Shiny. The example below actually regenerates the table as the input changes, but I'm hoping that given a change in input, the same action as clicking the relevant sort button would happen. Any ways to achieve this?
Thanks in advance!
library(shiny)
ui = shinyUI(pageWithSidebar(
headerPanel('Examples of DataTables'),
sidebarPanel(
radioButtons('var', 'Variable to sort by',
c(mpg='mpg',
cyl='cyl'),
'cyl')
),
mainPanel(
dataTableOutput("mytable")
)
)
)
server = shinyServer(function(input, output) {
output$mytable = renderDataTable({
mtcars[order(mtcars[,input$var]),]
}, options = list(orderClasses = TRUE, LengthMenu = c(5, 25, 50), pageLength = 25))
})
shinyApp(ui,server)
You need a reactive function in your server, otherwise it wont react. I've also change = to <- which is more of a convention in R
library(shiny)
ui <- shinyUI(pageWithSidebar(
headerPanel('Examples of DataTables'),
sidebarPanel(
radioButtons('var', 'Variable to sort by',
c(mpg='mpg',
cyl='cyl'),
'cyl')
),
mainPanel(
dataTableOutput("mytable")
)
)
)
server <- shinyServer(function(input, output) {
sortTable <- reactive({
mtcars[do.call(order, mtcars[as.character(input$var)]),]
})
output$mytable <- renderDataTable({
sortTable()
}, options = list(orderClasses = TRUE, LengthMenu = c(5, 25, 50), pageLength = 25))
})
shinyApp(ui,server)