Black background with white font for first column in DT::DataTables - r

I have a shiny app with a DT::DataTable element in which the first column is a row header and the second column contains data. How can I change the color of the first column to be white text on a black background? If found ways to change the column headers (section 4.3 here), but I how do I get the same effect applied to the first column?
Here's some example code showing a very simplified version of the table without the desired effect. I'm certain that adding something to the options list in the renderDataTable function will solve it, but I don't know what to add.
EDIT: Below is a solution suggested by #Stéphane Laurent, which answers my original question. However, it makes the change to all tables present on the app. In my modified code, below, the global change is shown, but how do I target just one of the two tables?
library(shiny)
library(DT)
CSS <- HTML(
"td.firstcol {color: white; background-color: black}"
)
ui <- fluidPage(
tags$head(
tags$style(CSS)
),
fluidRow(
column(3,
DTOutput(outputId = 'tbl')
),
column(3,
DTOutput(outputId = 'tbl2')
)
)
)
server <- function(input, output) {
output$tbl<- renderDT({
datatable(
data.frame(
Label = c('Label1', 'Label2', 'Label3', 'Label4'),
Data = c('Data1', 'Data2', 'Data3', 'Data4')
),
rownames = FALSE,
colnames = "",
options = list(
dom = 't',
columnDefs = list(
list(targets = 0, className = "firstcol")
)
)
)
})
output$tbl2 <- renderDT({
datatable(
data.frame(
Label = c('Label1', 'Label2', 'Label3', 'Label4'),
Data = c('Data1', 'Data2', 'Data3', 'Data4')
),
rownames = FALSE,
colnames = "",
options = list(
dom = 't',
columnDefs = list(
list(targets = 0, className = "firstcol")
)
)
)
})
}
# Run the application
shinyApp(ui = ui, server = server)

library(shiny)
library(DT)
CSS <- HTML(
"td.firstcol {color: white; background-color: black}"
)
ui <- fluidPage(
tags$head(
tags$style(CSS)
),
fluidRow(
column(3,
DTOutput(outputId = 'tbl')
)
)
)
server <- function(input, output) {
output$tbl<- renderDT({
datatable(
data.frame(
Label = c('Label1', 'Label2', 'Label3', 'Label4'),
Data = c('Data1', 'Data2', 'Data3', 'Data4')
),
rownames = FALSE,
colnames = "",
options = list(
dom = 't',
columnDefs = list(
list(targets = 0, className = "firstcol")
)
)
)
})
}
# Run the application
shinyApp(ui = ui, server = server)

Related

Data table greater than WellPanel in Shiny

