gpt4 book ai didi

r - Shiny :用一些文本描述或默认绘图替换 renderPlotly 错误

转载 作者:行者123 更新时间:2023-12-03 23:04:41 24 4
gpt4 key购买 nike

我正在创建一个 Shiny 的应用程序,我想使用 brush功能。该应用程序目前看起来像:
enter image description here
在哪一个box()我收到错误 Error: argument 1 is not a vector .当用户对绘图进行刷选时,此错误就会消失。
enter image description here
如何将错误屏蔽为更用户友好的描述,例如“请选择上图的一个区域以生成此窗口...”。
剧情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()
)
})
enter image description here

关于r - Shiny :用一些文本描述或默认绘图替换 renderPlotly 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63430489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com