我创建了一个闪亮的应用程序网页,其中包含用于打印列表的动态元素。我正在处理非常大的列表,并且正在访问这些列表的某些部分以执行诸如绘图之类的操作。用户从下拉菜单中选择,然后页面自动更新。一切工作正常,但是按照我的方式,我的代码超过3000行。我相信,没有这些if / else语句,必须有一种更有效的方式来做我正在做的事情。我提供的代码是我正在使用的代码的简化版本。我仅使用mtcars数据集,以便可以重现我的程序。
library(shiny)
library(DT)
library(htmltools)
library(formattable)
library(lubridate)
library(ggplot2)
list1 = list("year" = 1, "plot" = ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point())
list2 = list("year" = 2, "plot" = ggplot(mtcars, aes(x=wt, y=disp)) + geom_point())
list3 = list("year" = 3, "plot" = ggplot(mtcars, aes(x=wt, y=hp)) + geom_point())
list4 = list("year" = 4, "plot" = ggplot(mtcars, aes(x=wt, y=drat)) + geom_point())
list5 = list("year" = 5, "plot" = ggplot(mtcars, aes(x=wt, y=qsec)) + geom_point())
list6 = list("year" = 6, "plot" = ggplot(mtcars, aes(x=mpg, y=disp)) + geom_point())
list7 = list("year" = 7, "plot" = ggplot(mtcars, aes(x=mpg, y=hp)) + geom_point())
list8 = list("year" = 8, "plot" = ggplot(mtcars, aes(x=mpg, y=drat)) + geom_point())
list9 = list("year" = 9, "plot" = ggplot(mtcars, aes(x=mpg, y=qsec)) + geom_point())
myFirstList = list(list1, list2, list3, list4, list5, list6, list7, list8, list9)
mySecondList = list(list9, list8, list7, list6, list5, list4, list3, list2, list1)
u.n <- 1:9
names(u.n) <- u.n
ui <- shinyUI(
navbarPage("",
tabPanel("Home",
fluidPage(
fluidRow(column(12,
selectInput("type", "",
c("1" = "1",
"2" = "2"), width = "33%")
)
)
)
),
navbarMenu("Tab 1",
tabPanel("Home",
sidebarLayout(
sidebarPanel(
fluidRow(selectInput('year','Select Year', choices = u.n, width = "100%"))
),
mainPanel(
fluidRow(
plotOutput('distPlot'),
style = "padding-bottom:50px"
)
)
)
)
))
)
服务器
server <- shinyServer(function(input,output,session){
output$distPlot <- renderPlot({
if (input$year == "1"){
if (input$type == "1"){
myFirstList[[1]]$plot
}
else if (input$type == "2"){
mySecondList[[1]]$plot
}
}
else if (input$year == "2"){
if (input$type == "1"){
myFirstList[[2]]$plot
}
else if (input$type == "2"){
mySecondList[[2]]$plot
}
}
else if (input$year == "3"){
if (input$type == "1"){
myFirstList[[3]]$plot
}
else if (input$type == "2"){
mySecondList[[3]]$plot
}
}
else if (input$year == "4"){
if (input$type == "1"){
myFirstList[[4]]$plot
}
else if (input$type == "2"){
mySecondList[[4]]$plot
}
}
else if (input$year == "5"){
if (input$type == "1"){
myFirstList[[5]]$plot
}
else if (input$type == "2"){
mySecondList[[5]]$plot
}
}
else if (input$year == "6"){
if (input$type == "1"){
myFirstList[[6]]$plot
}
else if (input$type == "2"){
mySecondList[[6]]$plot
}
}
else if (input$year == "7"){
if (input$type == "1"){
myFirstList[[7]]$plot
}
else if (input$type == "2"){
mySecondList[[7]]$plot
}
}
else if (input$year == "8"){
if (input$type == "1"){
myFirstList[[8]]$plot
}
else if (input$type == "2"){
mySecondList[[8]]$plot
}
}
else if (input$year == "9"){
if (input$type == "1"){
myFirstList[[9]]$plot
}
else if (input$type == "2"){
mySecondList[[9]]$plot
}
}
})
})
shinyApp(ui = ui,server = server)
我想要这样的服务器作为代替,但这不起作用
output$distPlot <- renderPlot({
if (input$type == "1"){
myFirstList[[input$year]]$plot
}
if (input$type == "2"){
mySecondList[[input$year]]$plot
}
})