moduleServer而不是callModule.moduleServer(与callModule)不同。moduleServer,以便我可以正确引用父会话,以便updateTabsetPanel在动态输出中正常工作renderUI。这篇文章演示了如何使用callModule将父会话传递到模块中,以便updateTabsetPanel可以引用父会话并相应地更新它。我在这里使用这种方法创建了一个最小的示例。有两个tabPanels,我们试图通过actionLink第一个中的 an 导航到第二个。该first_server模块是用 调用的callModule,我们可以将父会话参数作为附加参数传递,从而允许我们在 的updateTabsetPanel调用中引用它first_server。单击actionLinkthen 会将我们带到第二个模块 UI,如预期的那样:
library(shiny)
first_ui <- function(id) {
ns <- NS(id)
uiOutput(ns("goto_second_link_ui"))
}
second_ui <- function(id) {
ns <- NS(id)
p("It worked!")
}
first_server <- function(input, output, session, parent) {
output$goto_second_link_ui <- renderUI({
actionLink(session$ns("goto_second"), label …Run Code Online (Sandbox Code Playgroud) 考虑我们在服务器部分使用的任何 Shiny 模块session$ns,例如下面的模块。
我们可以等效地使用NS(id)来代替session$ns. 那么为什么推荐使用session$ns呢?相比有优势吗NS(id)?
library(shiny)
myModuleUI <- function(id){
ns <- NS(id)
uiOutput(ns("checkbox"))
}
myModuleServer <- function(id){
moduleServer(id, function(input, output, session){
ns <- session$ns
output[["checkbox"]] <- renderUI({
checkboxInput(ns("checkbox"), label = "Check me", value = FALSE)
})
observe({
print(input[["checkbox"]])
})
})
}
ui <- basicPage(
myModuleUI("myModule")
)
server <- function(input, output, session){
myModuleServer("myModule")
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud) 我想在闪亮的模块中有一个可编辑的设备标识符。当我更改 DT 中的值时,表会更新,并且数据表中的消息为空:
“未找到匹配的记录”
我的代码如下:
模块:
modDtUi <- function(id){ # UI module
ns = NS(id)
DT::dataTableOutput(ns('x1'))
}
modDt <- function(input, output, session, data){ # Server module
x <- data
output$x1 <- DT::renderDataTable(x, selection = 'none', editable = TRUE)
proxy <- dataTableProxy('x1', session = session)
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
print(info)
i = info$row
j = info$col
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE, rownames = FALSE)
})
}
Run Code Online (Sandbox Code Playgroud)
Flexdashboard 中的应用程序:
```{r}
modDtUi("editable") …Run Code Online (Sandbox Code Playgroud) 我想创建一个navbarPage,其中每个tabPanel或tagList每个tabPanel都是在另一个模块中创建的。
在“正常”的闪亮应用程序中,我将能够通过使用知道当前选择了哪个选项卡input$navbarPage_ID。(其中navbarPage_ID是分配的 id 变量navbarPage)
对于模块,我无法获得正确的 ID,因为它不会改变。
如何获取服务器模块中所选选项卡的正确ID?
示例 1,tabPanels在模块中创建:
library(shiny)
## Module 1 ####################
mod1_ui <- function(id, label, navid) {
ns <- NS(id)
tabPanel(label, value = navid,
h2("mod1")
)
}
mod1_server <- function(input, output, session, navid) {
observe({
message("mod1_server ", navid)
})
}
## Module 2 ####################
mod2_ui <- function(id, label, navid) {
ns <- NS(id)
tabPanel(label, value = navid,
h2("mod2") …Run Code Online (Sandbox Code Playgroud)该模块的目标是创建一个根据数据选择器模块的输出而变化的反应性条形图。不幸的是,条形图没有更新。它停留在选定的第一个变量上。
我尝试创建观察者函数来更新条形图,但无济于事。我还尝试将选择器服务器模块嵌套在 barplot 模块中,但收到错误:警告:UseMethod 中的错误:没有适用于“mutate”的方法应用于类“c('reactiveExpr', 'reactive'”的对象, '功能')”
我只需要某种方法来告诉 barplot 模块在输入的数据发生变化时进行更新。
条形图模块:
#UI
barplotUI <- function(id) {
tagList(plotlyOutput(NS(id, "barplot"), height = "300px"))
}
#Server
#' @param data Reactive element from another module: reactive(dplyr::filter(austin_map, var == input$var))
barplotServer <- function(id, data) {
moduleServer(id, function(input, output, session) {
#Data Manipulation
bardata <- reactive({
bar <-
data |>
mutate(
`> 50% People of Color` = if_else(`% people of color` >= 0.5, 1, 0),
`> 50% Low Income` = if_else(`% low-income` >= 0.5, 1, 0) …Run Code Online (Sandbox Code Playgroud) 编辑:该bs4Dash软件包的作者 David Granjon 最近对下面引用的 Github 问题中提出的问题提供了答案,并将其关闭。
bs4Dash有关,但那里没有提供答案。我正在制作一个模块化的 Shiny 应用程序,并尝试使用该bs4Dash包来完成它。该应用程序如下所示:
图片1
最终应用程序有几个部分(我只为本示例做了介绍),每个部分至少包含一个bs4TabCard. 上图中的标签卡有一个uiOutput和一个rhandsontableOutput元素,这些元素是在服务器函数中渲染的。请注意,这些都是***Output元素。在图 1的可重现代码中(您可以在问题末尾找到),我不使用任何模块。然而,我的目标是使用多个模块,因为应用程序有可能变得相当大。对于这个简单的示例,我尝试使用两个模块:每个部分(即每个bs4TabItem)一个模块,每个标签卡一个模块。这意味着这两个模块将始终嵌套:tabcard 模块将位于节模块内部。
图2
问题是,当我实现模块时,***Output元素不会显示:
图3
令人惊讶的是***Input元素被显示了。我制作了第三个模块,其中numericInput仅包含一个并将其放置在标签卡的第二个选项卡中。如下图所示,显示numericInput没有问题:
图4
在这个问题中,报告了类似的问题,但没有提供任何解决方案,并且我的挖掘证明不成功。当输出元素被放置在bs4Dash.
图1的可重现代码
library(shiny)
library(bs4Dash)
library(rhandsontable)
shiny::shinyApp(
ui = bs4DashPage(
old_school = FALSE,
sidebar_min = TRUE,
sidebar_collapsed = FALSE,
controlbar_collapsed = FALSE,
controlbar_overlay …Run Code Online (Sandbox Code Playgroud) 我已经从模块定义了我的操作按钮,如下所示。
现在按下时它无法触发观察事件。我认为模块是隔离的且自给自足的,但似乎并非如此。将其放入我的服务器中,效果很好,但我不想弄乱我的服务器。
任何想法?
cool_UI <- function(id) {
ns <- NS(id)
tagList(
uiOutput(ns("myUi"))
)
}
cool <- function(input, output, session) {
observeEvent(input$butonid,{
print("Button from Module")
})
output$myUi <- renderUI({
tabsetPanel(
tabPanel(title = "sometitle",actionButton("butonid","My Button"))
)
})
}
library(shiny)
ui <- fluidPage(
cool_UI("myUi")
)
server <- function(input, output, session) {
callModule(cool,"myUi")
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud) 我正在尝试打包一个闪亮的模块,该模块显示带有徽标的模式(png 格式)。为此,我创建了一个“inst/www”目录来存储徽标文件。目录树看起来像这样:
\n\xe2\x94\x9c\xe2\x94\x80 DESCRIPTION\n\xe2\x94\x9c\xe2\x94\x80 inst\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 www\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 logo.png\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 man\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.Rd\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 NAMESPACE\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 packagetest.Rproj\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 R\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.R\nRun Code Online (Sandbox Code Playgroud)\n但是,在构建和安装之后,该包似乎无法从我放置“logo.png”的预定义目录中读取。相反,它从主项目中读取,我在主项目中插入了包中的函数。包的 testUI() 函数是这样的:
\ntestUI <- function(id) {\n ns <- NS(id)\n\n shiny::showModal(\n modalDialog(\n title = img(src="inst/www/logo.png", style="display:block; margin-left:auto; margin-right:auto;"),\n br(),\n fluidRow(\n column(6, align="center", offset = 3,\n textInput(ns("username"), label = NULL, placeholder = "Username"),\n passwordInput(ns("password"), label = NULL, placeholder = "Password")\n )\n ),\n footer = (\n fluidRow(\n column(6, align="center", offset = 3,\n actionButton(ns("signin"),"Sign in")\n )\n )\n )\n )\n )\n}\nRun Code Online (Sandbox Code Playgroud)\n从我在其他项目中看到的情况来看,“inst”文件夹似乎是可行的方法,但我对 R …
我有一个闪亮的模块,它显示一个带有评论列的表格,用户可以在客户端输入文本,然后评论将存储在数据库中。现在,我想添加另一列带有复选框并将其相应的值(TRUE/FALSE)存储在数据库中。不确定如何从表中检索复选框值。以下是我对示例数据的尝试。
library(tidyverse)
library(shinyWidgets)
library(shiny)
library(htmlwidgets)
mtcars_df <- mtcars %>%
rownames_to_column(var="car")
writeback_UI <- function (id) {
ns <- NS(id)
DT::dataTableOutput(ns('records_tbl'))
}
shinyInput = function(FUN, len, id, ...) {
inputs = character(len)
for (i in seq_len(len)) {
inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
}
inputs
}
# obtain the values of inputs
shinyValue = function(id, len) {
unlist(lapply(seq_len(len), function(i) {
value = input[[paste0(id, i)]]
if (is.null(value)) NA else value
}))
}
writeback_server <- function (id,records_data) {
#stopifnot(is.reactive(records_data))
shiny::moduleServer(id, function (input,output,session) …Run Code Online (Sandbox Code Playgroud)