【问题标题】:renderUI conditioned by a reactive valuerenderUI 由响应值调节
【发布时间】:2016-01-28 12:12:21
【问题描述】:

今天我正在尝试创建一个 renderUI,其表单和内容取决于 reactiveValues 的值。我正在做一个闪亮的仪表板

在初始状态下,用户会点击按钮,renderUI 的形式会发生变化。如果用户再点击一次,我希望 renderUI 再次采用初始形式。

这是我的代码:

library(shiny)
library(shinydashboard)

sidebar <- dashboardSidebar(

)

body <- dashboardBody(
  uiOutput("Text2Bis")


)#dashboardBody

header <- dashboardHeader(
  title = list(icon("star-half-o"),"element-R")
  #messages,
  #notifications,
  #tasks
)

ui <- dashboardPage(header, sidebar, body)


server <- function(input, output, session) {

  previewAction <- reactiveValues(temp = F)

  output$Text2Bis <- renderUI({

    if(is.null(input$button)){
      box(width = 12,background = NULL, height = 100,
          actionButton("button","delete")
      )
    }
    else{
      print(input$button)
      if((isolate(input$button %%2)) == 1){
        print(input$button)
        box(width = 12,background = NULL, height = 100,
            actionButton("button","delete")
        )

      }
      else{
        print(input$button)
        box(width = 12,background = NULL, height = 300,
            actionButton("button","save")
        )
      }

    }



  })



}

shinyApp(ui, server)

我不明白为什么我的应用程序无法运行。看来actionButton还在重新初始化???

你有解决这个问题的想法吗?

非常感谢您!

干杯

【问题讨论】:

  • 所以看起来这里的问题是你创建动作按钮的反应实际上是检查 input$button 的状态,所以每次状态改变时,整个事情都会再次运行。尝试使用 input$button 隔离这些部分
  • 基本上,如果您在其他地方打印它的状态,是否将其隔离在一个地方并不重要。

标签: r shiny dashboard


【解决方案1】:

是的,我做到了!这个简单的脚本做了这么多工作!!

代码如下:

library(shiny)
library(shinydashboard)

sidebar <- dashboardSidebar(

)

body <- dashboardBody(
  uiOutput("Text2Bis")


)#dashboardBody

header <- dashboardHeader(
  title = list(icon("star-half-o"),"element-R")
  #messages,
  #notifications,
  #tasks
)

ui <- dashboardPage(header, sidebar, body)


server <- function(input, output, session) {

  previewAction <- reactiveValues(temp = 1)

  output$Text2Bis <- renderUI({

    if((previewAction$temp%%2) ==1 ){
      box(width = 12,background = NULL, height = 100,
          actionButton("save","save")

      )
    }
    else{

      box(width = 12,background = NULL, height = 200,
          actionButton("hide","hide"),
          h4("YEAH !!")

      )

    }


  })




observe({
  if(input$save >0){
    isolate(previewAction$temp <- previewAction$temp+1)
  }
})

observe({
  if(input$hide >0){
    isolate(previewAction$temp <- previewAction$temp+1)
  }
})



}

shinyApp(ui, server)

非常感谢大家的帮助!!

干杯

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    相关资源
    最近更新 更多