You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny应用变现:monetizr包使用故障及替代方案咨询

Shiny应用变现与monetizr包使用问题

一、Shinyapp.io部署的应用能否用monetizr变现?其他变现方法有哪些?

  • monetizr包可以用于Shiny应用变现,但它核心是插入自定义推广内容(如个人博客、付费链接),无法直接对接Google Adsense这类第三方商业广告平台。如果你的需求是展示商业广告,它可能无法满足。
  • 其他Shiny应用变现方法:
    • 付费订阅/会员制:用shinymanager实现用户权限控制,仅付费用户可访问完整功能。
    • 按需付费API调用:将应用核心逻辑封装为API,通过plumber对外提供,按调用次数收费。
    • 企业定制部署:为企业提供私有部署版本,收取一次性部署费或年度服务费。
    • 用户捐赠:在UI中添加PayPal等捐赠按钮,依赖用户自愿支持。
    • 嵌入第三方广告代码:直接在Shiny UI中插入HTML广告脚本(如Google Adsense代码),需确认Shinyapp.io的平台条款是否允许。

二、monetizr包使用出错的原因与修正方案

问题诊断

你在zzz.R中直接调用market_all()导致应用崩溃,核心原因有两个:

  1. market_all()需要在Shiny UI上下文中执行,而zzz.R中的代码会在包加载时立即运行,此时无Shiny环境,触发错误。
  2. 包内资源路径错误:birthstones.csv的读取路径未指向包内部资源目录,vroom::vroom("birthstones.csv")会在当前工作目录查找文件,而非包的内置资源。

修正步骤

  1. 调整monetizr调用位置:
    不在zzz.R中直接执行广告函数,而是在myApp()的UI里指定位置插入广告组件,示例如下:

    # 修改R/app.R中的UI部分
    ui <- navbarPage(
      "Sample app",
      tabPanel("Pick a month",
        selectInput("month", "What's your favourite month?", choices = months),
        # 在指定位置插入广告
        monetizr::market(ad = "Have I mentioned my blog, Variance Explained?")
      ),
      tabPanel("Feedback", monthFeedbackUI("tab1")),
      tabPanel("Birthstone", birthstoneUI("tab2"))
    )
    

    同时通过usethis::use_package("monetizr")将monetizr添加到包的DESCRIPTION依赖中。

  2. 修正包内资源路径:
    将birthstones.csv移动到包的inst/extdata/目录下,然后用system.file()读取正确路径:

    # 修改R/app.R中的数据加载代码
    stones <- vroom::vroom(system.file("extdata", "birthstones.csv", package = "你的包名"))
    

    替换你的包名为DESCRIPTION文件中Package:字段的实际名称。

  3. zzz.R的正确用法(可选):
    若需在包加载时做初始化操作,应使用.onLoad()函数而非直接执行代码:

    # R/zzz.R
    .onLoad <- function(libname, pkgname) {
      # 此处放置包加载时的初始化逻辑,比如monetizr全局设置
      # monetizr::market_setup(...)
    }
    

三、修正后的包代码示例

R/monthFeedback.R(无需修改)

monthFeedbackUI <- function(id) {
  textOutput(NS(id, "feedback"))
}
monthFeedbackServer <- function(id, month) {
  stopifnot(is.reactive(month))

  moduleServer(id, function(input, output, session) {
    output$feedback <- renderText({
      if (month() == "October") {
        "You picked a great month!"
      } else {
        "Eh, you could do better."
      }
    })
  })
}

R/birthstone.R(无需修改)

birthstoneUI <- function(id) {
 p(
   "The birthstone for ", textOutput(NS(id, "month"), inline = TRUE),
   " is ", textOutput(NS(id, "stone"), inline = TRUE)
 )
}
birthstoneServer <- function(id, month) {
 stopifnot(is.reactive(month))

 moduleServer(id, function(input, output, session) {
   stone <- reactive(stones$stone[stones$month == month()])
   output$month <- renderText(month())
   output$stone <- renderText(stone())
 })
}

R/app.R(修正后)

library(shiny)
library(monetizr)

myApp <- function(...) {
  # 加载包内资源
  stones <- vroom::vroom(system.file("extdata", "birthstones.csv", package = "你的包名"))
  months <- c(
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  )
  
  ui <- navbarPage(
    "Sample app",
    tabPanel("Pick a month",
      selectInput("month", "What's your favourite month?", choices = months),
      # 插入monetizr广告
      market(ad = "Have I mentioned my blog, Variance Explained?")
    ),
    tabPanel("Feedback", monthFeedbackUI("tab1")),
    tabPanel("Birthstone", birthstoneUI("tab2"))
  )
  server <- function(input, output, session) {
    monthFeedbackServer("tab1", reactive(input$month))
    birthstoneServer("tab2", reactive(input$month))
  }
  shinyApp(ui, server, ...)
}

R/zzz.R(修正后,可选)

.onLoad <- function(libname, pkgname) {
  # 如有需要,设置monetizr全局参数
  # monetizr::market_defaults(ad_style = "banner")
}

内容的提问来源于stack exchange,提问作者Maryam Momeni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:00:55