shinycssloaders

Published onesixx on

https://github.com/andrewsali/shinycssloaders

#devtools::install_github('andrewsali/shinycssloaders')

#options(spinner.color="#0dc5c1")
#options(spinner.size=my_size)

library(shinycssloaders)

...

withSpinner(plotOutput("my_plot")) 
plotOutput("my_plot") %>% withSpinner()
plotOutput("my_plot") %>% withSpinner(color="#0dc5c1")
...

ex>

library(shiny)
library(shinycssloaders)
library(ggplot2)

options(spinner.color.background="#F5F5F5")

ui <- fluidPage(
  checkboxInput("show_plot", "Show tables", value=TRUE),
  actionButton("redraw_plot","Re-draw"),
  fluidRow(
    column(width=6,
           plotOutput("plotWithSpin") %>% withSpinner(type=7)
    )
  )
)
server <- function(input, output,session) {
  output$plotWithSpin <- renderPlot({
    shiny::validate(need(input$show_plot,"Show table is unchecked. Check to see table."))
    input$redraw_plot
    Sys.sleep(3) # just for animation
    ggplot(mtcars, aes(x=hp, y=mpg))+ facet_grid(~gear)+ geom_point()
  })
}

shinyApp(ui, server)

 

 

 

library(shiny); library(ggplot2); library(tidyverse)
library(shinycssloaders)

ui <- fluidPage(
    actionButton("btn_plotting", "Plot"),
    actionButton("btn_reset", "reset"),
    plotOutput("mainPlot") %>% withSpinner(type=2, color="#428bca")
)
server <- function(input, output, session){
    observeEvent(input$btn_plotting,{ 
        Sys.sleep(3)
        output$mainPlot <- renderPlot({  
            p <- ggplot(data.frame(val=rnorm(50)), aes(x=val)) + geom_histogram()
            return(p)
        })
    })
    observeEvent(input$btn_reset,{ 
        Sys.sleep(3)
        output$mainPlot <- renderPlot(p())
    })
}
shinyApp(ui, server)

 

 

 

library(shiny); library(ggplot2)
data(iris)

ui <- basicPage(
    plotOutput("plot1", brush="plot_brush"),
    verbatimTextOutput("info"),
    mainPanel(downloadButton('downloadData', 'Download'))
)

server <- function(input, output){
    output$plot1 <- renderPlot({
        ggplot(iris,aes(x=Sepal.Width,y=Sepal.Length)) +
            geom_point(aes(color=factor(Species))) + theme_bw()
    })
    # output$info <- renderPrint({
    #     brushedPoints(iris, input$plot_brush, xvar="Sepal.Width", yvar="Sepal.Length")
    # })
    selectedData <- reactive({
        brushedPoints(iris, input$plot_brush)
    })
    output$info <- renderPrint({
        selectedData()
    })
    
    output$downloadData <- downloadHandler(
        filename = function(){ paste('SelectedRows', '.csv', sep='') },
        #content  = function(file){ write.csv(output$info, file)}
        content  = function(file){ write.csv(selectedData(), file)}
        selectedData
    )
}


shinyApp(ui, server)

 

library(shiny)
library(shinycssloaders)

# for spinners 2-3 match the background color of wellPanel
options(spinner.color.background="#F5F5F5")

ui <- fluidPage(
    wellPanel(
        tags$b("This example shows the loading spinner whilst the plot is loading and hides the spinner when the plot is not shown."),br(),br(),
        tags$ul(
            tags$li("You can use it to wrap any kind of output."),
            tags$li("To see what happens on recalculation, click the recalculate button"),
            tags$li("To see what happens if no output should be generated, check off 'Show plots'.")
        ),
        checkboxInput("show_plot","Show plot",value=TRUE),
        actionButton("redraw_plot","Re-draw plots")
    ),
    
    do.call(tabsetPanel,lapply(1:8,function(.type) {
        tabPanel(paste0("Type ",.type),
            fluidRow(
                column(width=6,
                    wellPanel(
                        tags$b("With spinner:"),
                        plotOutput(paste0("plot",.type)) %>% withSpinner(type=.type)
                    )
                ),
                column(width=6,
                    wellPanel(
                        tags$b("Without spinner (default):"),
                        plotOutput(paste0("nospin_plot",.type))
                    )
                )
            )
        )
    }))
)

server <- function(input, output,session){
    for (i in 1:8) {
        output[[paste0("nospin_plot",i)]] <- output[[paste0("plot",i)]] <- renderPlot({
            validate(need(input$show_plot, "Show plot is unchecked. Check to see plot."))
            input$redraw_plot
            Sys.sleep(5) # just for demo so you can enjoy the animation
            df <- data.frame(x = runif(1e4), y = runif(1e4)
            ggplot(df, aes(x,y))+ geom_point
        })
    }
}

shinyApp(ui, server)

 

 

Categories: ggplot2

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.