0
votes

I am new to Shiny. What I want to do in my application is, running & displaying some part of the code only when a condition on another calculation is met.

The conditionalPanel works fine with the conditions on input values but I could not figure out how to do this with the 'output' values, i.e., conditionally on the output values of the functions. Below is my example code:

library(shiny)
msLocation <- "msLoc"
searchMWText <- "searchMW"
bid <- "2333333"
fulltext <- "fullDisplay"

ui <- fluidPage(
  titlePanel("Run server codes conditionally"),

  sidebarLayout(
    sidebarPanel(
      helpText("Evaluate input and run different parts of the code depending on the output functions"),
      br(),
      sliderInput("rand", "select seed", min = 1, max = 50, step = 1, value = 1)
     ),

mainPanel(
  fluidRow(conditionalPanel("output.rand == 1"),
           tags$h4("Here comes the default part"),
           br(),
           textOutput("defaultCalc")),
  
  fluidRow(conditionalPanel("output.randomint != 1",
                            tags$h4("I can evaluate if the chosen number is even or odd."),
                            br(),
                            textOutput("evenodd")
                            ),
           fluidRow(conditionalPanel("output.evenodd == 'Number is even'",
                                    tags$h4("Number even calculation "),
                                    textOutput("msLoc"),
                                    br(),
                                    textOutput("searchMW"),
                                    br(),
                                    textOutput("defaultID"),
                                    br()
                                    ),
           fluidRow(conditionalPanel("output.evenodd == 'Number is odd'",
                                     tags$h4("Here is some id:", textOutput("id")),
                                     textOutput("displayFull")
                                     )
           )
  

  )
)
)))
#
server <- function(input, output) {
  rand1 <- reactive({
    if(is.null(input$rand)){return(NULL)}
    rn <- input$rand
    return(rn)
  })

  randomint <- reactive({
    seedn <- rand1()
    set.seed(seedn)
    rint <- sample(1:50, 1)
    return(rint)
  })

  calc1 <- reactive({
    intn <- randomint()
    modn <- intn %% 2
    return(modn)
  })

  evenOdd <- reactive({
    modn <- calc1()
    if(modn == 0){valueText = "Number is even"}
    if(modn != 0){valueText = "Number is odd"}
    return(valueText)
  })

  idtext <- reactive({
    idint <- sample(1:10000, 3)
    idint <- as.character(idint)
    idint <- paste(idint, collapse = "")
    return(idint)
  })

  output$defaultCalc <- renderText({
    as.character(randomint())
  })

  output$evenodd <- renderText({
    evenOdd()
  })

  output$searchMW <- renderText({
    searchMWText
  })

  output$defaultID <- renderText({
    bid
  })

  output$id <- renderText({
    idtext()
  })

  output$displayFull <- renderText({
    fulltext
  })

  }

  shinyApp(ui = ui, server = server)

The problem is, the parts after default always appear, e..g., 'Here is some id' text always appears and this is not what I want. I want to display 'Here is some id' and run the calculation (idtext) only when the number is odd.The number is not coming from the slider input, the slider input is providing the seed only. The number is also calculated and depends on its value, the other parts should be run and displayed. Until the user selects a slider input value, only the 'default part' should be displayed and nothing else.

I searched a lot and could not find a solution that mentions the conditions on output. What is the best way to solve this?

1

1 Answers

2
votes

Do:

  randomint <- reactive({
    seedn <- rand1()
    set.seed(seedn)
    rint <- sample(1:50, 1)
    return(rint)
  })
  output$randomint <- reactive(randomint())
  outputOptions(output, "randomint", suspendWhenHidden = FALSE)

Then you can use "output.randomint !== 1".