无法将我的功能性 shiy 应用程序转换为具有闪亮模块的应用程序
Cant transform my functional shiy app to one with shiny modules
当前有效
你好,我正在尝试构建一个用于物种采样的闪亮应用程序。对于每个被 selected 的物种,我需要 select 在哪种采样中,如果它在针点采样中被 selected 需要计算,这里是工作示例:
library(shiny)
library(shinyMobile)
ui = f7Page(
title = "Show navbar",
f7SingleLayout(
navbar = f7Navbar("Hide/Show navbar"),
f7Button(inputId = "toggle", "Toggle navbar", color = "red"),
f7Text(inputId = "SpeciesName", label = "SpeciesName"),
shinyMobile::f7Card(
f7Flex(
textOutput("SpeciesAgain"),
uiOutput("Sampling_type_ui"),
uiOutput("SpeciesCount")
)
)
)
)
server = function(input, output, session) {
output$SpeciesAgain <- renderText({input$SpeciesName})
output$Sampling_type_ui <- renderUI({
req(input$SpeciesName)
f7Select(inputId = "Sampling_type",
label = "Sampling type",
choices = c("Pin-point", "5m circle", "15m circle"))
})
output$SpeciesCount <- renderUI({
if (req(input$Sampling_type) == "Pin-point") {
shinyMobile::f7Stepper(inputId = "Species1", label = "Species count", min = 1, max = 1000, step = 1, value = 1)
}
})
observeEvent(input$toggle, {
updateF7Navbar()
})
}
shinyApp(ui, server)
这就像我想要的那样工作,因为它会等到我写一个物种名称,f7select
出现,如果我 select Pin-point我可以用计数器来计算人数。
这行不通
但是我需要 select 真实应用程序中的几个物种,这就是为什么我想把它变成一个闪亮的模块。这是我试过的:
library(shiny)
library(shinyMobile)
#Species <- "Nothofagus"
Species_UI <- function(id){
f7Text(inputId = NS(id,"SpeciesName"), label = "SpeciesName")
shinyMobile::f7Card(
f7Flex(
textOutput(NS(id, "SpeciesAgain")),
uiOutput(NS(id, "Sampling_type_ui")),
uiOutput(NS(id,"SpeciesCount"))
)
)
}
Species_Server <- function(id){
moduleServer(id, function(input, output, session) {
output$SpeciesAgain <- renderText({input$SpeciesName})
output$Sampling_type_ui <- renderUI({
req(input$SpeciesName)
f7Select(inputId = "Sampling_type",
label = "Sampling type",
choices = c("Pin-point", "5m circle", "15m circle"))
})
output$SpeciesCount <- renderUI({
if (req(input$Sampling_type) == "Pin-point") {
shinyMobile::f7Stepper(inputId = "Species1", label = "Species count", min = 1, max = 1000, step = 1, value = 1)
}
})
})
}
library(shiny)
library(shinyMobile)
ui = f7Page(
title = "Show navbar",
f7SingleLayout(
navbar = f7Navbar("Hide/Show navbar"),
f7Button(inputId = "toggle", "Toggle navbar", color = "red"),
Species_UI("Species")
)
)
server = function(input, output, session) {
Species_Server("Species")
observeEvent(input$toggle, {
updateF7Navbar()
})
}
shinyApp(ui, server)
现在当我这样做时,模块中的 UI 没有出现在应用程序中,但我无法弄清楚是什么问题。
附加问题
在应用程序的最后一个选项中,我想替换我用来为 f7SmartSelect
输入物种的 f7Text
,其中物种名称的输入如下:
library(shiny)
library(shinyMobile)
ui = f7Page(
title = "Show navbar",
f7SingleLayout(
navbar = f7Navbar("Hide/Show navbar"),
f7Button(inputId = "toggle", "Toggle navbar", color = "red"),
f7SmartSelect(inputId = "SpeciesName", label = "SpeciesName",
choices = c("Species1", "Species2", "Species3", "Species4", "Species5"),
multiple = T, openIn = "popup"),
shinyMobile::f7Card(
f7Flex(
textOutput("SpeciesAgain"),
uiOutput("Sampling_type_ui"),
uiOutput("SpeciesCount")
)
)
)
)
server = function(input, output, session) {
output$SpeciesAgain <- renderText({input$SpeciesName})
output$Sampling_type_ui <- renderUI({
req(input$SpeciesName)
f7Select(inputId = "Sampling_type",
label = "Sampling type",
choices = c("Pin-point", "5m circle", "15m circle"))
})
output$SpeciesCount <- renderUI({
if (req(input$Sampling_type) == "Pin-point") {
shinyMobile::f7Stepper(inputId = "Species1", label = "Species count", min = 1, max = 1000, step = 1, value = 1)
}
})
observeEvent(input$toggle, {
updateF7Navbar()
})
}
shinyApp(ui, server)
以便为每个物种重复该模块
欢迎任何帮助
您的代码中存在两个问题:
- UI 模块应该 return 一个
tagList()
所以你需要使用
tagList(
f7Text(inputId = NS(id,"SpeciesName"), label = "SpeciesName"),
shinyMobile::f7Card(
f7Flex(
textOutput(NS(id, "SpeciesAgain")),
uiOutput(NS(id, "Sampling_type_ui")),
uiOutput(NS(id,"SpeciesCount"))
)
)
)
- 当您在服务器中创建输入时(如您的
f7Select()
),您仍然需要注意名称空间。在模块的 server
部分,您需要使用 session$ns(id-of-input)
。这在 R Shiny modules 文章的“在模块中使用 renderUI”一节中指定。在您的情况下,您应该使用:
f7Select(inputId = session$ns("Sampling_type"),
label = "Sampling type",
choices = c("Pin-point", "5m circle", "15m circle"))
小补充:通常在模块的UI部分,定义ns <- NS(id)
,然后使用ns(id-of-input)
比较好。同样,在模块的服务器部分,您可以定义 ns <- session$ns
.
当前有效
你好,我正在尝试构建一个用于物种采样的闪亮应用程序。对于每个被 selected 的物种,我需要 select 在哪种采样中,如果它在针点采样中被 selected 需要计算,这里是工作示例:
library(shiny)
library(shinyMobile)
ui = f7Page(
title = "Show navbar",
f7SingleLayout(
navbar = f7Navbar("Hide/Show navbar"),
f7Button(inputId = "toggle", "Toggle navbar", color = "red"),
f7Text(inputId = "SpeciesName", label = "SpeciesName"),
shinyMobile::f7Card(
f7Flex(
textOutput("SpeciesAgain"),
uiOutput("Sampling_type_ui"),
uiOutput("SpeciesCount")
)
)
)
)
server = function(input, output, session) {
output$SpeciesAgain <- renderText({input$SpeciesName})
output$Sampling_type_ui <- renderUI({
req(input$SpeciesName)
f7Select(inputId = "Sampling_type",
label = "Sampling type",
choices = c("Pin-point", "5m circle", "15m circle"))
})
output$SpeciesCount <- renderUI({
if (req(input$Sampling_type) == "Pin-point") {
shinyMobile::f7Stepper(inputId = "Species1", label = "Species count", min = 1, max = 1000, step = 1, value = 1)
}
})
observeEvent(input$toggle, {
updateF7Navbar()
})
}
shinyApp(ui, server)
这就像我想要的那样工作,因为它会等到我写一个物种名称,f7select
出现,如果我 select Pin-point我可以用计数器来计算人数。
这行不通
但是我需要 select 真实应用程序中的几个物种,这就是为什么我想把它变成一个闪亮的模块。这是我试过的:
library(shiny)
library(shinyMobile)
#Species <- "Nothofagus"
Species_UI <- function(id){
f7Text(inputId = NS(id,"SpeciesName"), label = "SpeciesName")
shinyMobile::f7Card(
f7Flex(
textOutput(NS(id, "SpeciesAgain")),
uiOutput(NS(id, "Sampling_type_ui")),
uiOutput(NS(id,"SpeciesCount"))
)
)
}
Species_Server <- function(id){
moduleServer(id, function(input, output, session) {
output$SpeciesAgain <- renderText({input$SpeciesName})
output$Sampling_type_ui <- renderUI({
req(input$SpeciesName)
f7Select(inputId = "Sampling_type",
label = "Sampling type",
choices = c("Pin-point", "5m circle", "15m circle"))
})
output$SpeciesCount <- renderUI({
if (req(input$Sampling_type) == "Pin-point") {
shinyMobile::f7Stepper(inputId = "Species1", label = "Species count", min = 1, max = 1000, step = 1, value = 1)
}
})
})
}
library(shiny)
library(shinyMobile)
ui = f7Page(
title = "Show navbar",
f7SingleLayout(
navbar = f7Navbar("Hide/Show navbar"),
f7Button(inputId = "toggle", "Toggle navbar", color = "red"),
Species_UI("Species")
)
)
server = function(input, output, session) {
Species_Server("Species")
observeEvent(input$toggle, {
updateF7Navbar()
})
}
shinyApp(ui, server)
现在当我这样做时,模块中的 UI 没有出现在应用程序中,但我无法弄清楚是什么问题。
附加问题
在应用程序的最后一个选项中,我想替换我用来为 f7SmartSelect
输入物种的 f7Text
,其中物种名称的输入如下:
library(shiny)
library(shinyMobile)
ui = f7Page(
title = "Show navbar",
f7SingleLayout(
navbar = f7Navbar("Hide/Show navbar"),
f7Button(inputId = "toggle", "Toggle navbar", color = "red"),
f7SmartSelect(inputId = "SpeciesName", label = "SpeciesName",
choices = c("Species1", "Species2", "Species3", "Species4", "Species5"),
multiple = T, openIn = "popup"),
shinyMobile::f7Card(
f7Flex(
textOutput("SpeciesAgain"),
uiOutput("Sampling_type_ui"),
uiOutput("SpeciesCount")
)
)
)
)
server = function(input, output, session) {
output$SpeciesAgain <- renderText({input$SpeciesName})
output$Sampling_type_ui <- renderUI({
req(input$SpeciesName)
f7Select(inputId = "Sampling_type",
label = "Sampling type",
choices = c("Pin-point", "5m circle", "15m circle"))
})
output$SpeciesCount <- renderUI({
if (req(input$Sampling_type) == "Pin-point") {
shinyMobile::f7Stepper(inputId = "Species1", label = "Species count", min = 1, max = 1000, step = 1, value = 1)
}
})
observeEvent(input$toggle, {
updateF7Navbar()
})
}
shinyApp(ui, server)
以便为每个物种重复该模块
欢迎任何帮助
您的代码中存在两个问题:
- UI 模块应该 return 一个
tagList()
所以你需要使用
tagList(
f7Text(inputId = NS(id,"SpeciesName"), label = "SpeciesName"),
shinyMobile::f7Card(
f7Flex(
textOutput(NS(id, "SpeciesAgain")),
uiOutput(NS(id, "Sampling_type_ui")),
uiOutput(NS(id,"SpeciesCount"))
)
)
)
- 当您在服务器中创建输入时(如您的
f7Select()
),您仍然需要注意名称空间。在模块的server
部分,您需要使用session$ns(id-of-input)
。这在 R Shiny modules 文章的“在模块中使用 renderUI”一节中指定。在您的情况下,您应该使用:
f7Select(inputId = session$ns("Sampling_type"),
label = "Sampling type",
choices = c("Pin-point", "5m circle", "15m circle"))
小补充:通常在模块的UI部分,定义ns <- NS(id)
,然后使用ns(id-of-input)
比较好。同样,在模块的服务器部分,您可以定义 ns <- session$ns
.