I have a shinyApp, where I would like to capture the change in a specific directory.
i.e : the user click on the shinyDirButton, creates a sub-directory in a specific directory. I would like to capture any change in this directory (creation, deletion). I tried to use reactiveValues but I didn't succeed
library(shiny)
ui = fluidPage(sidebarLayout(
sidebarPanel(
class = "sidebar_upload",
id = "form",
tags$h1("1- Choose a folder"),
shinyFiles::shinyDirButton(
id = 'choose_directory',
label = 'Choose a folder',
title = 'Choose a folder',
multiple = F
),
br(),
br(),
br(),
actionButton("button", "Update")
),
mainPanel(uiOutput(outputId = "test"))
))
server <- function(input, output, session) {
r_global <- reactiveValues()
observe({
r_global$volumes = c(home = 'C:/')
r_global$dossier = list.dirs(r_global$volumes,
recursive = F,
full.names = F)
shinyFiles::shinyDirChoose(
input = input,
id = 'choose_directory',
roots = r_global$volumes,
session = session
)
})
observeEvent(input$button, {
print(r_global$dossier)
})
}
shinyApp(ui, server)
You have to replace your first observe by an observeEvent:
library(shiny)
ui = fluidPage(sidebarLayout(
sidebarPanel(
class = "sidebar_upload",
id = "form",
tags$h1("1- Choose a folder"),
shinyFiles::shinyDirButton(
id = 'choose_directory',
label = 'Choose a folder',
title = 'Choose a folder',
multiple = F
),
br(),
br(),
br(),
actionButton("button", "Update")
),
mainPanel(uiOutput(outputId = "test"))
))
server <- function(input, output, session) {
r_global <- reactiveValues()
#############################
### here add observeEvent ###
#############################
observeEvent(input$button, {
r_global$volumes = c(home = '~/project/SHINY/wedding/PROJET/')
r_global$dossier = list.dirs(r_global$volumes,
recursive = F,
full.names = F)
shinyFiles::shinyDirChoose(
input = input,
id = 'choose_directory',
roots = r_global$volumes,
session = session
)
})
observeEvent(input$button, {
print(r_global$dossier)
})
}
shinyApp(ui, server)
I have the below code which displays a datatable. Is it possible to change the color of the csv ,excel and columnVisibility buttons? I am not sure which CSS tags to change or modify to get the desired affect.
library(DT)
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Header"
),
dashboardSidebar( sidebarMenu(id = "tabs",
menuItem("Page1", tabName = "page1"))),
dashboardBody(
tabItems(
tabItem(
tabName = "page1",
tabBox(id="tabs",
tabPanel("tab1",
column(12,
DT::dataTableOutput("table1")
))
)
)
)
)
)
server <- function(input, output) {
output$table1 <- DT::renderDataTable({
datatable( data = mtcars,
options = DToptions,
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
}
shinyApp(ui, server)
We could add a javascript/jquery to change the colors of the buttons in the callback:
output$table1 <- DT::renderDataTable({
datatable( data = mtcars,
callback=JS('$("button.buttons-copy").css("background","red");
$("button.buttons-print").css("background","green");
return table;'),
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons = c('copy', 'print')
),
rownames = TRUE,
selection = 'none'
)
})
I am working with a shiny app where it is desired to have a single downloadButton in the header of the application that downloads the data table present in the current/active page/tab.
Below is a simple app that has two data tables in page1 and one in page 2. Each data table has the csv , excel buttons on top of each data table.
Could these csv, excel buttons be removed and place a single downloadButton in a fixed position in the header bar that offers to download csv/excel options of the active table in the current page or tab.
The idea is to have a single fixed downloadButton for the entire app in the header bar. Any possible solutions within shiny to do this or if anyone has attempted this before.
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Header",
dropdownMenuOutput("updatedTimeOutput"),
dropdownMenu(type = "notifications",
badgeStatus = "warning",
icon = icon("bullhorn", "fa-lg"),
notificationItem(icon = icon("bullhorn", "fa-1x"),
status = "info",
text = tags$span(
tags$b("Please notice!")
)
))),
dashboardSidebar( sidebarMenu(id = "tabs",
menuItem("Page1", tabName = "page1"),
menuItem("Page2", tabName = "page2"))),
dashboardBody(
tabItems(
tabItem(
tabName = "page1",
tabBox(id="tabs",
tabPanel("tab1",
column(12,
DT::dataTableOutput("table1")
)),
tabPanel( "tab2",
column(12,
DT::dataTableOutput("table2")
))
)
)
,
tabItem(
tabName = "page2",
fluidRow(
column(12,
DT::dataTableOutput("table3")
))
)
)
)
)
server <- function(input, output) {
output$table1 <- DT::renderDataTable({
datatable( data = mtcars,
options = DToptions,
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
output$table2 <- DT::renderDataTable({
datatable( data = mtcars,
options = DToptions,
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
output$table3 <- DT::renderDataTable({
datatable( data = mtcars,
options = DToptions,
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
}
shinyApp(ui, server)
(a) If you only want "one downloadButton visible in the header common to all pages that downloads the table in the active page or tab", it needs firstly to know the active page and tab based on the page / tab IDs. (b) If you only need a single button to download all the tables, you can download them into a .xlsx file (see download data onto multiple sheets from shiny). (c)If you need a button for each tab, place the button in each tab and you can simply save table as .csv.
Here is the code for situation (a).
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Header",
dropdownMenuOutput("updatedTimeOutput"),
dropdownMenu(type = "notifications",
badgeStatus = "warning",
icon = icon("bullhorn", "fa-lg"),
notificationItem(icon = icon("bullhorn", "fa-1x"),
status = "info",
text = tags$span(
tags$b("Please notice!")
)
))),
dashboardSidebar( sidebarMenu(id = "pages", # use unique id for pages
menuItem("Page1", tabName = "page1"),
menuItem("Page2", tabName = "page2"))),
dashboardBody(
# Add download button
downloadButton('downloadData', 'Download Table',
style="font-weight:bold;"
),
helpText(
hr(style = "border-top: 1px solid #000000;"),
),
tabItems(
tabItem(
tabName = "page1",
tabsetPanel(id="tabs",
tabPanel("tab1",
column(12,
DT::dataTableOutput("table1")
)),
tabPanel( "tab2",
column(12,
DT::dataTableOutput("table2")
))
)
)
,
tabItem(
tabName = "page2",
fluidRow(
column(12,
DT::dataTableOutput("table3")
))
)
)
)
)
server <- function(input, output) {
# table1
tbl1 <- mtcars[1:30, ] # tables 1, 2, 3 use different rows of mtcars to differentiate tables
output$table1 <- DT::renderDataTable({
datatable( tbl1,
# options = DToptions, # no such object called "DToptions"
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
# table2
tbl2 <- mtcars[5:45, ]
output$table2 <- DT::renderDataTable({
datatable( tbl2,
# options = DToptions,
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
# table3
tbl3 <- mtcars[11:35, ]
output$table3 <- DT::renderDataTable({
datatable( tbl3,
# options = DToptions,
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
page_name <- reactive({
input$pages
})
# select table on the active page / tab
selected_table <- reactive({
if(page_name() == "page1"){
tbl.list <- list("tab1" = tbl1, "tab2" = tbl2)
select_tbl <- tbl.list[input$tabs]
}else{
select_tbl <- tbl3
}
return(select_tbl)
})
# download table
output$downloadData <- downloadHandler(
filename = function() {"table.csv"},
content = function(file) {write.csv(selected_table(), file, row.names=TRUE)}
)
}
shinyApp(ui, server)
library(shiny)
library(shinydashboard)
library(DT)
library(writexl)
ui <- dashboardPage(
dashboardHeader(title = "Header",
dropdownMenuOutput("updatedTimeOutput"),
dropdownMenu(type = "notifications",
badgeStatus = "warning",
icon = icon("bullhorn", "fa-lg"),
notificationItem(icon = icon("bullhorn", "fa-1x"),
status = "info",
text = tags$span(
tags$b("Please notice!")
)
))),
dashboardSidebar(sidebarMenu(id = "pages", # use unique id for pages
menuItem("Page1", tabName = "page1"),
menuItem("Page2", tabName = "page2"))),
dashboardBody(
# Add download button and radioButton
fluidRow(
column(3,
downloadButton('downloadData', 'Download Table',
style="font-weight:bold;"
),
helpText(
hr(style = "border-top: 1px solid #000000;"),
)),
column(3,
radioButtons("f", "Download format:",
c("csv" = "csv",
"Excel" = "xlsx"), inline=T)
)),
tabItems(
tabItem(
tabName = "page1",
tabsetPanel(id="tabs",
tabPanel("tab1",
column(12,
DT::dataTableOutput("table1")
)),
tabPanel( "tab2",
column(12,
DT::dataTableOutput("table2")
))
)
)),
tabItem(
tabName = "page2",
fluidRow(
column(12,
DT::dataTableOutput("table3")
))
)
)
)
server <- function(input, output) {
# table1
tbl1 <- mtcars[1:30, ] # tables 1, 2, 3 use different rows of mtcars to differentiate tables
output$table1 <- DT::renderDataTable({
datatable( tbl1,
# options = DToptions, # no such object called "DToptions"
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
# table2
tbl2 <- mtcars[5:45, ]
output$table2 <- DT::renderDataTable({
datatable( tbl2,
# options = DToptions,
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
# table3
tbl3 <- mtcars[11:35, ]
output$table3 <- DT::renderDataTable({
datatable( tbl3,
# options = DToptions,
extensions = 'Buttons',
rownames = TRUE,
selection = 'none'
)
})
page_name <- reactive({
input$pages
})
# select table on the active page / tab
selected_table <- reactive({
if(page_name() == "page1"){
tbl.list <- list("tab1" = tbl1, "tab2" = tbl2)
select_tbl <- tbl.list[input$tabs]
}else{
select_tbl <- tbl3
}
return(select_tbl)
})
# select download format
select_format <- reactive(input$f)
# download table
output$downloadData <- downloadHandler(
filename = function(){
if(select_format() == "csv"){
{"table.csv"}
}else{
{"table.xlsx"}
}
} ,
content = function(file){
if(select_format() == "csv"){
{write.csv(selected_table(), file, row.names=TRUE)}
}else{
{write_xlsx(selected_table(), file)}
}
}
)
}
shinyApp(ui, server)
Given following shiny app:
library(shiny)
library(tidyverse)
library(DT)
ui <- fluidPage(
br(),
DTOutput("DT")
)
server <- function(input, output) {
output$DT <- renderDataTable({
mtcars %>%
datatable(.,extensions = 'Buttons',
options = list(dom = 'Bfrtip',
exportOptions = list(header = ""),
buttons = c('copy', 'csv', 'excel', 'pdf')))
})
}
shinyApp(ui = ui, server = server)
one can copy the full table in the clipboard using the buttons on the top left of the datatable.
But when pasting the content into excel, notepad, or whatever there is a header, which I want to remove.
I'm pretty sure that the header can be deleted or changed using sth. similar to exportOptions = list(header = ""),. But not suprisingly this is not working. Perhaps one can find or translate a solution from here to R/Shiny.
That's the title option of the button:
library(shiny)
library(DT)
ui <- fluidPage(
br(),
DTOutput("DT")
)
server <- function(input, output) {
output$DT <- renderDT({
mtcars %>%
datatable(., extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons = list(
list(
extend = "copy",
text = "COPY",
title = NULL
)
)
)
)
})
}
shinyApp(ui = ui, server = server)
I'm building a shinyApp on mtcars data. I got 2 actionButtons (Go & Clear).
The Go button is for displaying the output on mainPanel whereas the Clear button is for clearing that output.
My Clear button isn't working due to some unforeseen reason. Can somebody please have a look at my codes. I shall be extremely grateful.
library(shiny)
library(DT)
library(dplyr)
library(shinythemes)
library(htmlwidgets)
library(shinyWidgets)
library(shinydashboard)
data_table<-mtcars
#ui
ui = fluidPage(
sidebarLayout(
sidebarPanel (
uiOutput("cyl_selector"),
uiOutput("disp_selector"),
actionButton(inputId = "go", label = "Go"),
actionButton(inputId = "reset", label = "Clear")),
mainPanel(
DT::dataTableOutput('mytable') )))
#server
server = function(input, output, session) {
output$cyl_selector <- renderUI({
selectInput(inputId = "cyl",
label = "cyl:", multiple = TRUE,
choices = c( unique(as.character(data_table$cyl))),
selected = c('4')) })
output$disp_selector <- renderUI({
available <- data_table[c(data_table$cyl %in% input$cyl ), "disp"]
selectInput(
inputId = "disp",
label = "disp:",
multiple = TRUE,
choices = c('All',as.character(unique(available))),
selected = 'All') })
thedata <- eventReactive(input$go,{
data_table<-data_table[data_table$cyl %in% input$cyl,]
if(input$disp != 'All'){
data_table<-data_table[data_table$disp %in% input$disp,]
}
data_table
})
# thedata <- eventReactive(input$reset,{
# data_table<-NULL
# })
output$mytable = DT::renderDataTable({
DT::datatable( filter = "top", rownames = FALSE, escape = FALSE,
options = list(pageLength = 50, autowidth=FALSE,
dom = 'Brtip' ),
{
thedata() # Call reactive thedata()
})
})}
shinyApp(ui = ui, server = server)
I didn't analyze your script completly, but i can see that it doesn't call the second button at all (Clear). You made an eventReactive() using input$go for the first button to make the plot, but you need to call input$reset too if you want to make it work.