我有一个闪亮的代码,类似于下面的人为例子。我的意图是,在server
部分,我动态地将inputSelect值作为参数传递给table_Server函数,如下所示(不工作):
# Line 94 of code
server = function(input,output,session){
table_Server("ER", input$region_choice)
}
相反,我必须硬编码该区域,如下所示:
# Line 94 of code
server = function(input,output,session){
table_Server("ER", "Morogoro)
}
完整的运行代码(硬编码)如下,任何建议赞赏.
library(shiny)
library(shinydashboard)
#>
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#>
#> box
get_dataset = function(region){
if(region=="Morogoro"){
mtcars
}else{
iris
}
}
get_reg_rate = function(region){
data.frame(
region="Morogoro",
numerator=459,
denominator=541,
green_gap=80,
yellow_gap=77,
message="Regional Performance"
)
}
table_UI <- function(id) {
ns <- NS(id)
tagList(
sidebarLayout(
sidebarPanel(width = 2,
uiOutput(ns("selector")),
),
mainPanel(
valueBoxOutput(ns('regional_value')),
valueBoxOutput(ns('green_gap_value')),
valueBoxOutput(ns('yellow_gap_value')),
DT::dataTableOutput(ns('table'))
)
)
)
}
table_Server <- function(id, region) {
moduleServer(id,function(input, output, session) {
ds=get_dataset(region)
rate=get_reg_rate(region)
output$table = DT::renderDataTable({
ds
})
output$regional_value <- renderValueBox({
valueBox(
rate$rate,
rate$message
)
})
if(!id %in% c("DE","Score_district","DE_district")){
output$green_gap_value <- renderValueBox({
valueBox(
rate$green_gap,
"Green Gap"
)
})
output$yellow_gap_value <- renderValueBox({
valueBox(
rate$yellow_gap,
"Yellow Gap"
)
})
}
output$selector=renderUI({
selectInput(inputId=NS(id,"region_choice"),
label="Region",
choices = c("Morogoro","Lindi","Mtwara","Njombe","Ruvuma",
"Iringa"),selected = "Morogoro" )
})
}
)
}
ui = fluidPage(
tabsetPanel(id = 'cqi_indicators',
tabPanel('Region',
tabsetPanel(
id='region_indicators',
tabPanel("Early Retention",table_UI("ER"))
)
)
)
)
server = function(input,output,session){
table_Server("ER", "Morogoro")
}
shinyApp(ui,server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
静态R Markdown文档中不支持Shiny应用程序
由reprex package(v2.0.1)于2023-06-17创建
1条答案
按热度按时间z0qdvdin1#
调用
table_Server("ER", input$region_choice)
后,input$region_choice
的值还不存在->从模块服务器函数内部访问
input$region_choice
。使用
observeEvent
使renderDataTable
和renderValueBox
在input$region_choice
更改时响应input$region_choice
: