2

Let's say I have created 10 selectInput dropdowns for a multi plot export and these selectInputs are called "xaxis_1", "xaxis_2", ..... , "xaxis_10"

for a single 1 I can write: if(!is.null(input$xaxis_1)) { .... do stuff } to stop it running export when the user hasn't entered any name, and presses submit, to avoid crashes.

A bit more general you can check this:

if(!is.null(input[[paste('xaxis', i, sep = '_')]])) { ...}

how can you write it elegantly so that 1 line of code checks whether ANY of the 1:10 input[[...]] is empty, i.e. NULL?

The nr of inputs depends on how many plots the user wants to export per file, so all is build with lapply(1:input$nrofplots, function(i) { .... } renderUI structure, and my if statement needs to have the same flexibility of 1:n

In a situation like below in the image, pressing Initiate export should give a sweetalert (got that covered) saying there is at least 1 value missing

enter image description here

Mark
  • 2,789
  • 1
  • 26
  • 66
  • `req()` suits you? – LocoGris Mar 17 '19 at 11:11
  • no, I would like to fire a warning sweetalert if the equation returns that any input is empty. In my case n is flexible since the nr of inputs depends on how many plots the user sets to generate by the way – Mark Mar 17 '19 at 11:12

1 Answers1

2

Here a snippet I used in the UI side to validate the user's inputs.

library(shiny)
library(shinyjs)

ui <- fluidPage(
 useShinyjs(),  # Set up shinyjs
 numericInput('axis1','Val 1',1),
 numericInput('axis2','Val 2',1),
 numericInput('axis3','Val 3',1),
 actionButton('Go','Plot')
)

server <- function(input, output, session) {
  #Try 1, space, AAA and check what shiny will return
  observe(print(input$axis1))
  observe({
    All_Inputs <- vapply(paste0('axis',1:3),
                                function(x){isTruthy(input[[x]])},
                                logical(1))
    All_InputsCP <- all(All_Inputs)
    shinyjs::toggleState(id="Go", condition = All_InputsCP) #This is to make the button Go able or disable according to condition All_InputsCP #
  })
}

shinyApp(ui, server)

I hope it helps.

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • Ah nice trick A Suliman. Knew about most individual elements but have never used isTruthy before, and toggleState seems a nice simplification of ::enable ::disable which I use a lot in my app. Is there any major advances over using isTruthy rather than !is.null here? – Mark Mar 17 '19 at 11:21
  • @Mark as you can see above Shiny returns NA when users inter values not compatible with the input method. So `> !is.null(NA) [1] TRUE`. – A. Suliman Mar 17 '19 at 11:32
  • ok. Could your function be modified to test for all 4 lists of inputs as in my image? the common name of the 4 sets are xaxis_channel_n, xaxis_parameter_n, yaxis_channel_n, yaxis_parameter_n – Mark Mar 17 '19 at 11:34
  • and then c() around all paste0 vectors I assume would do the trick in the top line – Mark Mar 17 '19 at 11:38
  • Yes and see `?sprintf` to make it more easy for you than `paste0` – A. Suliman Mar 17 '19 at 11:41