conditional panel

Published by onesixx on

  •  

 

https://shiny.rstudio.com/reference/shiny/1.0.1/conditionalPanel.html

ConditionalPanel

 

 

https://stackoverflow.com/questions/39405136/how-to-use-actionbutton-as-a-trigger-to-conditionalpanel?rq=1

conditionalPanel works on client side(js), so its not see your variable.

Here you can do it on server side using renderUI

 

library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("Button_Initial", "Button_Initial")
)

#*------------------------------------------------------------------------- ====
server <- function(input, output, session) {
  init_env1 <- function(){
    env <- new.env()
    env$num <- 0
    return(env)
  }
  env1 <- init_env1()
  #num <<- 0
  observeEvent(input$Button_Initial, {
      showModal(
        modalDialog(title="Clustering", easyClose=F, size="l", #footer=tagList( modalButton("Cancel"), actionButton("pCluster_MainUI_apply", "Apply") ),
                    fluidRow(column(width=12,
                                    plotOutput("Plot1", width="100%"))),
                    fluidRow(column(width=12,
                                    plotOutput("Plot2", width="100%"))), 
                    
                    fluidRow(column(width=4,
                                    actionButton("btn_toggle", label="toggle", click="btn_toggle_click"))),

                                    
                    fluidRow(#condition = "input.btn_toggle_click %% 2 == 1" ,
                                     #condition = "num %% 2 == 1" ,
                                     #condition = "env1$num %% 2 == 1" ,
                                     
                                     column(width=10, hr(),rHandsontableOutput("Table1"))
                    )
        )
      )
      uFunc_Plot1()
      uFunc_Plot2()
      uFunc_Table1()
      
    
      hide("Plot2")
      hide("Table1")
      show("Plot1")
  })
  
  observeEvent(input$btn_toggle, {
    #if (isolate(input$btn_toggle) %% 2 == 1) {
      toggle("Plot1")
      toggle("Plot2")
      toggle("Table1")
   # }
    
 })

  uFunc_Plot1 <- function(){
    output$Plot1 <- renderPlot({
      p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width))+ geom_point(aes(color="red"))
      return(p)
    })
  }
  uFunc_Plot2 <- function(){
    output$Plot2 <- renderPlot({
      p <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width))+ geom_point() 
      return(p)
    })
  }
  
  uFunc_Table1 <- function(){
    output$Table1 <- renderRHandsontable({
      rhandsontable(iris[1:5,], readOnly = TRUE, selectCallback = TRUE)
    })
  } 
#***************************************************************************************************************** -----  
}

shinyApp(ui, server)

 

 

 

library(shinyjs)

ui <- fluidPage(
  actionButton("Button_Initial", "Button_Initial")
)

#*------------------------------------------------------------------------- ====
server <- function(input, output, session) {
  observeEvent(input$Button_Initial, {
    showModal(
      modalDialog(title="Clustering", easyClose=F, size="l", #footer=tagList( modalButton("Cancel"), actionButton("pCluster_MainUI_apply", "Apply") ),
                  uiOutput("ui1"),
                  fluidRow(column(width=4, actionButton("btn_toggle", label="toggle"))),
                  uiOutput("ui2")                
      )
    )
    uFunc_ui_scree()   
  })
  
  observeEvent(input$btn_toggle, {
    #print(isolate(input$btn_toggle))
    if (isolate(input$btn_toggle) %% 2 == 0){ uFunc_ui_scree() }else{ uFunc_ui_cluster() }
  })
  
  uFunc_ui_scree <- function(){
    output$ui1 <- renderUI({
      fluidRow(
        column(width=12, plotOutput("Plot1", width="100%")))
    })
    output$ui2 <- renderUI({ return() })
    
    output$Plot1 <- renderPlot({
      p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width))+ geom_point(aes(color="red"))
      return(p)
    })
  }
  
  uFunc_ui_cluster <- function(){
    output$ui1 <- renderUI({
      fluidRow(
        column(width=12, plotOutput("Plot2", width="100%")))
    })
    output$ui2 <- renderUI({
      fluidRow(
        hr(),
        column(width=10, rHandsontableOutput("Table1"))
      )
    })
    
    output$Plot2 <- renderPlot({
      p <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width))+ geom_point() 
      return(p)
    })
    output$Table1 <- renderRHandsontable({
      rhandsontable(iris[1:5,], readOnly = TRUE, selectCallback = TRUE)
    })
  }
  #***************************************************************************************************************** -----  
}

shinyApp(ui, server)

 

 

 

Categories: R-Shiny

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x