英文:
How do I reuse a user element in R shiny?
问题
我正在编写一个Shiny应用程序,其中包含一个sidebarPanel,其中包含各种交互式用户元素。这个sidebarPanel嵌入在UI中,如下所示:
服务器/用户界面
library(shiny)
ui <- navbarPage(
title="一些标题",
tabPanel(
title="选项卡1",
sidebarLayout(
# 这是我想要重用的元素,请参见第二个选项卡面板
sidebarPanel(radioButtons(inputId="button1", label="选择", choices=c("1", "2"))),
mainPanel(uiOutput("output1"))
)
),
tabPanel(
title="选项卡2",
sidebarLayout(
sidebarPanel(radioButtons(inputId="button1", label="选择", choices=c("1", "2"))),
mainPanel(uiOutput("output2"))
)
)
)
server <- function(input, output, session) {
output$output1 <- renderText("文本1")
output$output2 <- renderText("文本2")
}
shinyApp(ui=ui, server=server)
我尝试创建一个模块,例如https://stackoverflow.com/questions/60756339/translate-shiny-app-using-shiny-i18n-in-modules中所做的方式。
模块 module.R
library(shiny)
samplePanel <- function(id) {
ns <- NS(id)
sidebarPanel(
radioButtons(inputId="button1", label="选择", choices=c("1", "2")),
textOutput("sample_text")
)
}
sampleModule <- function(input, output, session) {
output$sample_text <- renderText("示例文本")
}
新的服务器/用户界面主脚本
source("module.R")
library(shiny)
ui <- navbarPage(
title="一些标题",
tabPanel(
title="选项卡1",
sidebarLayout(
samplePanel("moduleId"),
mainPanel(uiOutput("output1"))
)
),
tabPanel(
title="选项卡2",
sidebarLayout(
samplePanel("moduleId"),
mainPanel(uiOutput("output2"))
)
)
)
server <- function(input, output, session) {
callModule(sampleModule, "moduleId")
output$output1 <- renderText("文本1")
output$output2 <- renderText("文本2")
}
shinyApp(ui=ui, server=server)
关于将Shiny代码模块化的问题,我不明白的是为什么我的samplePanel
甚至不显示我想要显示的sample_text
。编辑:已修复有关在sidebarPanel中呈现文本的微不足道的错误。
英文:
I am writing a shiny app where I have a sidebarPanel containing various interactive user elements. This sidebarPanel is embedded in the ui in such a way:
Server/UI
library(shiny)
ui <- navbarPage(
title="some title",
tabPanel(
title="Tab 1",
sidebarLayout(
# This is the element I want to reuse, see second tabPanel
sidebarPanel(radioButtons(inputId="button1", label="Choose", choices=c("1", "2"))),
mainPanel(uiOutput("output1"))
)
),
tabPanel(
title="Tab 2",
sidebarLayout(
sidebarPanel(radioButtons(inputId="button1", label="Choose", choices=c("1", "2"))),
mainPanel(uiOutput("output2"))
)
)
)
server <- function(input, output, session) {
output$output1 <- renderText("Text 1")
output$output2 <- renderText("Text 2")
}
shinyApp(ui=ui, server=server)
I have tried creating a module such as they did in https://stackoverflow.com/questions/60756339/translate-shiny-app-using-shiny-i18n-in-modules
Module module.R
library(shiny)
samplePanel <- function(id) {
ns <- NS(id)
sidebarPanel(
radioButtons(inputId="button1", label="Choose", choices=c("1", "2")),
textOutput("sample_text")
)
}
sampleModule <- function(input, output, session) {
output$sample_text <- renderText("Sample text")
}
New Server/UI main script
source("module.R")
library(shiny)
ui <- navbarPage(
title="some title",
tabPanel(
title="Tab 1",
sidebarLayout(
samplePanel("moduleId"),
mainPanel(uiOutput("output1"))
)
),
tabPanel(
title="Tab 2",
sidebarLayout(
samplePanel("moduleId"),
mainPanel(uiOutput("output2"))
)
)
)
server <- function(input, output, session) {
callModule(sampleModule, "moduleId")
output$output1 <- renderText("Text 1")
output$output2 <- renderText("Text 2")
}
shinyApp(ui=ui, server=server)
What is it I don't understand about modularizing shiny code? One thing that surprises me is that my samplePanel
doesn't even show the sample_text
I want it to show.
Edit: Fixed the trivial error regarding text rendering in the sidebarPanel.
答案1
得分: 1
这个布局是否接近您想要的效果?
library(shiny)
samplePanel <- function(id) {
ns <- NS(id)
sidebarPanel(
radioButtons(inputId=ns("button1"), label="选择", choices=c("1", "2")),
textOutput(ns("sample_text"))
)
}
sampleModule <- function(input, output, session) {
output$sample_text <- renderText("示例文本")
}
ui <- navbarPage(
title="一些标题",
tabPanel(
title="选项卡1",
sidebarLayout(
samplePanel("moduleId1"),
mainPanel(uiOutput("output1"))
)
),
tabPanel(
title="选项卡2",
sidebarLayout(
samplePanel("moduleId2"),
mainPanel(uiOutput("output2"))
)
)
)
server <- function(input, output, session) {
callModule(sampleModule, "moduleId1")
callModule(sampleModule, "moduleId2")
output$output1 <- renderText("文本1")
output$output2 <- renderText("文本2")
}
shinyApp(ui=ui, server=server)
编辑
根据评论中的讨论,这个布局将模块从tabPanel
的侧边栏移动到页面侧边栏,这意味着一个单一的实例将对每个tabPanel
都是共用的。因为现在只有一个实例,可能不需要使用模块。
library(shiny)
samplePanel <- function(id) {
ns <- NS(id)
sidebarPanel(
radioButtons(inputId=ns("button1"), label="选择", choices=c("1", "2")),
textOutput(ns("sample_text"))
)
}
sampleModule <- function(input, output, session) {
output$sample_text <- renderText("示例文本")
}
ui <- navbarPage(
title="一些标题",
samplePanel("moduleId1"),
mainPanel(
tabsetPanel(
tabPanel(
title="选项卡1",
uiOutput("output1")
),
tabPanel(
title="选项卡2",
uiOutput("output2")
)
)
)
)
server <- function(input, output, session) {
callModule(sampleModule, "moduleId1")
output$output1 <- renderText("文本1")
output$output2 <- renderText("文本2")
}
shinyApp(ui=ui, server=server)
如果这个回答有用,请接受它,而不仅仅是点赞。点击接近点赞和反对按钮的对勾标记。谢谢。
英文:
Does this give you something close to what you want?
library(shiny)
samplePanel <- function(id) {
ns <- NS(id)
sidebarPanel(
radioButtons(inputId=ns("button1"), label="Choose", choices=c("1", "2")),
textOutput(ns("sample_text"))
)
}
sampleModule <- function(input, output, session) {
output$sample_text <- renderText("Sample text")
}
ui <- navbarPage(
title="some title",
tabPanel(
title="Tab 1",
sidebarLayout(
samplePanel("moduleId1"),
mainPanel(uiOutput("output1"))
)
),
tabPanel(
title="Tab 2",
sidebarLayout(
samplePanel("moduleId2"),
mainPanel(uiOutput("output2"))
)
)
)
server <- function(input, output, session) {
callModule(sampleModule, "moduleId1")
callModule(sampleModule, "moduleId2")
output$output1 <- renderText("Text 1")
output$output2 <- renderText("Text 2")
}
shinyApp(ui=ui, server=server)
EDIT
In response to discussion in comments, this layout moves the module from the tabPanel
sidebar to the page sidebar, meaning that a single instance is common to each of the tabPanel
s. As there's now only a single instance, there may not be any need to use a module.
library(shiny)
samplePanel <- function(id) {
ns <- NS(id)
sidebarPanel(
radioButtons(inputId=ns("button1"), label="Choose", choices=c("1", "2")),
textOutput(ns("sample_text"))
)
}
sampleModule <- function(input, output, session) {
output$sample_text <- renderText("Sample text")
}
ui <- navbarPage(
title="some title",
samplePanel("moduleId1"),
mainPanel(
tabsetPanel(
tabPanel(
title="Tab 1",
uiOutput("output1")
),
tabPanel(
title="Tab 2",
uiOutput("output2")
)
)
)
)
server <- function(input, output, session) {
callModule(sampleModule, "moduleId1")
output$output1 <- renderText("Text 1")
output$output2 <- renderText("Text 2")
}
shinyApp(ui=ui, server=server)
Please accept this answer if it is useful, rather than merely upvoting it. Click the tick mark close to the up- and downvote buttons. Thank you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论