在 R/Shiny 中将输出组件设置为空
Set an Output Component to Empty in R/Shiny
我的 Shiny 主面板中有 uiOutput 和 plotOutput 组件。
plotOutput("plot_data"),
uiOutput("summary_data")
我在服务器函数中有典型的代码来反应和填充每个组件,例如:
output$plot_data <- renderPlot({
hist(data_vars())
})
output$summary_data <- renderPrint({
summary(data_vars())
})
我想为每个添加功能,将另一个的输出组件设置为 NULL 或空字符串等,以便这两个输出共享相同的 space。当一个有数据时,另一个是空的。我不认为它会以这种方式工作,但它可能看起来像这样:
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 来完成,但我还没有找到一种方法来完全删除一个内容,以便另一个可以在页面上占据相同的内容 space。请帮忙。谢谢。
而不是有一个单独的 plotOutput
和 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 插槽中呈现。
我的 Shiny 主面板中有 uiOutput 和 plotOutput 组件。
plotOutput("plot_data"),
uiOutput("summary_data")
我在服务器函数中有典型的代码来反应和填充每个组件,例如:
output$plot_data <- renderPlot({
hist(data_vars())
})
output$summary_data <- renderPrint({
summary(data_vars())
})
我想为每个添加功能,将另一个的输出组件设置为 NULL 或空字符串等,以便这两个输出共享相同的 space。当一个有数据时,另一个是空的。我不认为它会以这种方式工作,但它可能看起来像这样:
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 来完成,但我还没有找到一种方法来完全删除一个内容,以便另一个可以在页面上占据相同的内容 space。请帮忙。谢谢。
而不是有一个单独的 plotOutput
和 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 插槽中呈现。