我有一个闪亮的应用程序,其中基于选定的选项(列)数据集是动态重新计算的。重新计算的结果用于向用户显示另一组选项并创建绘图。
目前我observe()
用于读取用户选择、重新计算数据集和更新 UI。但是,在显示输出(绘图)时,我必须再次重新计算,因为observe()
不会返回任何内容。
有没有办法只重新计算一次数据集?
我创建了一个简单的例子来说明这一点:
library(devtools)
library(shiny)
runGist('7333949')
在server.R
我想调用自定义函数AggregateData
inobserve()
而没有调用 in renderUI()
。
应用代码:
服务器.R:
# shiny server side code for each call
shinyServer(function(input, output, session){
#update variable and group based on dataset
observe({
require(sqldf)
if (is.null(input$source_columns)) {
obj <-TestData
} else {
obj<-AggregateData(TestData,Columns=input$source_columns)
}
var.opts<-namel(colnames(obj))
var.opts.original.slicers <- namel(colnames(TestData))
measures <- c('m1','m2','m3','m4','m5')
var.opts.slicers <- var.opts[!(var.opts %in% c(measures,'x'))]
var.opts.original.slicers <- var.opts.original.slicers[!(var.opts.original.slicers %in% c(measures,'x'))]
var.opts.measures <- var.opts[var.opts %in% measures]
updateSelectInput(session, "source_columns", choices = var.opts.original.slicers, selected=var.opts.slicers)
updateSelectInput(session, "xaxis", choices = var.opts.slicers,selected="x")
updateSelectInput(session, "yaxis", choices = var.opts.measures,selected="m1")
})
output$plot <- renderUI({
plotOutput("p")
})
#plotting function using ggplot2
output$p <- renderPlot({
require(ggplot2)
obj <- AggregateData(TestData,Columns=input$source_columns)
p <- PlotData(obj,x=input$xaxis, y=input$yaxis)
print(p)
})
})
ui.R:
shinyUI(pageWithSidebar(
# title
headerPanel("Analysis setup"),
#input
sidebarPanel
(
selectInput("source_columns","Source Columns:", "Loading...",multiple=TRUE),
selectInput("xaxis","X Axis:", "Loading..."),
selectInput("yaxis","Y Axis:", "Loading...")
),
# output
mainPanel(
#h3('Vintage Analysis'),
uiOutput("plot") # depends on input
)
))
全球.R:
#initialize
library(ggplot2)
TestData <- data.frame( a = rep(LETTERS[1:4],10),
b = rep(c('A','B'),20),
c = rep(LETTERS[1:5],each=8),
d = rep(c('A','B'),2,each=10),
m1 = rnorm(40),
m2 = rnorm(40),
m3 = rnorm(40),
m4 = rnorm(40),
m5 = rnorm(40),
x = rep(1:5,each=8)
)
#helper function (convert vector to named list)
namel<-function (vec){
tmp<-as.list(vec)
names(tmp)<-as.character(unlist(vec))
tmp
}
# Function to aggregate data based on selected columns (Source Columns)
AggregateData <- function(data,Columns=NA) {
require(sqldf)
if (all(is.na(Columns))) {
sql <- "select
sum(m1) as m1, sum(m2) as m2, sum(m3) as m3, sum(m4) as m4, sum(m5) as m5, x
from TestData group by x"
sqldf(sql)
} else {
sql <- paste("select ", paste(Columns, collapse =','), ",
sum(m1) as m1, sum(m2) as m2, sum(m3) as m3, sum(m4) as m4, sum(m5) as m5, x
from TestData group by ",paste(Columns, collapse =','),", x")
sqldf(sql)
}
}
# Function to plot data
PlotData <- function(data,x="x",y="m1") {
ggplot(data, aes_string(x=x, y=y)) + geom_line()
}