Conditional statement in server.R

Hi all,

Not sure what wrong I am doing here. Since the length(key) is 0, I need to assign the value of selectedInput to key_as . If the length(key) is not 0, then key_as should be "a"

ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
            uiOutput("edit_condition")
        )
    )
))

server.R

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

    columnNames <- c("A","B","C")
    key <- c()
    key_1 <- c("a")
    # print(length(key))

    if(length(key) == 0){
        output$edit_condition <- renderUI({
            selectInput("ed", "Condition", choices = c(columnNames), selected = columnNames[[1]])
        })
        key_as <- input$ed
    } else {
        key_as <- key_1
    }

    print(key_as)
})
library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
        "Number of bins:",
        min = 1,
        max = 50,
        value = 30
      )
    ),

    # Show a plot of the generated distribution
    mainPanel(
      uiOutput("edit_condition"),
      verbatimTextOutput("keyreport")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  columnNames <- c("A", "B", "C")
  key <- c()
  key_1 <- c("a")
  # print(length(key))

  output$edit_condition <- renderUI({
    selectInput("ed", "Condition", choices = c(columnNames), selected = columnNames[[1]])
  })


  key_as <- reactive({
    if (length(key) == 0) {
      key_as <- input$ed
    } else {
      key_as <- key_1
    }
  })

  output$keyreport <- renderText(
    key_as()
  )
}

shinyApp(ui, server)

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

No you don't. I just wanted to see it

Thanks Nir. I did try this . Actually, I do not need verbatimTextOutput("keyreport") in UI. But I need the value that is selected from Input to be assigned to key_as. Because U use this value later on in my application. So i do need any UI for this :slight_smile: .Is it possible?

given your code, that would depend on length(key). as its 0 then it would come from input$ed

ok then :). But If I call key_as anywhere in the server, will it take what is been selected from the dropdown?