Variables vanish in selectinput in shiny app

I have a shiny app here, based on the "shinymeta" package, which captures the logic and exposes it as R-code that, can be run outside the shiny.

However, When I upload a dataset and get all my variables in the selectinput, the variables for the "color", "facet row" and "facet column" selectinput, vanish, after generating a plot, and clicking on "showcode". How can this be fixed? I have tried many things, but it just doesn't work.

I have added "None" in the updateselectinput so if the variable is not needed, the user can choose "None", but directly after generating the first plot, all other variable from the uploaded dataset vanish from the shiny.

Thanks in advance.

library(shiny)
library(ggplot2)
library(shinymeta)
library(data.table)


ui <- shinyUI(fluidPage(
  titlePanel("Plot"),
  tabsetPanel(
    tabPanel("Upload File",
             titlePanel("Uploading Files"),
             sidebarLayout(
               sidebarPanel(
                 uiOutput('file1_ui'), 
                 tags$br(),
                 checkboxInput('header', 'Header', TRUE),
                 radioButtons('sep', 'Separator',
                              c(Comma=',',
                                Semicolon=';',
                                Tab='\t'),
                              ','),
                 radioButtons('quote', 'Quote',
                              c(None='',
                                'Double Quote'='"',
                                'Single Quote'="'"),
                              '"'),
                 actionButton("reset_file", "Reset")
                 
               ),
               mainPanel(
                 tableOutput('contents')
               )
             )
    ),
    ############################################
    tabPanel("First Type",
             pageWithSidebar(
               headerPanel('My First Plot'),
               sidebarPanel(
                 
                 selectInput('xcol', 'X Variable',""),
                 selectInput('ycol', 'Y Variable', ""),
                 selectInput('color', 'Color',choices = " "),
                 checkboxInput('smooth', 'Smooth'),
                 selectInput('facet_row', 'Facet Row', "",),
                 selectInput('facet_col', 'Facet Column', ""),
                 hr(),
                 checkboxInput("plotly1", "Interactive plot!",value = FALSE, width=140),
                 uiOutput("graghbutton"),
                 actionButton("generateCode", label = "reproducibility!",width = "90%", style='background-color: #9fc5e8;'),
                 actionButton("reset", "Reset")
                 
                 
               ),
               mainPanel(
                 outputCodeButton(plotOutput('MyPlot', click = "plot_click",
                                             hover = hoverOpts(id = "plot_hover", delayType = "throttle"),
                                             brush = brushOpts(id = "plot_brush"))),
                 hr(),
                 tableOutput("plot_brushedpoints"),
                 hr()
                 
               )
             )
    )
    
  )
)
)


server <- shinyServer(function(input, output, session) {
  
  
  output$graghbutton <- renderUI({
    actionButton("plotxxx", "Plot!")
    
  })
  
  
  observeEvent(is.null(input$file1), {
    showNotification("Upload the file for data analysis",duration = 7)
  })
  
  
  
  data <- metaReactive2({
    req(input$file1)
    if (endsWith(input$file1$datapath, "RDS")) {
      df <-metaExpr({
        readRDS(..(input$file1$datapath))
      })
    } else {
      df<- metaExpr({
        fread(..(input$file1$datapath))
      })
    }
    
    
    
    updateSelectInput(session, inputId = 'xcol', label = 'X Variable',
                      choices = names(df))
    updateSelectInput(session, inputId = 'ycol', label = 'Y Variable',
                      choices = names(df))
    
    
    updateSelectInput(session, inputId = 'color', label = 'Color',
                      choices = c('None', names(df)) )
    
    updateSelectInput(session, inputId = 'facet_row', label = 'Facet Row',
                      choices = c(None='.', names(df)))
    
    
    updateSelectInput(session, inputId = 'facet_col', label = 'Facet Column',
                      choices = c(None='.',names(df)))
    
    
    
    
    return(df)
  })
  
  
  observeEvent(input$file1, {
    output$contents <- renderTable({
      data()
    })
    
  })
  
  
  output$file1_ui <- renderUI({
    input$reset_file ## Create a dependency with the reset button
    fileInput('file1', 'Choose CSV File',
              multiple = FALSE,
              accept=c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv", ".fit", ".RDS"))
  })
  
  
  
  observeEvent(input$reset_file, {
    output$contents <- renderTable({
      
    })
    
  })
  
  graph_1 <- metaReactive2({
    req(input$plotxxx)
    
    graph <- isolate(metaExpr({
      
      ggplot(..(data()), aes(.data[[..(input$xcol)]], .data[[..(input$ycol)]])) +
        geom_point()
      
      
      
    }))
    
    if (input$color != 'None')
      graph <- isolate(metaExpr({
        
        ..(graph) + aes_string(color=..(input$color))
        
        
      }))
    
    
    facets <- isolate(metaExpr({
      paste(..(input$facet_row), '~', ..(input$facet_col))
      
      
      
    }))
    
    
    if (facets != '. ~ .')
      graph <- isolate(metaExpr({
        ..(graph) + facet_grid(..(facets))
      }))
    
    
    if (input$smooth)
      graph <- isolate(metaExpr({
        ..(graph) + geom_smooth()
        
      }))

    graph
    
    
    
  })
  
  
  
  observeEvent(input$plotxxx, {
    req(input$plotxxx)
    output$MyPlot <- metaRender2(renderPlot, {
      isolate(metaExpr({
        ..(graph_1())
        
        
      })

      )
    })
    
    
  })
  
  
  observeEvent(input$reset, {
    
    output$MyPlot <- renderPlot({
      
    })

  })
  
  
  observeEvent(input$MyPlot_output_code, {
    
    code <- expandChain(
      quote({
        library(ggplot2)
      }),
      
      output$MyPlot()
    )
    
    displayCodeModal(
      code = code,
      title = "Code to reproduce plot ",easyClose = T,fade = T
      
      
      
    )
  })
  
  
  observeEvent(input$generateCode, {
    code <- expandChain(
      quote({
        library(ggplot2)
        
      }),
      output$MyPlot() 
    )
    
    displayCodeModal(
      code = code,
      title = "Code to reproduce VPC"
    )
  })
  
  
  output$plot_brushedpoints <- renderTable({
    df<- data()  
    res <- brushedPoints(df, input$plot_brush,
                         xvar = input$xcol,
                         yvar = input$ycol,
                         panelvar1 = NULL,
                         panelvar2 = NULL,
                         allRows = FALSE
    )
    if (nrow(res) == 0|is.null(res))
      return(NULL)
    res
  })
  

})

shinyApp(ui, server)


This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.