Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save pdf/take screenshot of a big page. #15

Open
alearrigo opened this issue Oct 21, 2024 · 0 comments
Open

Save pdf/take screenshot of a big page. #15

alearrigo opened this issue Oct 21, 2024 · 0 comments

Comments

@alearrigo
Copy link

Hi,
is there a way to save a pdf of a page with multiple rows that requires scrolling and doesn't fit all at once on screen?
Example app below: When I press the "save PDF" button only a part of the app get screenshotted.

library(shiny)
library(bslib)
library(dplyr)
library(echarts4r)
library(DT)
library(capture)

# Generate sample data
set.seed(123)
data <- expand.grid(
  year = 2018:2022,
  region = c("North", "South", "East", "West"),
  product = c("A", "B", "C")
)
data$sales <- round(runif(nrow(data), 1000, 10000))
data$profit <- round(data$sales * runif(nrow(data), 0.1, 0.3))

ui <- page_sidebar(
  title = "Sales Dashboard",
  sidebar = sidebar(
    selectInput("year", "Select Year", choices = unique(data$year), selected = max(data$year)),
    selectInput("region", "Select Region", choices = c("All", unique(data$region)), selected = "All")
  ),
  page_fluid(
  layout_columns(
    card(
      card_header("Total Sales by Region"),
      echarts4rOutput("sales_by_region")
    ),
    card(
      card_header("Product Sales Distribution"),
      echarts4rOutput("product_sales_pie")
    )
  ),
  layout_columns(
    card(
      card_header("Sales Trend"),
      echarts4rOutput("sales_trend")
    ),
    card(
      card_header("Top Products"),
      DTOutput("top_products_table")
    )
  ),
  card(
    card_header("Sales vs Profit Scatter"),
    echarts4rOutput("sales_profit_scatter")
  ),
layout_columns(
  card(
    capture_pdf(
      selector = "body",
      filename = "all-page.pdf",
      icon("camera"), "Save in pdf"
    )
    ),
  
  )
)
)

server <- function(input, output, session) {
  
  filtered_data <- reactive({
    df <- data %>% filter(year == input$year)
    if (input$region != "All") {
      df <- df %>% filter(region == input$region)
    }
    df
  })
  
  output$sales_by_region <- renderEcharts4r({
    filtered_data() %>%
      group_by(region) %>%
      summarise(total_sales = sum(sales)) %>%
      e_charts(region) %>%
      e_bar(total_sales) %>%
      e_title("Total Sales by Region")
  })
  
  output$product_sales_pie <- renderEcharts4r({
    filtered_data() %>%
      group_by(product) %>%
      summarise(total_sales = sum(sales)) %>%
      e_charts(product) %>%
      e_pie(total_sales) %>%
      e_title("Product Sales Distribution")
  })
  
  output$sales_trend <- renderEcharts4r({
    data %>%
      group_by(year) %>%
      summarise(total_sales = sum(sales)) %>%
      e_charts(year) %>%
      e_line(total_sales) %>%
      e_title("Sales Trend Over Years")
  })
  
  output$top_products_table <- renderDT({
    filtered_data() %>%
      group_by(product) %>%
      summarise(total_sales = sum(sales)) %>%
      arrange(desc(total_sales)) %>%
      datatable(options = list(pageLength = 5))
  })
  
  output$sales_profit_scatter <- renderEcharts4r({
    filtered_data() %>%
      e_charts(sales) %>%
      e_scatter(profit, sales) %>%
      e_title("Sales vs Profit") %>%
      e_x_axis(name = "Sales") %>%
      e_y_axis(name = "Profit")
  })

}

shinyApp(ui, server)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant