使用shinylive允许从静态网络服务器部署Rshiny应用程序会产生巨大的文件

问题描述 投票:0回答:1

我正在测试这种利用

R shiny
技术在哑网络服务器上部署
WASM
应用程序的新方法。 我制作了这个简单的应用程序,不需要任何数据。它唯一的功能是允许用户上传
.csv
文件。然后它读取文件并显示其内容。

这是应用程序:

# Load required libraries
library(shiny)
library(shinythemes)

# Define UI
ui <- fluidPage(
  theme = shinytheme("flatly"),
  
  # App title
  titlePanel("CSV File Uploader"),
  
  # Sidebar layout with input and output definitions
  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Choose CSV File", accept = ".csv"),
      tags$hr(),
      checkboxInput("header", "Header", TRUE),
      checkboxInput("stringAsFactors", "Convert strings to factors", TRUE)
    ),
    
    # Show CSV data
    mainPanel(
      tableOutput("contents")
    )
  )
)

# Define server logic
server <- function(input, output) {
  
  # Read CSV file
  data <- reactive({
    req(input$file)
    df <- read.csv(input$file$datapath,
                   header = input$header,
                   stringsAsFactors = input$stringAsFactors)
    return(df)
  })
  
  # Show CSV data
  output$contents <- renderTable({
    data()
  })
}

# Run the application
shinyApp(ui = ui, server = server)

我将此文件保存在名称

"app.R"
下,然后打包应用程序并使用
"app"
函数将其保存在名为
shinylive::export()
的文件夹中。

使用

httpuv
包在本地测试应用程序效果很好:

httpuv::runStaticServer("path-to-the-exported-app-folder")

最终,我想将应用程序文件上传到 Github 存储库,并通过

github pages
设施查看应用程序。

不幸的是,文件太大了。查看

"app"
文件夹的属性显示其大小为
98mb
。这怎么可能? 尝试使用 github 页面运行此应用程序会导致
"time out"
错误。

发生了什么事?

shiny webassembly r-shinylive
1个回答
0
投票

我已将这篇文章分为几个部分来解决提出的问题。希望有帮助!

Shinylive 应用程序大小

Shinylive 应用程序需要大约 60 MB 或更多数据,因为它们依赖于初始 webR 基础和所有 {shiny} 包依赖项。当从运行 Shiny Server 的计算服务器迁移到通用 Web 服务器时,需要权衡所需的用户带宽量。因此,与主机支付许可费(Posit)、服务器和维护成本的旧现状相比,用户的互联网和计算机正在为查看应用程序的能力付费。

您可以使用以下 R 代码检查基本 Shiny 应用程序的当前包依赖项列表:

pkg_db <- tools::CRAN_package_db()
shiny_pkg_dependencies <- tools::package_dependencies(
  packages = c("shiny"),
  recursive = TRUE,
  db = pkg_db
)

shiny_pkg_dependencies
#> $shiny
#>  [1] "methods"     "utils"       "grDevices"   "httpuv"      "mime"       
#>  [6] "jsonlite"    "xtable"      "fontawesome" "htmltools"   "R6"         
#> [11] "sourcetools" "later"       "promises"    "tools"       "crayon"     
#> [16] "rlang"       "fastmap"     "withr"       "commonmark"  "glue"       
#> [21] "bslib"       "cachem"      "ellipsis"    "lifecycle"   "base64enc"  
#> [26] "jquerylib"   "memoise"     "sass"        "digest"      "Rcpp"       
#> [31] "cli"         "magrittr"    "stats"       "graphics"    "fs"         
#> [36] "rappdirs"

通过应用程序内依赖项添加更多 R 包将扩展此列表并增加每个应用程序的下载大小。

使用 GitHub Actions 自动部署

避免使用

gh-pages
分支部署技术在GitHub页面上共享应用程序。

相反,选择 GitHub Actions 方法,这是首选方法,因为它不会在存储库中存储将 Shiny 应用程序转换为 Shinylive 应用程序的工件。另外,这种方法允许将 Shinylive 应用程序部署到 GitHub Pages 最大约 1 GB

具体来说,我们提倡以下文件结构:

.
├── .github
│   └── workflows
│       └── build-and-deploy-shinylive-r-app.yml
├── README.md
└── app.R

因此,我们可以将示例应用程序源代码放入

app.R
中,然后在每次更新存储库时使用
.github/workflows/build-and-deploy-shinylive-r-app.yml
中的以下GitHub操作来构建和部署shinylive应用程序:

on:
    push:
      branches: [main, master]
    release:
        types: [published]
    workflow_dispatch: {}
   
name: demo-r-shinylive-app

jobs:
    demo-website:
      runs-on: ubuntu-latest
      # Only restrict concurrency for non-PR jobs
      concurrency:
        group: r-shinylive-website-${{ github.event_name != 'pull_request' || github.run_id }}
      # Describe the permissions for obtain repository contents and 
      # deploying a GitHub pages website for the repository
      permissions:
        contents: read
        pages: write
        id-token: write
      steps:
        # Obtain the contents of the repository
        - name: "Check out repository"
          uses: actions/checkout@v4

        # Install R on the GitHub Actions worker
        - name: "Setup R"
          uses: r-lib/actions/setup-r@v2
  
        # Install and pin the shinylive R package dependency
        - name: "Setup R dependency for Shinylive App export"
          uses: r-lib/actions/setup-r-dependencies@v2
          with:
            packages:
              cran::[email protected]
  
        # Export the current working directory as the shiny app
        # using the pinned version of the Shinylive R package
        - name: Create Shinylive App from working directory files
          shell: Rscript {0}
          run: |
           shinylive::export(".", "_site")

        # Upload a tar file that will work with GitHub Pages
        # Make sure to set a retention day to avoid running into a cap
        # This artifact shouldn't be required after deployment onto pages was a success.
        - name: Upload Pages artifact
          uses: actions/upload-pages-artifact@v2
          with: 
            retention-days: 1
        
        # Use an Action deploy to push the artifact onto GitHub Pages
        # This requires the `Action` tab being structured to allow for deployment
        # instead of using `docs/` or the `gh-pages` branch of the repository
        - name: Deploy to GitHub Pages
          id: deployment
          uses: actions/deploy-pages@v2

工作示例

有关演示应用程序的部署、文档和功能版本的综合示例,请参阅以下内容:

一些描述发生了什么的快速屏幕截图:

© www.soinside.com 2019 - 2024. All rights reserved.