dagg3r
dagg3r

Reputation: 341

r shiny - Get radio button value as variable

I am new with r shiny and I am trying to get selected value of a radio button as variable and then concatenate it with something else. Here is my code:

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("This is test app"),

  sidebarLayout(
    sidebarPanel(
      radioButtons("rd",
                   label="Select window size:",
                   choices=list("100","200","500","1000"),
                   selected="100")
    ),
    mainPanel(
         //Something
    )
  )
))  

server.R

library(shiny)

shinyServer(function(input, output) {


  ncount <- reactive({input$rd})
  print(ncount)
  my_var <- paste(ncount,"100",sep="_")

})

Now when I print ncount it prints out "ncount" rather than the value stored in the variable. Is there anything that I'm missing here.

Thanks

Upvotes: 2

Views: 11558

Answers (1)

Sumedh
Sumedh

Reputation: 4965

UI

library(shiny)
shinyUI(fluidPage(
  titlePanel("This is test app"),

  sidebarLayout(
    sidebarPanel(
      radioButtons("rd",
                   label = "Select window size:",
                   choices = list("100" = 100,"200" = 200,"500" = 500,"1000" = 1000),
                   selected = 100)
    ),
    mainPanel(
      verbatimTextOutput("ncount_2")
    )
  )
)) 

Server

library(shiny)

shinyServer(function(input, output) {


# The current application doesnt need reactive

  output$ncount_2 <- renderPrint({
    ncount <- input$rd
    paste(ncount,"100",sep="_")
    })

  # However, if you need reactive for your actual data, comment the above part
  # and use this instead


  # ncount <- reactive({input$rd})
  # 
  # output$ncount_2 <- renderPrint({ 
  #   paste(ncount(),"100",sep="_")
  # })



})

Upvotes: 10

Related Questions