我希望移植一些较旧的Shiny应用程序来使用Shiny Modules,但是在尝试移植我的反应式表达式时遇到了麻烦.
根据文件:
目标不是阻止模块与其包含的应用程序交互,而是使这些交互显式化.如果模块需要使用反应式表达式,请将反应式表达式作为函数参数.
我有现有的反应式表达式从API等导入我希望传递的数据,但似乎无法找到语法.如果我修改下面给出的Shiny模块示例,我可能会遇到同样的问题.
任何人都可以修改下面的内容,以便您可以将car_data()反应数据传入模块吗?我试过的几乎所有组合isolate和car_data/car_data()我能想到的和我难倒了:)
我宁愿不需要在模块本身内调用数据,因为在我的情况下,我正在尝试推广适用于大量数据集的ETL函数.
library(shiny)
library(ggplot2)
linkedScatterUI <- function(id) {
ns <- NS(id)
fluidRow(
column(6, plotOutput(ns("plot1"), brush = ns("brush"))),
column(6, plotOutput(ns("plot2"), brush = ns("brush")))
)
}
linkedScatter <- function(input, output, session, data, left, right) {
# Yields the data frame with an additional column "selected_"
# that indicates whether that observation is brushed
dataWithSelection <- reactive({
brushedPoints(data(), input$brush, allRows = TRUE)
})
output$plot1 <- renderPlot({
scatterPlot(dataWithSelection(), left())
})
output$plot2 <- renderPlot({
scatterPlot(dataWithSelection(), right())
})
return(dataWithSelection)
}
scatterPlot <- function(data, cols) {
ggplot(data, aes_string(x = cols[1], y = cols[2])) +
geom_point(aes(color = selected_)) +
scale_color_manual(values = c("black", "#66D65C"), guide = FALSE)
}
ui <- fixedPage(
h2("Module example"),
linkedScatterUI("scatters"),
textOutput("summary")
)
server <- function(input, output, session) {
### My modification
### making the reactive outside of module call
car_data <- reactive({
mpg
})
## This doesn't work
## What is the syntax for being able to call car_data()?
df <- callModule(linkedScatter, "scatters", car_data(),
left = reactive(c("cty", "hwy")),
right = reactive(c("drv", "hwy"))
)
output$summary <- renderText({
sprintf("%d observation(s) selected", nrow(dplyr::filter(df(), selected_)))
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
如果您想传递不属于模块的输入,只需reactive()按照教程中的说明将其包裹起来。
如果模块需要访问不属于该模块的输入,则包含的应用程序应传递包含在反应式表达式中的输入值(即反应式(...)):
callModule(myModule, "myModule1", reactive(input$checkbox1))
更新:
正如在另一个答案中正确陈述的那样,Joe Cheng传递反应式表达式的正确方法是没有括号()
callModule(linkedScatter, "scatters", car_data)
一种选择也是模块化你的 API 输入函数,这样你就不需要在模块外定义反应式表达式。可以从此答案中找到模块化输入的示例。在您的代码下方提供正确答案。
library(shiny)
library(ggplot2)
linkedScatterUI <- function(id) {
ns <- NS(id)
fluidRow(
column(6, plotOutput(ns("plot1"), brush = ns("brush"))),
column(6, plotOutput(ns("plot2"), brush = ns("brush")))
)
}
linkedScatter <- function(input, output, session, data, left, right) {
# Yields the data frame with an additional column "selected_"
# that indicates whether that observation is brushed
dataWithSelection <- reactive({
brushedPoints(data(), input$brush, allRows = TRUE)
})
output$plot1 <- renderPlot({
scatterPlot(dataWithSelection(), left())
})
output$plot2 <- renderPlot({
scatterPlot(dataWithSelection(), right())
})
return(dataWithSelection)
}
scatterPlot <- function(data, cols) {
ggplot(data, aes_string(x = cols[1], y = cols[2])) +
geom_point(aes(color = selected_)) +
scale_color_manual(values = c("black", "#66D65C"), guide = FALSE)
}
ui <- fixedPage(
h2("Module example"),
linkedScatterUI("scatters"),
textOutput("summary")
)
server <- function(input, output, session) {
data(mpg)
### My modification
### making the reactive outside of module call
car_data <- reactive({
mpg
})
## Fix This doesn't work by reactive (var) no brackets()
## What is the syntax for being able to call car_data()?
df <- callModule(linkedScatter, "scatters", reactive(car_data),
left = reactive(c("cty", "hwy")),
right = reactive(c("drv", "hwy"))
)
output$summary <- renderText({
sprintf("%d observation(s) selected", nrow(dplyr::filter(df(), selected_)))
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
小智 5
在car_data之后删除parens:
df <- callModule(linkedScatter, "scatters", car_data,
left = reactive(c("cty", "hwy")),
right = reactive(c("drv", "hwy"))
)
Run Code Online (Sandbox Code Playgroud)
该模块似乎想要"未解决"的反应.括号"解析"它们.