使用`source()`与Shiny模块。

huangapple go评论76阅读模式
英文:

Use source() with Shiny Modules

问题

我有一个Shiny应用程序,我正在尝试"模块化"它。当我将子选项卡tabPanel tab_Summary 分离到另一个R文件中时,出现了一个问题,它无法被识别。

如果我将tab_Summary的创建放在ui.R内部,它可以正常工作,但如果我想将这个子选项卡放在另一个文件中,就像以下脚本中所示,那么我会收到object 'tab_Summary' not found的错误:

位于文件夹'C:/Users/ROG/Downloads/example_shiny/Shiny_Modules'中的0_tab_Summary_ui.R

tab_Summary <- tabPanel('Summary',  
    fluidRow(
        column(width = 3,
            htmlOutput("Summary_Number_ui")
        )
    )
)

ui.R脚本:

setwd(paste0(main_working_dir, "Shiny_Modules"))
source("0_tab_Summary_ui.R")

ui <- navbarPage(
    title = div("SHINY DASHBOARD"),
    tab_Summary
)

server.R脚本:

server <- function(input, output, session) {
    output$Summary_Number_ui <- renderUI({
        HTML(paste0("<div id='mydiv'><font size='5'><font color=\"#0d0a36\"> Total Number of Accounts: <b>", 726431 , "</b></div>"))
    })
}

app.R脚本:

library(shiny)

local_working_dir <- "C:/Users/ROG/Downloads/example_shiny/"
main_working_dir <- local_working_dir
setwd(main_working_dir)

shinyApp(ui, server)

以下是不显示任何错误但未模块化的ui.R脚本:

setwd(paste0(main_working_dir, "Shiny_Modules"))
source("0_tab_Summary_ui.R")

ui <- navbarPage(
    title = div("SHINY DASHBOARD"),

    tab_Summary <- tabPanel('Summary',
        fluidRow(
            column(width = 3,
                htmlOutput("Summary_Number_ui")
            )
        )
    )
)
英文:

I have a Shiny app that I am trying to "modularize". I have an issue that my subtab tabPanel tab_Summary is not recognized when I separate it in another R file.

If I place the creation of the tab_Summary inside the ui.R it works, but if I want to be able to have this subtab in another file like showed in the following scripts, then I get error that object 'tab_Summary' not found :

The 0_tab_Summary_ui.R placed in the folder 'C:/Users/ROG/Downloads/example_shiny/Shiny_Modules':

tab_Summary &lt;- tabPanel(&#39;Summary&#39;,  

         fluidRow(
           column(width = 3,
                  htmlOutput(&quot;Summary_Number_ui&quot;)
                  
                  
           )
)
         
         
)

The ui.R script:

setwd(paste0(main_working_dir, &quot;Shiny_Modules&quot;))
source(&quot;0_tab_Summary_ui.R&quot;)

ui &lt;- navbarPage(
  
  title=div(&quot;SHINY DASHBOARD&quot;),
  
  tab_Summary
  
)

The server.R script:

