【发布时间】:2016-08-10 06:50:51
【问题描述】:
我希望移植一些较旧的 Shiny 应用程序以使用 Shiny 模块,但在尝试移植我的反应式表达式时遇到了麻烦。
目标不是阻止模块与其交互 包含应用程序,而是使这些交互明确。如果一个 模块需要使用响应式表达式,取响应式 表达式作为函数参数。
我有现有的反应式表达式,可以从我想传入的 API 等导入数据,但似乎找不到语法。如果我修改下面给定的Shiny module example,我会遇到同样的问题。
任何人都可以修改以下内容,以便您可以将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)
【问题讨论】:
-
这可能会有所帮助:stackoverflow.com/a/36517069/4222792 为什么必须进行反应性外部模块调用?它不能是你的一些模块的输出吗?您的 dataImportApi 可能是一个模块?
-
谢谢米凯尔。它的外部原因是它用于包中的通用模块,我想将任何反应数据帧传递给它,而不仅仅是一个特定的 API 调用。