0

I need to combine two select box values in R shiny. Select box 1 have year, select box 2 have month.

If user select 2018 and 06, I should get 2018-06 into a variable.

I tried paste(input$year,input$month,sep="-") But it is not working.

qwww
  • 1,313
  • 4
  • 22
  • 48

1 Answers1

3

This should do, note that I changed from reative to reactiveValues as I think this should be more intutive for you where you can use the v$value which will contain what you want. I suggest you have a read over https://shiny.rstudio.com/articles/reactivity-overview.html so you have a better grasp of what is happening

library(shiny)

ui <- fluidPage(
  textOutput("value"),
  selectInput("year","year",choices = c(2017,2018),selected = 1),
  selectInput("month","month",choices = c(1:12),selected = 1)

)

server <- function( session,input, output) {

  v <- reactiveValues(value=NULL)

  observe({
    year <- input$year
    month <- input$month
    if(nchar(month)==1){
      month <- paste0("0",month)
    }
    v$value <- paste(year,month,sep="-")
  })

  output$value <- renderText({
    v$value
  })
}

shinyApp(ui, server)
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • How to get this value into some variable? – qwww Nov 26 '18 at 09:10
  • that value is in the variable called `Combined()` – Pork Chop Nov 26 '18 at 09:22
  • `Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)` – qwww Nov 26 '18 at 09:24
  • 1
    If you have a look at the ?reactive you can see that its an expression and not to be used as a value, instead you can either use `reactiveVal` or `reactivevalues` – Pork Chop Nov 26 '18 at 09:50
  • I am still getting the same error and my shiny app is closing – qwww Nov 26 '18 at 09:58
  • `Error in v$value : object of type 'closure' is not subsettable` now this error is coming – qwww Nov 26 '18 at 09:59
  • My example works fine, since you dont post the entire app with code I wont speculate on your problem – Pork Chop Nov 26 '18 at 10:10