使用 lookup tables 绘制 ggplot 和 table

using lookup tables to plot a ggplot and table

我正在创建一个闪亮的应用程序,我让用户选择应该在绘图和 table 中显示的数据。这个选择是通过 3 个不同的输入变量完成的,分别包含 14、4 和两个选择。

ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = "DataSource", label = "Data source", choices = 
c("Restoration plots", "all semi natural grasslands")),

selectInput(inputId = "Variabel", label = "Variable", choices = 
choicesVariables)),
#choicesVariables definition is omitted here, because it's very long but it 
#contains 14 string values

selectInput(inputId = "Factor", label = "Factor", choices = c("Company 
type", "Region and type of application", "Approved or not approved 
applications", "Age group"  ))

),

dashboardBody(
plotOutput("thePlot"),
tableOutput("theTable")
))

这总共有 73 个选择(是的,我知道那里的数学不相加,但有些选择是无效的)。我想使用查找 table 来执行此操作,因此创建了一个包含每个有效选择组合的查找,如下所示:

rad1<-c(rep("Company type",20), rep("Region and type of application",20), 
rep("Approved or not approved applications", 13), rep("Age group", 20))

rad2<-choicesVariable[c(1:14,1,4,5,9,10,11, 1:14,1,4,5,9,10,11, 1:7,9:14, 
1:14,1,4,5,9,10,11)]

rad3<-c(rep("Restoration plots",14),rep("all semi natural grasslands",6), 
rep("Restoration plots",14), rep("all semi natural grasslands",6), 
rep("Restoration plots",27), rep("all semi natural grasslands",6))

rad4<-1:73

letaLista<-data.frame(rad1,rad2,rad3, rad4)

colnames(letaLista) <- c("Factor", "Variabel", "rest_alla", "id")

现在它易于使用的子集只获取用户做出的选择。但是我如何在不使用 73 行长的 ifelse 语句的情况下使用这些信息来绘制情节和 table?

我试图创建某种多维数组,可以容纳所有 table(还有一个用于绘图),但我无法让它工作。我对这类数组的经验有限,这可能是一个简单的问题,但任何提示都会有所帮助!

我的数据集是绘图的基础,table 由具有 23 个变量、因子和数值的数据框组成。然后使用以下代码为所有 73 种组合创建图表和表格

s_A1 <- summarySE(Samlad_info, measurevar="Dist_brukcentrum", 
groupvars="Companytype")
s_A1 <- s_A1[2:6,]

p_A1=ggplot(s_A1, aes(x=Companytype, 
y=Dist_brukcentrum))+geom_bar(position=position_dodge(), stat="identity") +
geom_errorbar(aes(ymin=Dist_brukcentrum-se, 
ymax=Dist_brukcentrum+se),width=.2,position=position_dodge(.9))+
scale_y_continuous(name = "") + scale_x_discrete(name = "")

其中 summarySE 是以下函数,来自 cookbook for R

summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=TRUE,
                    conf.interval=.95, .drop=TRUE) {


# New version of length which can handle NA's: if na.rm==T, don't count them
length2 <- function (x, na.rm=FALSE) {
  if (na.rm) sum(!is.na(x))
  else       length(x)
}

# This does the summary. For each group's data frame, return a vector with
# N, mean, and sd
datac <- ddply(data, groupvars, .drop=.drop,
               .fun = function(xx, col) {
                 c(N    = length2(xx[[col]], na.rm=na.rm),
                   mean = mean   (xx[[col]], na.rm=na.rm),
                   sd   = sd     (xx[[col]], na.rm=na.rm)
                 )
               },
               measurevar
)

# Rename the "mean" column    
datac <- rename(datac, c("mean" = measurevar))

datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean

# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval: 
# e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
ciMult <- qt(conf.interval/2 + .5, datac$N-1)
datac$ci <- datac$se * ciMult

return(datac)
}

完整的代码有点大,但我希望这可以阐明我正在尝试做的事情。

好吧,感谢 florian 的评论,我想我可能已经找到了自己的解决方案。我将在此处展示它,但将问题悬而未决,因为可能有更简洁的方法。

我将绘图(由 ggplot 创建为列表)组装成一个列表

plotList <- list(p_A1, p_A2, p_A3...)
tableList <- list(s_A1, s_A2, s_A3...)

然后我在查找中使用了子集 table 来获取列表的匹配 ID select 正确的图和 table.

output$thePlot <-renderPlot({

plotValue<-subset(letaLista, letaLista$Factor==input$Factor & 
letaLista$Variabel== input$Variabel & letaLista$rest_alla==input$DataSource)

plotList[as.integer(plotValue[1,4])]
})

output$theTable <-renderTable({ 

plotValue<-subset(letaLista, letaLista$Factor==input$Factor & 
letaLista$Variabel== input$Variabel & letaLista$rest_alla==input$DataSource)

skriva <- tableList[as.integer(plotValue[4])]
print(skriva)   

})