【发布时间】:2021-11-28 07:13:49
【问题描述】:
下午好!
我正在尝试创建一个模块,用于闪亮的应用程序,它将输出用户可以与之交互的图形和数据表,其想法是用户可以创建各种场景,然后进行评估/分析。我无法理解的是如何在服务器代码中使用模块的输出。我显然在做一些愚蠢的事情,但两天后我已经用完了可供阅读的选项和文章。这是我对模块的第一次探索。
我正在尝试复制这篇文章(尽管我更愿意同时显示两种输入法,而不是使用按钮在两者之间切换)
https://www.r-bloggers.com/2021/09/how-to-use-shinymatrix-and-plotly-graphs-as-inputs-in-a-shiny-app/
使用类似于此的 DT::datatable
https://www.r-bloggers.com/2019/04/edit-datatables-in-r-shiny-app/
这是我要去的地方的一个代表,它不远(在第一个障碍上掉下来!)...... 我试图通过渲染模块生成的数据来重用它并证明我可以单独使用这些数据。
library(shiny)
library(tidyverse)
library(plotly)
library(DT)
library(shinydashboard)
#base data----
default_df <- structure(list(`0` = 70, `1` = 70, `2` = 70, `3` = 70, `4` = 70,
`5` = 70, `6` = 70, `7` = 70, `8` = 70, `9` = 70, `10` = 70,
`11` = 70, `12` = 70, `13` = 70, `14` = 70, `15` = 70, `16` = 70,
`17` = 70, `18` = 70, `19` = 70, `20` = 70, `21` = 70, `22` = 70,
`23` = 70), row.names = "calls", class = "data.frame")
#module----
##mod server----
mod_editable_srv <- function(id, data) {
moduleServer(id, function(input, output, session) {
x = data
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE) # important
print(x)
})
output$x1 = renderDT(x, selection = 'none', editable = TRUE)
return(x)
})
}
##mod ui----
mod_editable_ui <- function(id) {
tagList(
DT::dataTableOutput(NS(id, "x1")),
NS(id,"x")
)
}
#ui----
header <- dashboardHeader(title = "Density based clustering using file upload")
table <- column(width = 4,
box(width = 12, title = "Input hours",
mod_editable_ui('x1')
),
box(width = 12, title = "Input hours",
renderDT('test_dat')
)
)
body <- dashboardBody(table)
ui <- dashboardPage(
header,
dashboardSidebar(disable = TRUE),
body
)
#server----
server <- function(input, output) {
datafile <- mod_editable_srv("x1", default_df)
output$test_dat <- renderDataTable({
datafile()$x
})
}
#run app----
shinyApp(ui = ui, server = server)
我可以通过使用打印调试看到模块正在工作(ish)。
似乎有很多关于 callModule 模式的教程,但没有多少关于 moduleServer 模式的教程。还有什么往往会遗漏服务器端,解释模块如何在彼此之间移动数据。由于 RStudio 不建议使用 moduleServer,我更愿意学习这些模式。
任何帮助将不胜感激!
【问题讨论】:
-
那么,你想要的是
mod_editable_ui和mod_editable_srv来输出一个绘图和一个数据表?您是否已经成功完成了相同但没有模块的操作?如果没有,请先尝试。然后我们可以从那里进入模块。 -
这暴露了我不了解反应值。我会玩一会,看看我能做些什么