[英]Passing reactive data table server to ui in modular shiny app
I want to present a data table with one column from a data frame by default and then have a user populate other columns from the dataframe using the drag and drop package sortable.我想在默认情况下显示一个数据表,其中包含数据框中的一列,然后让用户使用拖放 package 可排序来填充 dataframe 中的其他列。
I have a working example here when running via one file shiny app.通过一个文件 shiny 应用程序运行时,我在这里有一个工作示例。
library(shiny)
library(sortable)
library(DT)
a <- c("13232","24343","A434535")
b <- c("fsf","dfgds","ggdf")
c <- c("13232","24343","A434535")
d <- c("fsf","dfgds","ggdf")
data <- data.frame(a,b,c,d)
ui <- fluidPage(
tags$head(
tags$style(HTML(".bucket-list-container {min-height: 350px;}"))
),
fluidRow(
tags$b("Data Table"),
width = 12,
bucket_list(
header = "Drag the items in any desired bucket",
group_name = "bucket_list_group",
orientation = "horizontal",
add_rank_list(
text = " Specific Variables",
labels = colnames(data),
input_id = "rank_list_1"
),
add_rank_list(
text = "Contents Of Data Table",
labels = NULL,
input_id = "rank_list_2"
))
),
fluidRow(
column(
width = 12,
tags$b("Result"),
column(
width = 12,
tags$p("Table"),
DTOutput('tbl')
)
)
)
)
server <- function(input,output) {
output$tbl = renderDT(cbind(data[1],data[,c(input$rank_list_2)]), options = list(lengthChange = FALSE)
)
}
shinyApp(ui, server)
While this works fine.虽然这工作正常。 when i try to implement this in a modular format, the data table fails to update.当我尝试以模块化格式实现这一点时,数据表无法更新。
ui用户界面
sort_ui <- function(id) {
ns <- NS(id)
tagList(
tabsetPanel(
tabPanel("Data Table",
fluidRow(
tags$b("Data Table"),
width = 12,
bucket_list(
header = "Drag the items in any desired bucket",
group_name = "bucket_list_group",
orientation = "horizontal",
add_rank_list(
text = "Contents Of Data Table",
labels = NULL,
input_id = "rank_list_1"
))
),
add_rank_list(
text = "Contents Of Data Table",
labels = NULL,
input_id = "rank_list_2"
))
),
fluidRow(
column(
width = 12,
tags$p("Table"),
DT::dataTableOutput(ns('table'))
)
)
)
Server服务器
sort_server <- function(input, output, session,globalSession){
ns <- session$ns
a <- c("13232","24343","A434535")
b <- c("fsf","dfgds","ggdf")
c <- c("13232","24343","A434535")
d <- c("fsf","dfgds","ggdf")
data <- data.frame(a,b,c,d)
x <- data[1]
data <- reactive(cbind(x,ihc[,c(input$rank_list_2)]))
output$table = DT::renderDataTable(data(), options = list(stateSave = TRUE)
)
proxy <- dataTableProxy('table', session = globalSession)
}
And I call the module with我用
callModule(sort_server,"my_sort_module",globalSession = session)
Not sure what im doing wrong here.不知道我在这里做错了什么。
You've had a few issues with your code:您的代码存在一些问题:
ns
also for the ids in the bucket_list
, namely group_name
and input_id
您还需要将ns
用于bucket_list
中的 id,即group_name
和input_id
library(shiny)
library(sortable)
library(DT)
sort_ui <- function(id) {
ns <- NS(id)
tagList(
tabsetPanel(
tabPanel("Data Table",
fluidRow(
tags$b("Data Table"),
width = 12,
bucket_list(
header = "Drag the items in any desired bucket",
group_name = ns("bucket_list_group"),
orientation = "horizontal",
add_rank_list(
text = "Contents Of Data Table",
labels = colnames(data),
input_id = ns("rank_list_1")
),
add_rank_list(
text = "Contents Of Data Table",
labels = NULL,
input_id = ns("rank_list_2")
))
),
fluidRow(
column(
width = 12,
tags$p("Table"),
DT::dataTableOutput(ns('table'))
)
)
)
)
)
}
sort_server <- function(input, output, session){
ns <- session$ns
a <- c("13232","24343","A434535")
b <- c("fsf","dfgds","ggdf")
c <- c("13232","24343","A434535")
d <- c("fsf","dfgds","ggdf")
data <- data.frame(a,b,c,d)
table_data <- reactive({
cbind(data[1], data[,c(input$rank_list_2)])
})
output$table = DT::renderDataTable(table_data(), options = list(stateSave = TRUE)
)
proxy <- dataTableProxy('table')
}
ui <- fluidPage(
tags$head(
tags$style(HTML(".bucket-list-container {min-height: 350px;}"))
),
sort_ui("my_sort_module")
)
server <- function(input, output, session) {
callModule(sort_server, "my_sort_module")
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.