【问题标题】:In Shiny R render html conditionally based on user selection在 Shiny R 中,根据用户选择有条件地渲染 html
【发布时间】:2022-01-10 16:46:56
【问题描述】:

我想根据用户输入显示不同的 html 文件。基本上,用户在两个 pickerinput 元素中进行选择,并根据选择显示不同的 html 文件。

我的 ui 中有。 R

  fluidRow( style = "background-color:#FFFAFA00;",
       
            htmlOutput("example")
  ))),

在我的服务器中。 R

示例

if (input$chap == "ai" & input$cat == "ch") {
  htmlOutput("aich")
} 

else if (input$chap == "ai" & input$cat == "pr") {
  htmlOutput("aipr")
  }
})

选择时没有任何反应。关于此的任何想法

【问题讨论】:

    标签: html r shiny server-side-rendering reactive


    【解决方案1】:

    我们可以试试这个:

    observe({
      
      if (input$chap == "ai" & input$cat == "ch") {
        output$example <- renderText("html_code_here")
      } 
      
      else if (input$chap == "ai" & input$cat == "pr") {
        output$example <- renderText("html_code_here")
      }
    })
    

    另外,我认为observeEvent(c(input$chap, input$chap), {...}) 可以工作。

    根据提供的信息很难判断这是否可行,但我建立了一个示例。

    library(shiny)
    
    ui <- fluidPage(
      sidebarLayout(
      sidebarPanel(textInput(inputId = 'chap','input 1',placeholder = 'ai'),
                   textInput(inputId = 'cat', 'input 2',placeholder = 'ch')),
      mainPanel(htmlOutput('example')))
      
    )
    
    server <- function(input, output, session) {
      observe({
        req(input$chap)
        
        if (input$chap == "ai" & input$cat == "ch") {
          output$example <- renderText("<h1>This is the H1</h1>")
        } 
        
        else { if (input$chap == "ai" & input$cat == "pr") {
          output$example <- renderText("aipr")
        }
        }  
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2020-06-12
      • 2020-08-20
      • 2021-02-05
      • 2013-04-03
      • 2020-05-21
      • 2022-10-14
      • 2022-12-13
      • 2019-04-01
      • 2017-02-22
      相关资源
      最近更新 更多