我的主 Shiny 面板中有 uiOutput 和 plotOutput 組件。
plotOutput("plot_data"),
uiOutput("summary_data")
我在服務器函式中有典型的代碼來回應和填充每個組件,例如:
output$plot_data <- renderPlot({
hist(data_vars())
})
output$summary_data <- renderPrint({
summary(data_vars())
})
我想為每個添加功能,將另一個的輸出組件設定為 NULL 或空字串等,以便這兩個輸出共享相同的空間。當一個有資料時,另一個是空的。我不認為它會這樣作業,但它可能看起來像這樣:
output$plot_data <- renderPlot({
# Code to "flatten" uiOutput
# Then populate the component
hist(data_vars())
})
output$summary_data <- renderPrint({
# Code to "flatten" plotOutput
# Then populate the component
summary(data_vars())
})
我認為這可以使用observeEvent來完成,但我還沒有找到一種方法來完全洗掉一個內容,以便另一個可以在頁面上占據相同的空間。請幫忙。謝謝你。
uj5u.com熱心網友回復:
而不是有一個單獨的plotOutputand printOutput,你可以只有一個uiOutput,然后你可以在服務器中添加代碼來顯示你想要在那個插槽中的輸出。這是一個作業示例,我添加了一個按鈕以在視圖之間切換。
library(shiny)
ui <- fluidPage(
actionButton("swap","Swap"),
uiOutput("showPart")
)
server <- function(input, output, session) {
showState <- reactiveVal(TRUE)
observeEvent(input$swap, {showState(!showState())})
output$plot_data <- renderPlot({
hist(mtcars$mpg)
})
output$summary_data <- renderPrint({
summary(mtcars)
})
output$showPart <- renderUI({
if (showState()) {
plotOutput("plot_data")
} else {
verbatimTextOutput("summary_data")
}
})
}
shinyApp(ui, server)
使用此方法只會在 uiOutput 插槽中呈現兩個輸出中的一個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/344153.html
