我正在创建一个闪亮的应用程序,我让用户选择应该在绘图和表格中显示哪些数据。这个选择是通过3个不同的输入变量完成的,这些变量分别包含14个,4个和2个选项。
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个选择(是的,我知道数学并没有在那里加起来,但有些选择是无效的)。我想使用查找表来执行此操作,以便创建一个具有如下所有选项的有效组合:
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语句?
我试图创建一些可以容纳所有表格的多维数组(以及一个用于绘图的表格),但我无法使其工作。我对这类数组的经验是有限的,这可能是一个简单的问题,但任何提示都会有所帮助!
我的数据集是图和表的基础,包含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)
}
其中的代码有点大,但我希望这可以澄清我想要做的事情。
答案 0 :(得分:1)
嗯,感谢florian的评论,我想我可能已经找到了解决方案。我会在这里提出这个问题但是问题仍然存在,因为可能有更简洁的方法。
我将图表(由ggplot创建的列表)装入列表
helper()
然后我在查找表上使用子集来获取列表的匹配id,以选择正确的图和表。
-- :name do-something! :! :1
INSERT INTO SomeTable (someColumn) VALUES (helper());