Hide/show 输出 Shiny R
Hide/show outputs Shiny R
我正在尝试找出每次当用户在 widjet 中更改某些内容时如何显示和隐藏我的输出,如图形和表格。例如,我有一个名为 "gender" 的变量输入滑块,有 2 个选择:男性和女性。我还有一个按钮,当用户点击它时执行估计。每次用户在不同的 widjet 之间至少更改一个选择时,我都想隐藏输出。例如,经过一次估计,用户决定只更改教育水平,当用户单击 sliderInput 框时,我想隐藏以前的结果。
我尝试使用 R 包 shinyjs 和函数 hide/show 但它们不适用于输出。
你知道如何在不使用 shinyjs 包的情况下做到这一点吗?
这是我的部分代码:
shinyUI(fluidPage(
sidebarLayout(
fluidRow(
column(4, wellPanel(
fluidRow(
column(5,selectInput("gender",
label = div("Sexe",style = "color:royalblue"),
choices = list("Male", "Female"),
selected = "Female")),
# other different widjets..
column(8, plotOutput('simulationChange')),
column(4, tableOutput('simulationChangeTable'),
tags$style("#simulationChangeTable table {font-size:9pt;background-color: #E5E4E2;font-weight:bold;margin-top: 121px; margin-left:-30px;overflow:hidden; white-space:nowrap;text-align:left;align:left;}",
media="screen",
type="text/css"),
fluidRow(
column(6, tableOutput('simulationChangeEsperance'),
tags$style("#simulationChangeEsperance table {font-size:9pt;background-color: #E5E4E2;font-weight:bold;margin-top: -10px; margin-left:-30px;overflow:hidden; white-space:wrap;word-break: break-word;width:173px;text-align:left;}"))
)
)
)
)
)
))
shinyServer(function(input, output, session) {
# part of my server.R code
observe({
if (input$gender|input$age|input$birthplace|input$education){
shinyjs::hide("simulationChange")
shinyjs::hide("simulationChangeTable")
shinyjs::hide("simulationChangeEsperance")
}
})
谢谢。
正如 Dieter 在评论中提到的,您需要使用 conditionalPanel。例如,在您的 ui.R 中,而不是
plotOutput('simulationChange')
使用
conditionalPanel("output.show", plotOutput('simulationChange'))
并在您的 server.R 中添加以下内容:
values <- reactiveValues()
values$show <- TRUE
observe({
input$gender
input$age
input$birthplace
input$education
values$show <- FALSE
})
output$show <- reactive({
return(values$show)
})
另外,点击按钮时不要忘记更改 values$show
:
observeEvent(input$button, {
...
values$show <- TRUE
})
您的代码不起作用的原因是您没有调用 useShinyjs()
(如果您阅读文档或查看任何使用 shinyjs 的示例,您会发现您有在 UI).
中调用 useShinyjs()
我无法复制你的代码,因为它有太多错误,但只是为了证明它确实适用于输出,这里有一个你可以 运行 的小例子。在您的项目中,只需在 UI.
的某处添加 shinyjs::useShinyjs()
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
actionButton("hideshow", "Hide/show plot"),
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
plot(rnorm(100))
})
observeEvent(input$hideshow, {
# every time the button is pressed, alternate between hiding and showing the plot
toggle("plot")
})
}
shinyApp(ui = ui, server = server)
这里的其他答案似乎没有提供 right/complete 答案。解决方法其实很简单。
您需要使用outputOptions(output, 'show', suspendWhenHidden = FALSE)
下面是一个示例代码,如果下拉选择为 2,则显示 conditionalPanel
内的文本,如果为 1,则隐藏。
library(shiny)
ui <- fluidPage(
selectInput("num", "Choose a number", 1:2),
conditionalPanel(
condition = "output.show",
"The selected number is 2 so this text is displayed. Change it back to 1 to hide."
)
)
server <- function(input, output, session) {
output$show <- reactive({
input$num == 2 # Add whatever condition you want here. Must return TRUE or FALSE
})
outputOptions(output, 'show', suspendWhenHidden = FALSE)
}
shinyApp(ui = ui, server = server)
我正在尝试找出每次当用户在 widjet 中更改某些内容时如何显示和隐藏我的输出,如图形和表格。例如,我有一个名为 "gender" 的变量输入滑块,有 2 个选择:男性和女性。我还有一个按钮,当用户点击它时执行估计。每次用户在不同的 widjet 之间至少更改一个选择时,我都想隐藏输出。例如,经过一次估计,用户决定只更改教育水平,当用户单击 sliderInput 框时,我想隐藏以前的结果。
我尝试使用 R 包 shinyjs 和函数 hide/show 但它们不适用于输出。
你知道如何在不使用 shinyjs 包的情况下做到这一点吗?
这是我的部分代码:
shinyUI(fluidPage(
sidebarLayout(
fluidRow(
column(4, wellPanel(
fluidRow(
column(5,selectInput("gender",
label = div("Sexe",style = "color:royalblue"),
choices = list("Male", "Female"),
selected = "Female")),
# other different widjets..
column(8, plotOutput('simulationChange')),
column(4, tableOutput('simulationChangeTable'),
tags$style("#simulationChangeTable table {font-size:9pt;background-color: #E5E4E2;font-weight:bold;margin-top: 121px; margin-left:-30px;overflow:hidden; white-space:nowrap;text-align:left;align:left;}",
media="screen",
type="text/css"),
fluidRow(
column(6, tableOutput('simulationChangeEsperance'),
tags$style("#simulationChangeEsperance table {font-size:9pt;background-color: #E5E4E2;font-weight:bold;margin-top: -10px; margin-left:-30px;overflow:hidden; white-space:wrap;word-break: break-word;width:173px;text-align:left;}"))
)
)
)
)
)
))
shinyServer(function(input, output, session) {
# part of my server.R code
observe({
if (input$gender|input$age|input$birthplace|input$education){
shinyjs::hide("simulationChange")
shinyjs::hide("simulationChangeTable")
shinyjs::hide("simulationChangeEsperance")
}
})
谢谢。
正如 Dieter 在评论中提到的,您需要使用 conditionalPanel。例如,在您的 ui.R 中,而不是
plotOutput('simulationChange')
使用
conditionalPanel("output.show", plotOutput('simulationChange'))
并在您的 server.R 中添加以下内容:
values <- reactiveValues()
values$show <- TRUE
observe({
input$gender
input$age
input$birthplace
input$education
values$show <- FALSE
})
output$show <- reactive({
return(values$show)
})
另外,点击按钮时不要忘记更改 values$show
:
observeEvent(input$button, {
...
values$show <- TRUE
})
您的代码不起作用的原因是您没有调用 useShinyjs()
(如果您阅读文档或查看任何使用 shinyjs 的示例,您会发现您有在 UI).
useShinyjs()
我无法复制你的代码,因为它有太多错误,但只是为了证明它确实适用于输出,这里有一个你可以 运行 的小例子。在您的项目中,只需在 UI.
的某处添加shinyjs::useShinyjs()
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
actionButton("hideshow", "Hide/show plot"),
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
plot(rnorm(100))
})
observeEvent(input$hideshow, {
# every time the button is pressed, alternate between hiding and showing the plot
toggle("plot")
})
}
shinyApp(ui = ui, server = server)
这里的其他答案似乎没有提供 right/complete 答案。解决方法其实很简单。
您需要使用outputOptions(output, 'show', suspendWhenHidden = FALSE)
下面是一个示例代码,如果下拉选择为 2,则显示 conditionalPanel
内的文本,如果为 1,则隐藏。
library(shiny)
ui <- fluidPage(
selectInput("num", "Choose a number", 1:2),
conditionalPanel(
condition = "output.show",
"The selected number is 2 so this text is displayed. Change it back to 1 to hide."
)
)
server <- function(input, output, session) {
output$show <- reactive({
input$num == 2 # Add whatever condition you want here. Must return TRUE or FALSE
})
outputOptions(output, 'show', suspendWhenHidden = FALSE)
}
shinyApp(ui = ui, server = server)