簡體   English   中英

隱藏/顯示輸出 Shiny R

[英]Hide/show outputs Shiny R

我試圖找出每次用戶在 widjets 中更改某些內容時如何顯示和隱藏我的輸出,如圖形和表格。 例如,我有一個名為“性別”的變量的滑塊輸入,有 2 個選擇:男性和女性。 我還有一個按鈕,當用戶點擊它時執行估計。 每次當用戶在不同的 widjet 之間更改至少一個選擇時,我都想隱藏輸出。 例如,經過一次估計后,用戶決定僅更改教育水平,當用戶單擊滑塊輸入框時,我想隱藏以前的結果。

我嘗試使用 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")
      }      
})

謝謝你。

你的代碼沒有工作的原因是因為你沒有做出通話useShinyjs()如果你在閱讀使用shinyjs的任何實例文檔或看,你會看到,你必須調用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)

正如 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
})

這里的其他答案似乎沒有提供正確/完整的答案。 解決方法其實很簡單。

您需要使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM