您好,我正在尝试为物种采样构建一个闪亮的应用程序。对于选择的每个物种,我需要选择哪种采样,如果它是在精确采样中选择的,则需要对其进行计数,这是一个工作示例:
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
才会出现,如果我选择Pin-point,我可以使用计数器来获取个体数量。
但是我需要在真实的应用程序中选择几个物种,这就是为什么我想把它变成一个闪亮的模块。这是我尝试过的:
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 不会出现在应用程序中,但我不知道出了什么问题。
在应用程序的最后一个选项中,我想替换f7Text
我用来输入物种 a 的f7SmartSelect
,其中物种名称的输入如下:
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)
以便为每个物种重复该模块
欢迎任何帮助
您的代码中有两个问题:
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模块文章的“在模块中使用 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
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句