代码之家  ›  专栏  ›  技术社区  ›  user113156

闪亮:用一些文本描述或默认绘图替换renderPlotly错误

  •  0
  • user113156  · 技术社区  · 5 年前

    我正在创建一个闪亮的应用程序,我想使用 brush 功能。该应用程序目前看起来像:

    enter image description here

    在哪里 box() 我明白了 Error: argument 1 is not a vector 当用户对绘图进行刷选时,此错误消失。

    enter image description here

    我如何将错误隐藏为更用户友好的描述,例如“请选择上述绘图的一个区域以生成此窗口…”。

    情节 盒子() 应该绘制用户选择,例如绘图的放大版本。

    代码:

    data <- mtcars
    
    
    
    library(shiny)
    library(shinydashboard)
    
    header <- dashboardHeader(title = "My Dashboard")
    sidebar <- dashboardSidebar(
        sidebarMenu(
            menuItem(text = "Main Menu", tabName = "MainPage")
        )
    )
    body <- dashboardBody(
        tabItems(
            tabItem(tabName = "MainPage",
                    fluidRow(
                        box(
                            width = 12,
                            plotOutput("myPlot1", brush = "userSelectedSummaryStatistics")
                        )
                    ),
                    fluidRow(
                        box(
                            width = 4,
                            DT::dataTableOutput("userDefinedTable")
                        ),
                        box(
                            width = 8,
                            plotlyOutput("selectedUserDataFromUserDefinedTable")
                        )
                    )
            )
        )
    )
    
    
    ui <- dashboardPage(
        header, sidebar, body
    )
    
    server <- function(input, output, session){
        output$myPlot1 <- renderPlot({
            data %>% 
                ggplot(aes(x = mpg, y = disp)) +
                geom_line()
        })
        
        selectedUserData = reactive({
            userSelectedSummaryStatistics = input$userSelectedSummaryStatistics
            userSelection = brushedPoints(data, userSelectedSummaryStatistics)
            return(userSelection)
        })
        
        output$userDefinedTable = DT::renderDataTable(DT::datatable(selectedUserData()))
        
        output$selectedUserDataFromUserDefinedTable <- renderPlotly({
            ggplotly(
                selectedUserData() %>% 
                    ggplot(aes(x = mpg, y = disp)) +
                    geom_line()
            )
        })
    }
    
    
    shinyApp(ui, server)
    
    0 回复  |  直到 5 年前
        1
  •  4
  •   Waldi    5 年前

    您可以使用 validate :

      output$selectedUserDataFromUserDefinedTable <- renderPlotly({
        validate(need(nrow(selectedUserData())>0,'Please select an area of the above plot in order to generate this window'))
        ggplotly(
          selectedUserData() %>% 
            ggplot(aes(x = mpg, y = disp)) +
            geom_line()
        )
      })
    

    enter image description here

    推荐文章