Home > Back-end >  conditionalpanel doesn't work after applying shinymodules
conditionalpanel doesn't work after applying shinymodules

Time:07-28

I have modulized my shiny app and built a simple function to run it, but the conditionalpanel in the app, which creates a plotly of the ggplot has stopped working, is there any way to fix that? I have been playing with it the whole night, without any success. What am I missing? grateful for all your help.

library(shiny)
library(plotly)

dataset <- mtcars

mod_ui <- function(id){
  
  pageWithSidebar(
    headerPanel("Mtcars"),
    sidebarPanel(sliderInput(NS(id,'sampleSize'), 'Sample Size', min=10, max=nrow(dataset),
                             value=min(10, nrow(dataset)), step=5, round=0),
                 selectInput(NS(id, 'x'), 'X', names(dataset)),
                 selectInput(NS(id, 'y'), 'Y', names(dataset), names(dataset)[[2]]),
                 selectInput(NS(id, 'color'), 'Color', c('None', names(dataset))),
                 checkboxInput(NS(id, 'smooth'), 'Smooth'),
                 selectInput(NS(id, 'facet_row'), 'Facet Row', c(None='.', names(dataset))),
                 selectInput(NS(id,'facet_col'), 'Facet Column', c(None='.', names(dataset))),
                 hr(),
                 checkboxInput(NS(id, "plotly1"), "Interactive plot!",value = FALSE, width=140),
                 actionButton(NS(id, "plot"), "Plot!")
    ),
    mainPanel(
      plotOutput(NS(id,'plot')),
      hr(),
      hr(),
      conditionalPanel("input.plotly1 === true", plotlyOutput(NS(id,"plot2")))
    )
  )
  
}


mod_server <- function(id){
  moduleServer(id, function(input, output, session){
    
    dataset <- reactive( { mtcars[sample(nrow(mtcars), input$sampleSize),] }) 
    gragh <- reactive({
      p <- ggplot(dataset(), aes_string(x=input$x, y=input$y))   geom_point()
      if (input$color != 'None')
        p <- p   aes_string(color=input$color)
      facets <- paste(input$facet_row, '~', input$facet_col)
      if (facets != '. ~ .')
        p <- p   facet_grid(facets)
      
      if (input$smooth)
        p <- p   geom_smooth()
      
      p
    }) |> bindEvent(input$plot)
    
    output$plot <- renderPlot({
      gragh()
    })
    
    output$plot2 <- renderPlotly({
      ggplotly(gragh())
    })
    
    
  }
    
    )
}


simApp <- function(){
  ui <- shinyUI(
    mod_ui("ww")
  ) 
    
  server <- function(input, output, session){
    mod_server("ww")
  }
  shinyApp(ui, server)
}



simApp()


CodePudding user response:

In your UI module, do:

  conditionalPanel(
    ns = NS(id),
    "input.plotly1 === true", 
    plotlyOutput(NS(id, "plot2"))
  )
  • Related