diff --git a/R/examples.R b/R/examples.R index f618d347..1310d3f7 100644 --- a/R/examples.R +++ b/R/examples.R @@ -14,6 +14,8 @@ NULL #' @example inst/examples/Button.R #' @example inst/examples/Button2.R +#' @example inst/examples/Button3.R +#' @example inst/examples/Button4.R #' @name Button NULL diff --git a/inst/examples/Button3.R b/inst/examples/Button3.R new file mode 100644 index 00000000..732aa967 --- /dev/null +++ b/inst/examples/Button3.R @@ -0,0 +1,47 @@ + +# Example 3 +library(shiny) +library(shiny.fluent) +library(shinyjs) + +# This example app shows how to use a Fluent UI Button to trigger a file upload. +# File upload is not natively supported by shiny.fluent, so shinyjs is used +# to trigger the file upload input. +ui <- fluentPage( + useShinyjs(), + Stack( + tokens = list( + childrenGap = 10L + ), + horizontal = TRUE, + DefaultButton.shinyInput( + inputId = "uploadFileButton", + text = "Upload File", + iconProps = list(iconName = "Upload") + ), + div( + style = " + visibility: hidden; + height: 0; + width: 0; + ", + fileInput( + inputId = "uploadFile", + label = NULL + ) + ) + ), + textOutput("file_path") +) + +server <- function(input, output) { + observeEvent(input$uploadFileButton, { + click("uploadFile") + }) + + output$file_path <- renderPrint({ + input$uploadFile + }) +} + +shinyApp(ui, server) diff --git a/inst/examples/Button4.R b/inst/examples/Button4.R new file mode 100644 index 00000000..5940edd2 --- /dev/null +++ b/inst/examples/Button4.R @@ -0,0 +1,38 @@ + +# Example 4 +library(shiny) +library(shiny.fluent) +library(shinyjs) + +# This example app shows how to use a Fluent UI Button to trigger a file download. +# File download is not natively supported by shiny.fluent, so shinyjs is used +# to trigger the file upload input. +ui <- fluentPage( + useShinyjs(), + DefaultButton.shinyInput( + "downloadButton", + text = "Download", + iconProps = list(iconName = "Download") + ), + div( + style = "visibility: hidden;", + downloadButton("download", label = "") + ) +) + +server <- function(input, output, session) { + observeEvent(input$downloadButton, { + click("download") + }) + + output$download <- downloadHandler( + filename = function() { + paste("data-", Sys.Date(), ".csv", sep="") + }, + content = function(file) { + write.csv(iris, file) + } + ) +} + +shinyApp(ui, server) diff --git a/man/Button.Rd b/man/Button.Rd index ca2204b7..4686d1ca 100644 --- a/man/Button.Rd +++ b/man/Button.Rd @@ -342,4 +342,89 @@ server <- function(id) { if (interactive()) { shinyApp(ui("app"), function(input, output) server("app")) } + +# Example 3 +library(shiny) +library(shiny.fluent) +library(shinyjs) + +# This example app shows how to use a Fluent UI Button to trigger a file upload. +# File upload is not natively supported by shiny.fluent, so shinyjs is used +# to trigger the file upload input. +ui <- fluentPage( + useShinyjs(), + Stack( + tokens = list( + childrenGap = 10L + ), + horizontal = TRUE, + DefaultButton.shinyInput( + inputId = "uploadFileButton", + text = "Upload File", + iconProps = list(iconName = "Upload") + ), + div( + style = " + visibility: hidden; + height: 0; + width: 0; + ", + fileInput( + inputId = "uploadFile", + label = NULL + ) + ) + ), + textOutput("file_path") +) + +server <- function(input, output) { + observeEvent(input$uploadFileButton, { + click("uploadFile") + }) + + output$file_path <- renderPrint({ + input$uploadFile + }) +} + +shinyApp(ui, server) + +# Example 4 +library(shiny) +library(shiny.fluent) +library(shinyjs) + +# This example app shows how to use a Fluent UI Button to trigger a file download. +# File download is not natively supported by shiny.fluent, so shinyjs is used +# to trigger the file upload input. +ui <- fluentPage( + useShinyjs(), + DefaultButton.shinyInput( + "downloadButton", + text = "Download", + iconProps = list(iconName = "Download") + ), + div( + style = "visibility: hidden;", + downloadButton("download", label = "") + ) +) + +server <- function(input, output, session) { + observeEvent(input$downloadButton, { + click("download") + }) + + output$download <- downloadHandler( + filename = function() { + paste("data-", Sys.Date(), ".csv", sep="") + }, + content = function(file) { + write.csv(iris, file) + } + ) +} + +shinyApp(ui, server) }