首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Golem模块在服务器端不工作

Golem模块在服务器端不工作
EN

Stack Overflow用户
提问于 2022-12-01 14:13:54
回答 1查看 18关注 0票数 0

我一直在一个Shiny应用程序中工作,现在我试图将它放在Golem的模块中。

我的app_ui.R:

代码语言:javascript
复制
#' The application User-Interface
#'
#' @param request Internal parameter for `{shiny}`.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_ui <- function(request) {
   tagList(# Leave this function for adding external resources
      golem_add_external_resources(),
      # Your application UI logic
      fluidPage(
         theme = shinythemes::shinytheme("united"),
         navbarPage(
            # theme = "cerulean",  # <--- To use a theme, uncomment this
            "App Cool Name",
            tabPanel(
               "Inicial",
               h5("Dados Gerais"),
               fluidRow(
                  shinydashboard::valueBoxOutput(outputId = "data_msg",
                                                 width = "3"),
                  shinydashboard::valueBoxOutput(outputId = "data_groups",
                                                 width = "3")
                  
               ))))
)
}

#' Add external Resources to the Application
#'
#' This function is internally used to add external
#' resources inside the Shiny application.
#'
#' @import shiny
#' @importFrom golem add_resource_path activate_js favicon bundle_resources
#' @noRd
golem_add_external_resources <- function() {
  add_resource_path(
    "www",
    app_sys("app/www")
  )

  tags$head(
    favicon(),
    bundle_resources(
      path = app_sys("app/www"),
      app_title = "reprex"
    )
    # Add here other external resources
    # for example, you can add shinyalert::useShinyalert()
  )
}

我的app_server.R:

代码语言:javascript
复制
#' The application server-side
#'
#' @param input,output,session Internal parameters for {shiny}.
#'     DO NOT REMOVE.
#' @import shiny
#' @noRd
app_server <- function(input, output, session) {
  # Your application server logic
   con <- DBI::dbConnect(drv = RSQLite::SQLite(),
                         dbname = "data-raw/data_base.db")

   output$data_msg <- shinydashboard::renderValueBox({
      data_msg <-
         as.character(
            DBI::dbGetQuery(
               con,
               "select count(*) as msg
                                                from cool_name
                                              where network IN ('data')"
            )
         )
      shinydashboard::valueBox(
         value = data_msg,
         subtitle = "Nº Mensagens",
         color = "yellow",
         width = "3 col-lg-2"
      )
   })

  

   output$data_group <- shinydashboard::renderValueBox({
      data_group <-
         as.character(
            DBI::dbGetQuery(
               con,
               "select sum(a.cnt)
                                      from (
                                              select count(distinct(group_id)) as cnt
                                              from cool_name
                             where network IN ('telegram')
                                              group by group_id
                                           ) as a"
            )
         )

      shinydashboard::valueBox(
         value = data_group,
         subtitle = "Nº de Grupos",
         color = "yellow",
         width = "3"
      )
   })

}

这很好用!但是当我把它放在模块中时,它根本不起作用。

我使用golem::add_modules("test")

我的mod_test.R:

代码语言:javascript
复制
#' test UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_test_ui <- function(id){
  ns <- NS(id)
  tagList(
     # Frontpage - boxes - start -----------------------------------------------
     shinydashboard::valueBoxOutput(outputId = "data_msg",
                                    width = "3"),
     shinydashboard::valueBoxOutput(outputId = "data_groups",
                                    width = "3")
     
  )
}

#' test Server Functions
#'
#' @noRd
mod_test_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    con <- DBI::dbConnect(drv = RSQLite::SQLite(),
                          dbname = "data-raw/data_base.db")
    
    output$data_msg <- shinydashboard::renderValueBox({
       data_msg <-
          as.character(
             DBI::dbGetQuery(
                con,
                "select count(*) as msg
                                                from cool_name
                                              where network IN ('data')"
             )
          )
       shinydashboard::valueBox(
          value = data_msg,
          subtitle = "Nº Mensagens",
          color = "yellow",
          width = "3 col-lg-2"
       )
    })
    
    
    
    output$data_group <- shinydashboard::renderValueBox({
       data_group <-
          as.character(
             DBI::dbGetQuery(
                con,
                "select sum(a.cnt)
                                      from (
                                              select count(distinct(group_id)) as cnt
                                              from cool_name
                             where network IN ('telegram')
                                              group by group_id
                                           ) as a"
             )
          )
       
       shinydashboard::valueBox(
          value = data_group,
          subtitle = "Nº de Grupos",
          color = "yellow",
          width = "3"
       )
    })
    
  })
}

## To be copied in the UI
# mod_test_ui("test_1")

## To be copied in the server
# mod_test_server("test_1")

当我用mod_test_ui("test_1")mod_test_server("test_1")替换ui和服务器中的完整代码时。我不工作!

但是,如果我只用mod_test_ui("test_1")替换ui部件,这就很好了。我在服务器上少了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-12-02 12:48:34

您必须在UI模块中使用命名空间(ns)。将每个id包含在ns()

代码语言:javascript
复制
mod_test_ui <- function(id){
  ns <- NS(id)
  tagList(
     # Frontpage - boxes - start -----------------------------------------------
     shinydashboard::valueBoxOutput(outputId = ns("data_msg"),
                                    width = "3"),
     shinydashboard::valueBoxOutput(outputId = ns("data_groups"),
                                    width = "3")
     
  )
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74643260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档