server &lt;- function(input, output, session) {
    
  output$Summary_Number_ui &lt;- renderUI({
    HTML(paste0(&quot;&lt;div id=&#39;mydiv&#39;&gt;&lt;font size=&#39;5&#39;&gt;&lt;font color=\&quot;#0d0a36\&quot;&gt; Total Number of Accounts: &lt;b&gt;&quot;,  726431 , &quot;&lt;/b&gt;&lt;/div&gt;&quot;))
  })


}

The app.R script:

library(shiny)

local_working_dir &lt;- &quot;C:/Users/ROG/Downloads/example_shiny/&quot;
main_working_dir &lt;- local_working_dir
setwd(main_working_dir)

shinyApp(ui, server)

And below the ui.R script that does not show any error but is not modularized:

setwd(paste0(main_working_dir, &quot;Shiny_Modules&quot;))
source(&quot;0_tab_Summary_ui.R&quot;)

ui &lt;- navbarPage(
  
  title=div(&quot;SHINY DASHBOARD&quot;),
  
  # tab_Summary
  
  tab_Summary &lt;- tabPanel(&#39;Summary&#39;,

                          fluidRow(
                            column(width = 3,
                                   htmlOutput(&quot;Summary_Number_ui&quot;)


                            )
                          )


  )

  
)

答案1

得分: 1

以下是您要翻译的内容:

"Try to learn how to use modules in Shiny, roughly global.R "includes" all your modules and those files have a module specific UI and a Server part that belong together. In the ui.R you define your layout and call the specific module UI part, same for the server.R. This way you keep all code for one module together, which makes it nicely scalable. Also note that whatever settings you may want to use and define, global.R is excecuted once upon the start of your app, while all code within your server.R server function is run upon every browser refresh.

global.R

# Global.R is loaded once at App start (not at browser refresh!)

# load all libraries
library("shiny")

# source all your modules here
source("modules/MyTabModule.R")

ui.R

ui <- navbarPage(
  
  title=div("SHINY DASHBOARD"),
  
  MyTabModuleUI("Summary_Number_ui")
  
)

server.R

server <- function(input, output, session) {
  
  MyTabModuleServer("Summary_Number_ui")
  
  
}

modules/MyTabModule.R

MyTabModuleUI <- function(id) {
  
  ns <- NS(id)

  tabPanel('Summary',
    fluidRow(
      column(
        width = 3,
        htmlOutput(ns("Summary_Number_ui"))
      )
    )
  )
  
}

MyTabModuleServer <- function(id) {
  
  moduleServer(id, function(input, output, session) {
    
    output$Summary_Number_ui <- renderUI({
      HTML(paste0("<div id='mydiv'><font size='5'><font color=\"#0d0a36\"> Total Number of Accounts: <b>",  726431 , "</b></div>"))
    })
    
  })
  
}
英文:

Try to learn how to use modules in Shiny, roughly global.R "includes" all your modules and those files have a module specific UI and a Server part that belong together. In the ui.R you define your layout and call the specific module UI part, same for the server.R. This way you keep all code for one module together, which makes it nicely scalable. Also note that whatever settings you may want to use and define, global.R is excecuted once upon the start of your app, while all code within your server.R server function is run upon every browser refresh.

global.R

# Global.R is loaded once at App start (not at browser refresh!)

# load all libraries
library(&quot;shiny&quot;)

# source all your modules here
source(&quot;modules/MyTabModule.R&quot;)

ui.R

ui &lt;- navbarPage(
  
  title=div(&quot;SHINY DASHBOARD&quot;),
  
  MyTabModuleUI(&quot;Summary_Number_ui&quot;)
  
)

server.R

server &lt;- function(input, output, session) {
  
  MyTabModuleServer(&quot;Summary_Number_ui&quot;)
  
  
}

modules/MyTabModule.R

MyTabModuleUI &lt;- function(id) {
  
  ns &lt;- NS(id)

  tabPanel(&#39;Summary&#39;,
    fluidRow(
      column(
        width = 3,
        htmlOutput(ns(&quot;Summary_Number_ui&quot;))
      )
    )
  )
  
}

MyTabModuleServer &lt;- function(id) {
  
  moduleServer(id, function(input, output, session) {
    
    output$Summary_Number_ui &lt;- renderUI({
      HTML(paste0(&quot;&lt;div id=&#39;mydiv&#39;&gt;&lt;font size=&#39;5&#39;&gt;&lt;font color=\&quot;#0d0a36\&quot;&gt; Total Number of Accounts: &lt;b&gt;&quot;,  726431 , &quot;&lt;/b&gt;&lt;/div&gt;&quot;))
    })
    
  })
  
}

huangapple
  • 本文由 发表于 2023年1月9日 17:34:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定