Shiny Plot Height ведет себя по-разному на разных рабочих столах - PullRequest
2 голосов
/ 19 октября 2019

Я пытаюсь создать пользовательский интерфейс, в котором мне нужно построить 4 графика внутри каждой вкладки,
, но я получаю прокрутку при рендеринге графиков, и когда я добавляю высоту, исчезает полоса прокрутки 240, и она работает для того же размера. рабочий стол, но на разных размерах рабочего стола он ведет себя по-разному, и я снова получаю полосу прокрутки.

Мотив состоит в том, чтобы разместить графики на экране без полосы прокрутки, также я хотел бы получить отзыв о том, что я правильно создаю пользовательский интерфейс

Спасибо

UI

navbarPage("NarBar",
       tabPanel("Tab1",
          column(12,      
              column(4,
                  column(12,     
                     checkboxInput("ID1", "Checkbox1", FALSE)
                     ),
                  column(12,
                     checkboxInput("ID2", "Checkbox2", FALSE)
                  )
                ),
              column(8,
                column(3,
                       selectInput("ID1", "Select1:",
                                       c("A" = "a",
                                         "B" = "b",
                                         "C" = "c"))
                ),
                column(3,
                       selectInput("ID2", "Select2:",
                                   c("A" = "a",
                                     "B" = "b",
                                     "C" = "c"))
                ),
                column(3,
                       selectInput("ID3", "Select3:",
                                   c("A" = "a",
                                     "B" = "b",
                                     "C" = "c"))
                ),
                column(3,
                       selectInput("ID4", "Select4:",
                                   c("A" = "a",
                                     "B" = "b",
                                     "C" = "c")
                                   )
                )
              )
            ),
              column(12,
                     column(4,
                        column(12,    
                           selectInput("ID1", "Select1:",
                                       c("A" = "a",
                                         "B" = "b",
                                         "C" = "c")
                                       )
                           ),
                        column(12,    
                           selectInput("ID2", "Select2:",
                                       c("A" = "a",
                                         "B" = "b",
                                         "C" = "c")
                                       )
                           ),
                        column(12,    
                               selectInput("ID3", "Select3:",
                                           c("A" = "a",
                                             "B" = "b",
                                             "C" = "c")
                               )
                        ),
                        column(12,    
                               selectInput("ID4", "Select4:",
                                           c("A" = "a",
                                             "B" = "b",
                                             "C" = "c")
                               )
                        ),
                        column(12,    
                               selectInput("ID5", "Select5:",
                                           c("A" = "a",
                                             "B" = "b",
                                             "C" = "c")
                               )
                        ),
                        column(12,    
                               selectInput("ID6", "Select6:",
                                           c("A" = "a",
                                             "B" = "b",
                                             "C" = "c")
                               )
                          )
                      ),
                     column(width = 8,
                            tabsetPanel(
                              tabPanel(title = 'Tab1',
                                    column(width = 6,
                                           plotOutput('plot1',height = 240)
                                    ),
                                    column(width = 6,
                                           plotOutput('plot2',height = 240)
                                    ),
                                    column(width = 6,
                                           plotOutput('plot3',height = 240)
                                    ),
                                    column(width = 6,
                                           plotOutput('plot4',height = 240)
                                    )
                              ),
                              tabPanel(title = 'Tab2', 
                                       column(width = 6,
                                              plotOutput('plot5',height = 240)
                                       ),
                                       column(width = 6,
                                              plotOutput('plot6',height = 240)
                                       ),
                                       column(width = 6,
                                              plotOutput('plot7',height = 240)
                                       ),
                                       column(width = 6,
                                              plotOutput('plot8',height = 240)
                                       )
                              ),
                              tabPanel(title = 'Tab3',
                                       column(width = 6,
                                              plotOutput('plot9',height = 240)
                                       ),
                                       column(width = 6,
                                              plotOutput('plot10',height = 240)
                                       ),
                                       column(width = 6,
                                              plotOutput('plot11',height = 240)
                                       ),
                                       column(width = 6,
                                              plotOutput('plot12',height = 240)
                                       )
                                 )
                            )
                     )   
            )              
       ),

       tabPanel("Tab 2"
        ),
       tabPanel("Tab 3"
        ),
       tabPanel("Tab 4"
        )
     )

сервер , просто использующий один и тот же график для каждого plotOutput

function(input, output, session) {

  output$plot1 <- renderPlot({        #just use the same plot on different plots like plot2,plot3,etc
  cars2 <- cars + rnorm(nrow(cars))
  plot(cars2)
  })

}

1 Ответ

1 голос
/ 20 октября 2019

Во-первых, в вашем коде много повторяющихся частей. Мы можем использовать lapply для более эффективного создания этих деталей следующим образом.

Основываясь на этом ответе (https://stackoverflow.com/a/26785047/7669809), Я добавил tags$head(tags$style(".shiny-plot-output{height:40vh !important;}")) к вашему navbarPage. Кажется, это работает. Вы можетезамените 40vh на другой номер, который вы хотели бы использовать.

library(shiny)
library(htmlwidgets)

ui <- navbarPage("NarBar",
                 tags$head(tags$style(".shiny-plot-output{height:40vh !important;}")),
                 tabPanel("Tab1",
                          column(12,      
                                 column(4,
                                        lapply(1:2, function(x){
                                          column(12,
                                                 checkboxInput(paste0("ID", x), 
                                                               paste0("Checkbox", x), 
                                                               FALSE)
                                          )
                                        })
                                 ),
                                 column(8,
                                        lapply(1:4, function(x){
                                          column(3,
                                                 selectInput(paste0("ID", x), 
                                                             paste0("Select", x, ":"),
                                                             c("A" = "a",
                                                               "B" = "b",
                                                               "C" = "c")))
                                        })
                                 )
                          ),
                          column(12,
                                 column(4,
                                        lapply(1:6, function(x){
                                          column(12,    
                                                 selectInput(paste0("ID", x), 
                                                             paste0("Select", x, ":"),
                                                             c("A" = "a",
                                                               "B" = "b",
                                                               "C" = "c")
                                                 )
                                          )
                                        })
                                 ),
                                 column(width = 8,
                                        tabsetPanel(
                                          tabPanel(title = 'Tab1',
                                                   lapply(1:4, function(x){
                                                     column(width = 6,
                                                            plotOutput(paste0('plot', x))
                                                     )
                                                   })
                                          ),
                                          tabPanel(title = 'Tab2', 
                                                   lapply(5:8, function(x){
                                                     column(width = 6,
                                                            plotOutput(paste0('plot', x))
                                                     )
                                                   })
                                          ),
                                          tabPanel(title = 'Tab3',
                                                   lapply(9:12, function(x){
                                                     column(width = 6,
                                                            plotOutput(paste0('plot', x))
                                                     )
                                                   })
                                          )
                                        )
                                 )   
                          )              
                 ),

                 tabPanel("Tab 2"
                 ),
                 tabPanel("Tab 3"
                 ),
                 tabPanel("Tab 4"
                 )
)

server <- function(input, output, session) {

  lapply(1:12, function(x) {
    output[[paste0('plot', x)]] <- renderPlot({
      cars2 <- cars + rnorm(nrow(cars))
      plot(cars2)
    })
  })
}

shinyApp(ui, server)
...