【发布时间】:2018-08-21 14:05:10
【问题描述】:
我要做的是将一个模块的值(封装在单选按钮中的选项)传递给另一个模块(接受选项并输出适合它的图表)。
我已经阅读了很多关于reactive() 和相关功能的信息,但仍然不知道如何正确使用该功能。在我看来,在声明某事时应该只需要调用reactive()一次——并且在整个使用过程中,某事将是反应性的。不过,情况似乎并非如此:
第一个模块的服务器功能:
# Return the choice user selects
sidePanel <- function(input, output, session) {
selected_graph <- reactive({input$selected_graph})
return(selected_graph)
}
第二个模块的服务器功能:
source('graph_choices.R')
mainPanel <- function(input, output, session, data, choice) {
output$graph <- renderPlot({
match.fun(reactive(choice))(data)
})
}
graph_choices.R
choice_1 <- function(data) {
ggplot(data, aes(x=time, y=score)) + geom_point()
choice_2 <- function(data) {
ggplot(data, aes(x=eyelashes, y=score)) + geom_bar(stat='identity')
应用的服务器功能
server <- function(input, output, session) {
file_name <- parseQueryString(isolate(session$clientData$url_search))
sounds <- read.csv(paste('/srv/tmp_data/', file_name, sep=""), encoding='UTF-8')
sounds_trimmed <- filter(sounds, score != -1)
sidePanel <- callModule(sidePanel, "side")
mainPanel <- callModule(mainPanel, "main", data=sounds_trimmed, choice=sidePanel())
}
所以,这里发生的情况是,第一个模块设置了带有两个单选按钮的侧面板——一个值为choice_1,一个值为choice_2。选择按钮后,return 位会吐出选择,并将其传递给第二个模块的服务器功能,该功能会吐出适当的图形。
我知道我的问题在于我对 reactive() 的使用,因为没有它,一切都“正常”(默认情况下选择第一个单选按钮,该选择被赋予第二个模块,并且适当的图表是显示。)但是,当我更改单选按钮时,没有任何反应。
对于将reactive 放在哪里,我有很多不同的排列,有时会得到不同的结果(一旦我用reactive() 封闭了第二个模块的输出,并且似乎发生了一些变化,但仍然没有更新)。
有人愿意帮助我吗?
根据this 问题,我还尝试了mainPanel <- callModule(mainPanel, "main", data=sounds_trimmed, selected_graph=sidePanel) 和match.fun(selected_graph())(data);图表会打印出来(和以前一样,但在更改按钮时不会改变)。
【问题讨论】:
-
我认为如果您提供一个可重现的示例(可以运行的 ui+server 的最小示例),您可能会有更多的运气。这里有很多信息,我很难理解。