RShiny: Как получить доступ к пользовательскому интерфейсу на сервере и использовать его для печати определенных индексов списка? - PullRequest
0 голосов
/ 10 июня 2019

Я создал блестящую веб-страницу приложения с динамическими элементами для печати списков. Я работаю с очень большими списками, и я обращаюсь к определенным частям этих списков, чтобы делать такие вещи, как сюжет. Пользователь выбирает из выпадающего меню, а затем страница автоматически обновляется. Все работает нормально, но, как я понял, мой код содержит более 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
        }
    })

...