R Shiny Observe Running Before Loading of UI and This Causes Null Parameters

How do I show default values prior to triggering ObserveEvent in R shiny?

Please see below resolved code. Main problem was with defined functions; they had to be changed to reflect the differently-dimensioned input matrices that should be being used in calculations. Also renderPlot was incorrectly calling on those same defined functions. It is now corrected in the below final code. Removing req per the comment sent me down the right path because it's usage was covering up the underlying issues with the defined functions.

library(shiny);library(shinyMatrix);library(shinyjs)

matrix1.input <- function(x){
matrixInput(
x,
value = matrix(c(0.2), 4, 1, dimnames = list(c("A","B","C","D"),NULL)),
rows = list(extend = FALSE, names = TRUE),
cols = list(extend = FALSE, names = FALSE, editableNames = FALSE),
class = "numeric")}

matrix2.input <- function(x,y,z){
matrixInput(
x,
value = matrix(c(y,z),1,2,dimnames=list(NULL,c("Y","Z"))),
rows = list(extend = TRUE, names = FALSE),
cols = list(extend = FALSE, names = TRUE, editableNames = FALSE),
class = "numeric")}

matrix.validate <- function(x,y){
a <- y
a[,1][a[,1]>x] <- x
b <- diff(a[,1,drop=FALSE])
b[b<=0] <- NA
b <- c(1,b)
a <- cbind(a,b)
a <- na.omit(a)
a <- a[,-c(3),drop=FALSE]
return(a)}

vector.base <- function(x,y){
a <- rep(y,x)
b <- seq(1:x)
c <- data.frame(x = b, y = a)
return(c)}

vector.multi <- function(x,y,z){
a <- rep(NA, x)
a[y] <- z
a[seq_len(min(y)-1)] <- a[min(y)]
if(max(y) < x){a[seq(max(y)+1, x, 1)] <- 0}
a <- approx(seq_along(a)[!is.na(a)],a[!is.na(a)],seq_along(a))$y
b <- seq(1:x)
c <- data.frame(x = b, z = a)
return(c)}

vector.multiFinal <- function(x,y){vector.multi(x,matrix.validate(x,y)[,1],matrix.validate(x,y)[,2])}

ui <-
pageWithSidebar(
headerPanel("Model"),
sidebarPanel(
conditionalPanel(condition="input.tabselected==2",
sliderInput('periods','',min=1,max=120,value=60),
matrix1.input("base_input"),
useShinyjs(),
actionButton('showBtn','Show vector'),
actionButton('hideBtn','Hide vector'),
uiOutput("Vectors")),
), # close sidebar panel
mainPanel(tabsetPanel(
tabPanel("Dynamic", value=2,plotOutput("graph1")),
id = "tabselected")
) # close main panel
) # close page with sidebar

server <- function(input,output,session)({
periods <- reactive(input$periods)
base_input <- reactive(input$base_input)
vector_input <- reactive(input$vector_input)

observeEvent(input$periods|input$base_input,{
updateMatrixInput(session,"vector_input",
value=matrix(c(input$periods,input$base_input[1,1]),1,2,
dimnames=list(NULL, c("Y","Z")))
) # close update matrix
}) # close observe event

# --- Show performance vector if user elects
output$Vectors <- renderUI({
req(input$showBtn)
tagList(matrix2.input("vector_input",input$periods,input$base_input[1,1]))
}) # close render UI

observeEvent(input$showBtn, {shinyjs::show("Vectors")})
observeEvent(input$hideBtn, {shinyjs::hide("Vectors")})

output$graph1 <- renderPlot(
if(input$showBtn == 0)
plot(vector.base(periods(),input$base_input[1,1]),type="b")
else
plot(vector.multiFinal(periods(),matrix.validate(periods(),vector_input())),type="b")
) # close render plot
}) # close server

shinyApp(ui, server)

RShiny: conditional success or error message depending on NULL values

You could use isTruthy to check if the input is filled in:

  observeEvent(input$submit,
if(isTruthy(input$text)) {
sendSweetAlert(
session = session,
title = "Plz halp",
text = "Cool stuff doing neat things",
type = "success",
btn_labels = c("Great")
)
} else {
sendSweetAlert(
session = session,
title = "Plz halp",
text = "Please Provide Text",
type = "error",
btn_labels = c("Sorry!")
)
})

Edit: Based on your comment you code do something like this to avoid repeating things. In global or elsewhere, .ake a function that will validate a list

allTruthy <- function(l) {
all(vapply(l, isTruthy, logical(1)))
}

Then in your server, make your form data a reactive list:

  t1Form <- reactive(list(
pop_interest = input$t1_pop_interest,
int_interest = input$t1_int_interest,
comparator = input$t1_comparator,
poutcome = input$t1_poutcome,
setting = input$t1_setting
))

Now your form data is in a nice list for use and validation. Then your observer can be:

  observeEvent(input$submit,
if(allTruthy(t1Form())) {
sendSweetAlert(
session = session,
title = "Plz halp",
text = "Cool stuff doing neat things",
type = "success",
btn_labels = c("Great")
)
} else {
sendSweetAlert(
session = session,
title = "Plz halp",
text = "Please Provide Text",
type = "error",
btn_labels = c("Sorry!")
)
})

In R Shiny, why do my functions not work when using the render UI function but work fine when not using render UI?

See comment. An alternative solution is to replace commented out line in original MWE, without using req function (req caused me problems later because it didn't address a core issue with using the defined functions at the top of the MWE, it allowed the user to skip over situations where there are mis-matches in matrix dimensions), with the following line of code:

observeEvent(vector.final(periods(),matrix.validate(yield_input(),periods())),{yield_vector.R <<- vector.final(periods(),matrix.validate(yield_input(),periods()))})

Another task of mine will be to simplify those functions!

Using observeEvent in shiny with null values

observeEvent(input$id, {
code
...
}, ignoreNULL = FALSE)

?observeEvent

This was a recent discovery for me too

Dynamic input selection in shiny app

It looks like the observe event is not reacting to the resultOut variable or it runs before the select input is rendered. Interesting problem. The only solution I have come up to is to render the complete selectInput (including the choices) in the output[["opts2"]]. This is the code:

output[["opts2"]] <- renderUI({

if(is.null(resultOut)) return()
dataOut <- resultOut()
yList <- dataOut$Variable
fluidRow(selectInput('yOut', 'Y Variable', choices = yList),
tags$hr(),
actionButton("submit1", "Proceed")
)
})

and of remove the last observer.

This discussion probably relates to this.
R shiny Observe running Before loading of UI and this causes Null parameters



Related Topics



Leave a reply



Submit