如何解决闪亮:用一些文本描述或默认图表替换renderPlotly错误
我正在创建一个有光泽的应用程序,并且我想使用brush
函数。该应用当前如下所示:
在一个box()
中出现错误Error: argument 1 is not a vector
。当用户对图进行笔刷选择时,此错误消失。
如何将错误掩盖为更人性化的描述,例如“请选择上图中的区域以生成此窗口...”。
图box()
应该用于绘制用户选择,例如图的放大版本。
代码:
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)
解决方法
您可以使用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()
)
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。