shinycssloaders

Published by onesixx on

https://github.com/daattali/shinycssloaders

https://daattali.com/shiny/shinycssloaders-demo/

설치 및 기본사용법

#devtools::install_github('andrewsali/shinycssloaders')
#options(spinner.color="#0dc5c1")
#options(spinner.size=my_size)
library(shinycssloaders)

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

demo in shiny

library(tidyverse); library(data.table);library(shiny)
library(shinycssloaders)

ui <- fluidPage(
  actionButton("go", "Go"),
  plotOutput("myPlot") %>% withSpinner()
)

server <- function(input, output) {
  output$myPlot <- renderPlot({
    input$go
    
    Sys.sleep(1.6)
    data.table(x=1:10, y=runif(10)) %>% ggplot(aes(x,y)) + geom_point() + geom_line()
  })
}

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

shinyApp(ui, server)
library(shiny)
library(shinycssloaders)
library(ggplot2)



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)




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: R ggplot2

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x