Display handsom table in a conditional panel


#1

This program works, but I would like to display the handsome table only when the variable validFile = FALSE is returned by the glycoPipe(infileName) function, I do not know what condition to put in the conditional panel. Any ideas?

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      column(width = 10,
             conditionalPanel(
               condition = "input.btn == true",
               selectInput("value", label = h4("Do you have a params file ready to use (Y/N)?"),
                           choices = c("", "Y", "N"), selected = NULL)
               
             ),
             conditionalPanel(
               condition = "input.value == 'Y'",
               helpText(h4("Select your params file")),
               fluidRow(
                 column(width = 10, fileInput("file", "upload the file", accept = ".tsv", placeholder = "No file selected", multiple = FALSE))
               )
               # useShinyjs(),  # Include shinyjs
               # extendShinyjs(text = jscode, functions = c("closeWindow")),
               #actionButton("close", "Stop glycoPipe")
               
             )
     
        )
    ),
    mainPanel(
      
      textOutput("invalidFile"),
  
        helpText("Changes to the table will be automatically saved to the source file"),
        actionButton("saveBtn", "Save"),
        helpText("Handsontable demo output. Column add/delete does work ",
                 "for tables with defined column properties, including type."),
        radioButtons("useType", "Use Data Types", c("TRUE", "FALSE")),
      
        rHandsontableOutput("hot", width = 1500),
      
      uiOutput("checkedFile")
      
    )
)
)

glycoPipe <- function(inFileName){
  
  list[valid, outL] <- extractParams(filename) 
  validFile = FALSE
  invalidParamsFile <- "invalid params file"
  
  list(valid = valid, outL = outL, validFile = validFile, invalidParamsFile = invalidParamsFile)
  
  
}

extractParams <- function(filename){
  
 valid = "valid"
 outL <- list(1,2,3,4,5)
 return(list(valid, outL))
}

server <- function(input, output){
  
  output$contents <- renderTable({
    inFile = input$file
    inFileName = input$file$name
    if(is.null("inFile")){
      return()
    }
    
    req(inFile)
    validate( need(file_ext(inFile) %in% c(
      'tsv'
    ), "Wrong File Format. The selected file is not a valid tab-separated PARAMS file try again. If
     you do not have a parameters.tsv file in your directory stop clycoPipe create a file and re-start glycoPipe"))
    read.delim(inFile$datapath, quote = "", sep = '\t')
    
  })
  
  output$checkedFile <- renderUI({
    inFile <- input$file
    inFileName <- input$file$name
    result <- glycoPipe(inFileName)
    result$outL
    
  })
  
  output$invalidFile <- renderText({
    inFile <- input$file
    inFileName <- input$file$name
    result <- glycoPipe(inFileName)
    if(result$validFile == FALSE)
    result$invalidParamsFile
   
  })
  
  fname = tempfile(fileext = ".tsv")
  observe({
    input$saveBtn
    hot = isolate(input$hot)
    if (!is.null(hot)) {
      write.table(hot_to_r(input$hot), fname)
      print(fname)
    }
    
  })
  
  output$hot = renderRHandsontable({
    if (!is.null(input$hot)) {
      DF = hot_to_r(input$hot)
    } else {
      DF = read.delim( "~/Development/Rand/glycoPipe_PARAMS_TEMPLATE.tsv", sep = '\t', stringsAsFactors = FALSE)
    }
    
    rhandsontable(DF) %>%
      hot_table(highlightCol = TRUE, highlightRow = TRUE)
  })
  values <- reactiveValues()
  data = reactive({
    if (!is.null(input$hot)) {
      DF = hot_to_r(input$hot)
    } else {
      if (is.null(values[["DF"]]))
        DF = data.frame(val = 1:10, bool = TRUE, nm = LETTERS[1:10],
                        dt = seq(from = Sys.Date(), by = "days", length.out = 10),
                        stringsAsFactors = F)
      else
        DF = values[["DF"]] 
      DF = read.delim("~/Development/Rand/glycoPipe_PARAMS_TEMPLATE.tsv", sep = '\t', stringsAsFactors = FALSE)
      
    }
    
    values[["DF"]] = DF
    DF
  })
  
  
  output$hot <- renderRHandsontable({
    DF = data()
    if (!is.null(DF))
      rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all")
  })
  
}

shinyApp(ui = ui, server = server)

Shiny applications not supported in static R Markdown documents


#2

You can just use

output$hot = renderRHandsontable({

if(glycoPipe(infleName) == FALSE){
  return()
  }
...
})

Unless I’m missing something obvious?


#3

You did not understand my question. Please see solution below:

  output$hot = renderRHandsontable({
          #  inFile = input$file
           # inFileName = input$file$name
            result <- glycoPipe(inFileName())
            if(result$validFile == FALSE){
           if (!is.null(input$hot)) {
             DF = hot_to_r(input$hot)
           } else {
             DF = read.delim( "~/Development/Rand/glycoPipe_PARAMS_TEMPLATE.tsv", sep = '\t', stringsAsFactors = FALSE)
           }
           
           rhandsontable(DF) %>%
             hot_table(highlightCol = TRUE, highlightRow = TRUE)
         }
         })

         values <- reactiveValues()
         data = reactive({
           if (!is.null(input$hot)) {
             DF = hot_to_r(input$hot)
           } else {
             if (is.null(values[["DF"]]))
               DF = data.frame(val = 1:10, bool = TRUE, nm = LETTERS[1:10],
                               dt = seq(from = Sys.Date(), by = "days", length.out = 10),
                               stringsAsFactors = F)
             else
               DF = values[["DF"]] 
             DF = read.delim("~/Development/Rand/glycoPipe_PARAMS_TEMPLATE.tsv", sep = '\t', stringsAsFactors = FALSE)
             
           }
           
           values[["DF"]] = DF
           DF
         })
        
           
         output$hot <- renderRHandsontable({
          # inFile = input$file
          # inFileName = input$file$name
           result <- glycoPipe(inFileName())
           if(result$validFile == FALSE){
           
           DF = data()
           if (!is.null(DF)){
             rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all")
           }
           }
         })