Can't acces reactive value outside of reactive consumer.

I got this error.

Listening on http://127.0.0.1:4404
Warning: Error in $: Can't access reactive value 'barcode' outside of reactive consumer.
:information_source: Do you need to wrap inside reactive() or observe()?
53:
Error in input$barcode :
Can't access reactive value 'barcode' outside of reactive consumer.
:information_source: Do you need to wrap inside reactive() or observe()?

I'm trying to get a user input from a textinput. That input should be splitted at the seperator ";". And the second half should create the barcode in following.

So this is my code

library(shinyjs)
library(dplyr)
library(stringr)

# Define UI for application that draws a histogram
ui <- fluidPage(
  useShinyjs(),
  headerPanel(title = "QR - Split and generate"),
  sidebarLayout(
    sidebarPanel(
      textInput("barcode","Enter the Barcode"),
      actionButton("button","GO"),
      imageOutput("svgplot"),
    ),
    
    mainPanel(
      textOutput("barcode_full"),
      textOutput("barcode_first"),
      #textOutput("barcode_second")
      
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  
  observeEvent(input$button,{
    session$sendCustomMessage(type = 'testmessage',
        message = 'Thank you for clicking')
  })
  
  #Split macht probleme
  barcode_first1<-input$barcode
  barcode_first<-strsplit(barcode_first1,";")[[1]]
  #barcode_first1 <- reactive({strsplit(input$barcode,";")[[1:1]]})
  

  
 # QR <- eventReactive(input$barcode, {
  #  qrcode::qr_code(input$barcode)
  #})
  QR<- eventReactive(barcode_first[1],{
    qrcode::qr_code(barcode_first1[1])
  })
  
  output$svgplot <- renderImage({
    txt <- isolate(barcode_first1[1])
    tf <- tempfile(fileext = ".svg")
    qrcode::generate_svg(QR(), tf, size = 100, foreground = "black", background = "white", show = FALSE)
    list(
      src = normalizePath(tf),
      contentType = "image/svg+xml",
      width = 100, height = 100,
      alt = paste("My QR code:", sQuote(txt, FALSE))
    )
  }, deleteFile = TRUE)
  
  
  #outputs
  output$barcode_full <- {(
    renderText(input$barcode)
  )}
  output$barcode_first <- {(
    renderText(barcode_first1[1])
  )}
  output$barcode_second <- {(
    renderText(input$barcode)
  )}
  
}

# Run the application 
shinyApp(ui = ui, server = server)

basically you cant access input$barcode without a reactive context, which is basically the curly braces space within a reactive({}) or observe({}); given that the code seems intended to define some data object, it seems it would be a reactive({}), then later access your reactive as normal via the () syntax.

 barcode_first1<-reactive({req(input$barcode)})
  barcode_first<-reactive({strsplit(req(barcode_first1()),";")[[1]]})
 QR<- eventReactive(req(barcode_first())[1],{
    qrcode::qr_code(req(barcode_first1())[1])
  })

etc.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.