How can I increase the size of the WellPanel to have my table completely inside the "grey" box?
image here
(I'm new in Shiny and edited the table due to confidential data).
I tried
wellPanel(
fluidRow(
fluidPage(
and only
wellPanel(
fluidRow(
None of then works
wellPanel(
fluidRow(
fluidPage(
headerPanel(""),
column(12, align="center",
output$dataprint_controles <- DT::renderDataTable({
datatable(data,
rownames = FALSE,
options = list(paging = TRUE,
scrollX = TRUE,
searching = TRUE,
ordering = FALSE,
autoWidth = FALSE,
names = TRUE,
columnDefs = list(list(visible = FALSE, targets = esconder),
list(className = 'dt-center', targets = "_all")
),
dom = '<"sep">',
headerCallback = DT::JS(stringr::str_glue(
"function(thead) {{",
" $(thead).closest('thead').find('th').css('border-top', '2px solid black');",
" $(thead).closest('thead').find('th').css('border-left', '0px solid black');",
" $(thead).closest('thead').find('th').css('background', '#D9D9D9');",
" $(thead).closest('thead').find('th').css('color', 'black');",
" $(thead).closest('thead').find('th').css('text-align', 'center');",
"}}"
))
)
)
}),
),
)
), # fecha fluid
), # fecha well panel
fluidPage should be the most outer element of the UI, and column must be contained in fluidRow. The wellPanel does not go beyond a width of 100%, so you have to add the CSS propert width: fit-content;.
library(shiny)
library(DT)
mat <- matrix(rnorm(5*14), nrow = 5, ncol = 14)
ui <- fluidPage(
fluidRow(
column(
12,
wellPanel(
style = "width: fit-content;",
DTOutput("dtable")
)
)
)
)
server <- function(input, output, session) {
output$dtable <- renderDT({
datatable(mat)
})
}
shinyApp(ui, server)

How to arrange user input checkboxes (or other user inputs) neatly in a grid pattern?

In the below MWE code I'd like to find a neat, organised way to arrange user checkbox inputs. The fluidRows and columns that I use are hard to work with and present cleanly. In the full App this MWE derives from, there are 12 user checkboxes so I need to find a clean way to arrange them and place them close together. As you can see in the below image and when running the code, the buttons do not line up in the rows correctly, the rows are too tall, gridlines would be helpful, etc.
The below code only uses "Show" and "Hide" checkboxes for the sake of clarity.
MWE code:
rm(list = ls())
library(shiny)
library(shinyMatrix)
library(shinyjs)
firstInput <- function(inputId){
matrixInput(inputId,
value = matrix(c(5), 1, 1, dimnames = list(c("1st input"),NULL)),
rows = list(extend = FALSE, names = TRUE),
cols = list(extend = FALSE, names = FALSE, editableNames = FALSE),
class = "numeric")}
secondInput <- function(inputId,x){
matrixInput(inputId,
value = matrix(c(x), 1, 1, dimnames = list(c("2nd input"),NULL)),
rows = list(extend = FALSE, names = TRUE),
cols = list(extend = FALSE, names = FALSE, editableNames = FALSE),
class = "numeric")}
ui <- fluidPage(
titlePanel("Model"),
sidebarLayout(
sidebarPanel(
uiOutput("panel"),
hidden(uiOutput("secondInput"))),
mainPanel(plotOutput("plot1"))
)
)
server <- function(input, output, session) {
input1 <- reactive(input$input1)
input2 <- reactive(input$input2)
output$panel <- renderUI({
tagList(
useShinyjs(),
firstInput("input1"),
strong(helpText("Generate curves (Y|X):")),
div(style = "font-size: 14px; padding: 0px; margin-top:0em",
fluidRow(
fluidRow(
column(5,),
column(2,helpText("Show"),align="center"),
column(2,helpText("Hide"),align="center"),
column(2,helpText("Reset"),align="center")
),
div(style = "font-size: 14px; padding: 0px; margin-top:0em",
fluidRow(
column(5, helpText("1st input"),offset = 1),
column(2, checkboxInput('show', NULL, value = FALSE, width = NULL)),
column(2, checkboxInput('hide', NULL, value = FALSE, width = NULL)),
column(2,)
)
),
div(style = "font-size: 14px; padding: 0px; margin-top:0em",
fluidRow(
column(5,helpText("2nd input"),offset = 1),
column(2,),
column(2,),
column(2,)
)
)
)
)
)
})
output$secondInput <- renderUI({
req(input1())
secondInput("input2",input$input1[1,1])
})
outputOptions(output, "secondInput", suspendWhenHidden = FALSE)
output$plot1 <-renderPlot({
req(input2())
plot(rep(input2(),times=5))
})
observeEvent(input$show,{
shinyjs::show("secondInput")
updateCheckboxInput(session, "hide", value = FALSE)
})
observeEvent(input$hide,{
shinyjs::hide("secondInput")
updateCheckboxInput(session, "show", value = FALSE)
})
}
shinyApp(ui, server)
You can try to put these inputs in a table:
library(shiny)
f <- function(action, i){
as.character(
checkboxInput(paste0(action, i), label = NULL)
)
}
actions <- c("shovv", "hide", "reset")
tbl <- t(outer(actions, c(1,2), FUN = Vectorize(f)))
colnames(tbl) <- c("Shovv", "Hide", "Reset")
rownames(tbl) <- c("1st input", "2nd input")
ui <- fluidPage(
br(),
tableOutput("checkboxes")
)
server <- function(input, output){
output[["checkboxes"]] <- renderTable({
tbl
},
rownames = TRUE, align = "c",
sanitize.text.function = function(x) x
)
observe({
print(input[["hide1"]])
})
}
shinyApp(ui, server)
With a bit of CSS, it renders well in the sidebar:
ui <- fluidPage(
tags$head(
tags$style(HTML(
"td .checkbox {margin-top: 0; margin-bottom: 0;}
td .form-group {margin-bottom: 0;}"
))
),
br(),
sidebarLayout(
sidebarPanel(
tableOutput("checkboxes")
),
mainPanel()
)
)

Wide datatables causing scrollx to scroll back when applying filters

I am able to render a datatable in my shiny app. However, whenever there is a wide table, the horizontal scroller gets back to it's initial position when you apply filters on the columns in the back. This issue occurs with numeric columns only.
I was wondering if there is a way I can disable range-based filters (but keep the filters itself) or if there is any other workaround for this problem.
I have searched github issues and stackoveflow prior to posting this question here since I couldn't find anybody having this problem.
Here is a reproducible example along with pictures-
options(scipen = 99999) #converts the sci numbers to their regular format
library(shiny)
library(shinyjs)
library(shinyWidgets)
library(shinythemes)
library(writexl)
library(dplyr)
library(DT)
library(dplyr)
mtcars_modified <- mtcars %>% dplyr::mutate(wt_2= wt,
qsec_2 = qsec,
am_2= am,
mpg_2= mpg,
gear_2 = gear,
carb_2 = carb,
disp_2 = disp,
row_names_col= rownames(mtcars))
ui <- fluidPage(
theme = shinythemes::shinytheme("simplex"),
shinyjs::useShinyjs(), # enables javascript/jQuery enhanchments
# Create Right Side Text
navbarPage(
id = "navbar",
title= div(HTML("G<em>T</em>")),
#windowTitle = "GT",
tabPanel("Data Set Info",
materialSwitch(inputId = "toggleSidebar", label = "Toggle Panel: ",
value = TRUE, status = "warning"),
sidebarLayout(
# radio/action buttons
sidebarPanel(
id = "Sidebar",
prettyRadioButtons(
inputId = "controller",
label = "Choose:",
choices = c("About"= 1,
"iris"= 2,
"mtcars_modified" = 3),
icon= icon("check"),
selected = 1,
status = "success",
animation="smooth"
),
br(),
br()
),
#panel where output is shown from server
mainPanel(
id = "main_panel",
tabsetPanel(
id = "hidden_tabs",
type = "hidden",
tabPanelBody(
"panel1", "navigation"
),
tabPanelBody(
"panel2",
tabsetPanel(
tabPanel("Data", DT::DTOutput('panel1_data')),
tabPanel("Summary", verbatimTextOutput("panel1_sum")),
tabPanel(
"Plot"
)
)
),
tabPanelBody(
"panel3",
tabsetPanel(
tabPanel("Data", DT::DTOutput('panel3_data')),
tabPanel("Summary", verbatimTextOutput("panel3_sum")),
tabPanel(
"Plot"
)
)
)
)
)
)
) ,
#resizes the navbar tabs/button
tags$head(tags$style(HTML('.navbar-brand {width: 270px; font-size:35px; text-align:left;
font-family: "serif";')))
)
)
server <- function(input, output, session) {
# this event hides the side panel when toggled on/off
observeEvent(input$toggleSidebar, {
shinyjs::toggle(id = "Sidebar", condition = input$toggleSidebar)
if(!isTRUE(input$toggleSidebar)) {
shinyjs::runjs("$('#main_panel').removeClass('col-sm-8').addClass('col-sm-12')")
} else {
shinyjs::runjs("$('#main_panel').removeClass('col-sm-12').addClass('col-sm-8')")
}
})
# here we put all the data
data_sets <- list(df1 = data.frame(),
df2= iris,
df3 = mtcars_modified)
# store current dataset in reactive so we can work with plot panels
data_to_use <- reactiveValues(name = "df", data = data.frame())
observeEvent(input$controller, {
# skip first panel since it is used to display navigation
updateTabsetPanel(session, inputId= "hidden_tabs", selected = paste0("panel", input$controller))
# enswure value is avilable throught selected tabSet
req(input$controller)
# get current data and df name
data_to_use$data <- data_sets[[as.numeric(input$controller)]]
data_to_use$name <- names(data_sets[as.numeric(input$controller)])
# update table and sum
output[[paste0('panel', input$controller, '_data')]] <-
DT::renderDT(server = FALSE, {
DT::datatable(data_to_use$data,
filter = 'top',
extensions = 'Buttons',
options = list(scrollY = 600,
scrollX = TRUE,
dom = '<"float-left"l><"float-right"f>rt<"row"<"col-sm-4"B><"col-sm-4"i><"col-sm-4"p>>',
lengthMenu= list(c(10, 25, 50, -1),
c('10', '25', '50','All')),
buttons = list(
list(extend = "collection", text = "Download",
filename = "data_excel",
exportOptions = list(
modifier = list(page = "all")
),
action = DT::JS("function ( e, dt, node, config ) {
Shiny.setInputValue('Download_DATA', true, {priority: 'event'});}"
)
)
),
scrollCollapse= TRUE,
lengthChange = TRUE,
widthChange= TRUE,
rownames = TRUE))})
output[[paste0('panel', input$controller, '_sum')]] <- renderPrint(summary(data_to_use$data))
})
}
#runs the app
shinyApp(ui= ui, server= server)

How to fix filter options not popping up for esquisserUI in Shiny?

I have some requests for my app.
{1} After readjusting the mainPanel, esquisserUI filters are not popping up anymore. Here is the working example which I followed https://dreamrs.github.io/esquisse/articles/shiny-usage.html
In addition, I also looked at this GitHub issue, however it was for disabling the filters: https://github.com/dreamRs/esquisse/issues/71
And final request regarding general information:
{2} what does server = FALSE will do for huge datasets? (https://rstudio.github.io/DT/server.html) DT recommends to leave it as default to TRUE state, however if I do that, I don't get the full data upon download. I only get the data in the current page. Are there problems you foresee?
Thank you, here is a reproducible example.
library(shiny)
library(shinyjs)
library(shinyWidgets)
library(esquisse)
library(shinythemes)
library(xlsx)
library(DT)
# Credit: #Iz100 helped me a lot with UI.
ui <- fluidPage(
theme = shinytheme("simplex"),
useShinyjs(),
# Create Right Side Text
navbarPage(
title= div(HTML("G<em>T</em>")),
tabPanel("Data Set Info",
materialSwitch(inputId = "toggleSidebar", label = "Toggle Panel: ",
value = TRUE, status = "warning"),
sidebarLayout(
# radio/action buttons
sidebarPanel(
id = "Sidebar",
prettyRadioButtons(
inputId = "controller",
label = "Choose:",
choices = c("About"= 1,
"iris"= 2,
"mtcars" = 3),
icon= icon("check"),
selected = 1,
status = "success",
animation="smooth"
),
br(),
br(),
a(actionButton(inputId = "admin_email", label = "Contact Admin",
icon = icon("envelope", lib = "font-awesome")),
href="mailto:my_awesome_email_address.com")
),
#panel where output is shown from server
mainPanel(
id = "main_panel",
tabsetPanel(
id = "hidden_tabs",
type = "hidden",
tabPanelBody(
"panel1", "navigation"
),
tabPanelBody(
"panel2",
tabsetPanel(
tabPanel("Data", DT::DTOutput('panel1_data')),
tabPanel("Summary", verbatimTextOutput("panel1_sum")),
tabPanel(
"Plot",
esquisserUI(
id = "esquisse2",
header = FALSE,
choose_data = FALSE
)
)
)
),
tabPanelBody(
"panel3",
tabsetPanel(
tabPanel("Data", DT::DTOutput('panel3_data')),
tabPanel("Summary", verbatimTextOutput("panel3_sum")),
tabPanel(
"Plot",
esquisserUI(
id = "esquisse3",
header = FALSE,
choose_data = FALSE
)
)
)
)
)
)
)
) ,
#resizes the navbar tabs/button
tags$head(tags$style(HTML('.navbar-brand {width: 270px; font-size:35px; text-align:left;}')))
)
)
server <- function(input, output, session) {
# this event hides the side panel when toggled on/off
observeEvent(input$toggleSidebar, {
shinyjs::toggle(id = "Sidebar", condition = input$toggleSidebar)
if(!isTRUE(input$toggleSidebar)) {
shinyjs::runjs("$('#main_panel').removeClass('col-sm-8').addClass('col-sm-12')")
} else {
shinyjs::runjs("$('#main_panel').removeClass('col-sm-12').addClass('col-sm-8')")
}
})
myModal <- function() {
div(id = "Download_DATA",
modalDialog(easyClose = TRUE,
title = "Alert!",
"Please remove all the filters if you want a full dataset.",
br(),
br(),
downloadButton("download_excel","Download as XLSX")
)
)
}
# here we put all the data
data_sets <- list(df1 = data.frame(),
df2= iris,
df3 = mtcars)
# store current dataset in reactive so we can work with plot panels
data_to_use <- reactiveValues(name = "df", data = data.frame())
# modules only need to be called it once but individually for esquisse
callModule(
module = esquisserServer,
id = "esquisse2",
data = data_to_use
)
callModule(
module = esquisserServer,
id = "esquisse3",
data = data_to_use
)
observeEvent(input$controller, {
# skip first panel since it is used to display navigation
updateTabsetPanel(session, inputId= "hidden_tabs", selected = paste0("panel", input$controller))
# enswure value is avilable throught selected tabSet
req(input$controller)
# get current data and df name
data_to_use$data <- data_sets[[as.numeric(input$controller)]]
data_to_use$name <- names(data_sets[as.numeric(input$controller)])
# update table and sum
output[[paste0('panel', input$controller, '_data')]] <- DT::renderDT(server = FALSE, {
DT::datatable(data_to_use$data,
filter = 'top',
extensions = 'Buttons',
options = list(scrollY = 600,
scrollX = TRUE,
dom = '<"float-left"l><"float-right"f>rt<"row"<"col-sm-4"B><"col-sm-4"i><"col-sm-4"p>>',
lengthMenu= list(c(10, 25, 50, -1),
c('10', '25', '50','All')),
buttons = list(
list(extend = "collection", text = "Download",
filename = "data_excel",
exportOptions = list(
modifier = list(page = "all")
),
action = DT::JS("function ( e, dt, node, config ) {
Shiny.setInputValue('Download_DATA', true, {priority: 'event'});}"
)
)
),
scrollCollapse= TRUE,
lengthChange = TRUE,
widthChange= TRUE,
rownames = TRUE))})
output[[paste0('panel', input$controller, '_sum')]] <- renderPrint(summary(data_to_use$data))
})
# observes if download is clicked
observeEvent(input$Download_DATA, {
showModal(myModal())
})
# writes to an excel file
output$download_excel <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".xlsx", sep="")
},
content = function(file) {
write.xlsx(data_to_use$data, file, row.names = FALSE)
}
)
}
#runs the app
shinyApp(ui= ui, server= server)
1. I checked the HTML of esquisserUI, they give all dropdowns the same IDs if you use multiple esquisserUI. This is a big NO in HTML development and will cause a lot of issues. They call it a module, but they didn't follow Shiny module guidelines where to use NS() for all UI IDs. The easy proof is try this below. Then uncomment the second set of esquisserUI and esquisserServer and try again. You will find the dropdown no longer works.
library(esquisse)
ui <- fluidPage(
esquisserUI(
id = "esquisse1",
header = FALSE,
choose_data = FALSE
)#,
# esquisserUI(
# id = "esquisse2",
# header = FALSE,
# choose_data = FALSE
# )
)
server <- function(input, output, session) {
data_to_use <- reactiveValues(data = iris, name = "iris")
callModule(
module = esquisserServer,
id = "esquisse1",
data = data_to_use
)
# callModule(
# module = esquisserServer,
# id = "esquisse2",
# data = data_to_use
# )
}
shinyApp(ui, server)
Currently there is no straight fix for this unless you ask them to fix it. We need to use a workaround:
I added a new tab to the main panel called "plot" which is the esquisserUI, and two buttons in the data panel so when you click on the button, it will jump you to the plot panel with the right data.
library(shiny)
library(shinyjs)
library(shinyWidgets)
library(esquisse)
library(shinythemes)
library(xlsx)
library(DT)
# Credit: #Iz100 helped me a lot with UI.
ns <- NS("myapp")
ui <- fluidPage(
theme = shinytheme("simplex"),
useShinyjs(),
# Create Right Side Text
navbarPage(
title= div(HTML("G<em>T</em>")),
tabPanel("Data Set Info",
materialSwitch(inputId = "toggleSidebar", label = "Toggle Panel: ",
value = TRUE, status = "warning"),
sidebarLayout(
# radio/action buttons
sidebarPanel(
id = "Sidebar",
prettyRadioButtons(
inputId = "controller",
label = "Choose:",
choices = c("About"= 1,
"iris"= 2,
"mtcars" = 3,
"plots" = 4),
icon= icon("check"),
selected = 1,
status = "success",
animation="smooth"
),
br(),
br(),
a(actionButton(inputId = "admin_email", label = "Contact Admin",
icon = icon("envelope", lib = "font-awesome")),
href="mailto:my_awesome_email_address.com")
),
#panel where output is shown from server
mainPanel(
id = "main_panel",
tabsetPanel(
id = "hidden_tabs",
type = "hidden",
tabPanelBody(
"panel1", "navigation"
),
tabPanelBody(
"panel2",
tabsetPanel(
tabPanel(
"Data", DT::DTOutput('panel2_data'),
actionButton("plot2", "Plot iris")
),
tabPanel("Summary", verbatimTextOutput("panel2_sum"))
)
),
tabPanelBody(
"panel3",
tabsetPanel(
tabPanel(
"Data", DT::DTOutput('panel3_data'),
actionButton("plot3", "Plot mtcars")
),
tabPanel("Summary", verbatimTextOutput("panel3_sum"))
)
),
tabPanelBody(
"panel4",
esquisserUI(
id = "esquisse",
header = FALSE,
choose_data = FALSE
)
)
)
)
)
),
#resizes the navbar tabs/button
tags$head(tags$style(HTML('.navbar-brand {width: 270px; font-size:35px; text-align:left;}')))
)
)
server <- function(input, output, session) {
# this event hides the side panel when toggled on/off
observeEvent(input$toggleSidebar, {
shinyjs::toggle(id = "Sidebar", condition = input$toggleSidebar)
if(!isTRUE(input$toggleSidebar)) {
shinyjs::runjs("$('#main_panel').removeClass('col-sm-8').addClass('col-sm-12')")
} else {
shinyjs::runjs("$('#main_panel').removeClass('col-sm-12').addClass('col-sm-8')")
}
})
myModal <- function() {
div(id = "Download_DATA",
modalDialog(easyClose = TRUE,
title = "Alert!",
"Please remove all the filters if you want a full dataset.",
br(),
br(),
downloadButton("download_excel","Download as XLSX")
)
)
}
# here we put all the data
data_sets <- list(df1 = data.frame(),
df2= iris,
df3 = mtcars)
# store current dataset in reactive so we can work with plot panels
data_to_use <- reactiveValues(name = "df", data = data.frame())
# modules only need to be called it once but individually for esquisse
callModule(
module = esquisserServer,
id = "esquisse",
data = data_to_use
)
# go to plot panel if plot button clicked
observeEvent(c(input$plot2, input$plot3), {
updatePrettyRadioButtons(session, "controller", selected = 4)
}, ignoreInit = TRUE)
observeEvent(input$controller, {
# skip first panel since it is used to display navigation
updateTabsetPanel(session, inputId= "hidden_tabs", selected = paste0("panel", input$controller))
# enswure value is avilable throught selected tabSet
# only render data if data panels are selected
req(input$controller %in% 2:3)
# get current data and df name
data_to_use$data <- data_sets[[as.numeric(input$controller)]]
data_to_use$name <- names(data_sets[as.numeric(input$controller)])
# update table and sum
output[[paste0('panel', input$controller, '_data')]] <- DT::renderDT(server = FALSE, {
DT::datatable(data_to_use$data,
filter = 'top',
extensions = 'Buttons',
options = list(scrollY = 600,
scrollX = TRUE,
dom = '<"float-left"l><"float-right"f>rt<"row"<"col-sm-4"B><"col-sm-4"i><"col-sm-4"p>>',
lengthMenu= list(c(10, 25, 50, -1),
c('10', '25', '50','All')),
buttons = list(
list(extend = "collection", text = "Download",
filename = "data_excel",
exportOptions = list(
modifier = list(page = "all")
),
action = DT::JS("function ( e, dt, node, config ) {
Shiny.setInputValue('Download_DATA', true, {priority: 'event'});}"
)
)
),
scrollCollapse= TRUE,
lengthChange = TRUE,
widthChange= TRUE,
rownames = TRUE))})
output[[paste0('panel', input$controller, '_sum')]] <- renderPrint(summary(data_to_use$data))
})
# observes if download is clicked
observeEvent(input$Download_DATA, {
showModal(myModal())
})
# writes to an excel file
output$download_excel <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".xlsx", sep="")
},
content = function(file) {
write.xlsx(data_to_use$data, file, row.names = FALSE)
}
)
}
#runs the app
shinyApp(ui= ui, server= server)
2. So server = TRUE only sends a very small portion of the entire dataset to UI for large ones. When you scroll or jump pages, new data will be sent. This saves time and has better performance. If it is FALSE, all data will be sent at once. Imagine you need to load a 2GB table in your browser everytime you start the app, how slow will it be. For small datasets, you can leave it FALSE.
Updates
It seems esquisse people fixed the bug. Install the develop version and then:
ui <- fluidPage(
esquisse_ui(
id = "esquisse1",
header = FALSE
),
esquisse_ui(
id = "esquisse2",
header = FALSE
)
)
server <- function(input, output, session) {
data_to_use <- reactiveValues(data = iris, name = "iris")
esquisse_server(id = "esquisse1", data_rv = data_to_use)
esquisse_server(id = "esquisse2", data_rv = data_to_use)
}
shinyApp(ui, server)

DT SearchPanes Custom Filter

I'm trying to do something like is seen here, but I'm having trouble figuring out how to do it in Shiny. As an example, it would be great to have a filter for mtcars of "efficient" (cars with at least 15 mpg) or "inefficient" (cars with less than 15 mpg).
Here is some code:
library(shiny)
library(DT)
ui <- shinyUI(
fluidPage(DT::dataTableOutput("mtcars_table"))
)
server <- shinyServer(function(input, output, session) {
output$mtcars_table <-
DT::renderDT({
DT::datatable(
mtcars,
options = list(dom = 'Pfrtip',
columnDefs = list(
list(
searchPanes = list(show = TRUE), targets = 1
),
list(
searchPanes = list(show = FALSE), targets = 2:11
))),
extensions = c('Select', 'SearchPanes'),
selection = 'none'
)
}, server = FALSE)
})
shinyApp(ui = ui, server = server)
Here is something to try based on the DataTables example with custom filtering options.
For the additional list options, I included a label like "Efficient", as well as a javascript function for value (rowData[1] should reference the first column, mpg).
library(shiny)
library(DT)
ui <- shinyUI(
fluidPage(DT::dataTableOutput("mtcars_table"))
)
server <- shinyServer(function(input, output, session) {
output$mtcars_table <-
DT::renderDT({
DT::datatable(
mtcars,
options = list(
dom = 'Pfrtip',
columnDefs = list(
list(
searchPanes = list(
show = TRUE,
options = list(
list(
label = "Efficient",
value = JS(
"function(rowData, rowIdx) { return rowData[1] >= 15; }"
)
),
list(
label = "Inefficient",
value = JS(
"function(rowData, rowIdx) { return rowData[1] < 15; }"
)
)
)
),
targets = 1
),
list(
searchPanes = list(show = FALSE), targets = 2:11
)
)
),
extensions = c('Select', 'SearchPanes'),
selection = 'none'
)
}, server = FALSE)
})
shinyApp(ui = ui, server = server)

Resources