Error in as.vector(x,"character")

I'm getting this error, myabe someone know whats the problem.

Warning: Error in as.vector: cannot coerce type 'closure' to vector of type 'character'

    barcode_first1<-reactive({req(input$barcode)})
    barcode_first<-strsplit(as.character(barcode_first1),";")[[1]]
    barcode_first2<-barcode_first1[1]
  })```

There are two problems: barcode_first1 is a reactive, you would need to call it with parentheses, as you would a function.

But there is another problem: req() is used to check that an input is correct, but see the documentation, you never use the result of req()!

This should work as expected:

library(shiny)

# for testing in interactive session
reactiveConsole(TRUE)

# or use input
barcode <- reactive("RTYUIKJHGBV;2345678765")

# This is only to test availability of input
req(barcode)

# Now you can use the input directly!
barcode_first<-strsplit(barcode(),";")[[1]]
barcode_first
#> [1] "RTYUIKJHGBV" "2345678765"

barcode_first2<-barcode()[1]
barcode_first2
#> [1] "RTYUIKJHGBV;2345678765"

# Or maybe you wanted to do that?
barcode_first3 <- reactive(strsplit(barcode(),";")[[1]])
barcode_first3()
#> [1] "RTYUIKJHGBV" "2345678765"
barcode_first4 <- barcode_first3()[[1]]
barcode_first4
#> [1] "RTYUIKJHGBV"

Created on 2022-07-24 by the reprex package (v2.0.1)

No it's not working. I'm getting now the error that it could not find function barcode_first

Just to Add if it's necessary that is my whole 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,{
    barcode<-reactive("ERVERBERG;43tgebef")
    req(barcode)
    barcode_first<-strsplit(barcode(),";"[[1]])
    barcode_first2<-barcode_first()[[1]]
  })
  
  #Split macht probleme
  
  
  #barcode_first1 <- reactive({strsplit(input$barcode,";")[[1:1]]})
  
  
  
  # QR <- eventReactive(input$barcode, {
  #  qrcode::qr_code(input$barcode)
  #})
  QR<- eventReactive(req(barcode_first2()),{
    qrcode::qr_code(req(barcode_first2()))
  })
  
  output$svgplot <- renderImage({
    txt <- isolate(barcode_first2)
    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_first2)
  )}
  output$barcode_second <- {(
    renderText(input$barcode)
  )}
  
}

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

I hope you can learn from this

  1. reactive() should be at the highest scope level of your server code, and not nested within other constructs
  2. observe/observeEvents are for sideeffects, not producing data, if you want to produce data use reactive/eventReactive
  3. you have complete control over the names of what you make, so try to avoid names like barcode_first2 ... meaningful names are preferred. (I used the names barcode_split,barcode_first, barcode_second, but I dont use barcode_first2 etc. )

p.s. I added some styling just for the fun of it .

library(shiny)
library(tidyverse)
library(qrcode)

# Define UI for application that draws a histogram
ui <- fluidPage(
  headerPanel(title = "QR - Split and generate"),
  sidebarLayout(
    sidebarPanel(
      textInput("barcode","Enter the Barcode",
                value = "ERVERBERG;43tgebef"),
      actionButton("button","GO")
    ),
    
    mainPanel(
      tags$style( HTML(" .mycontainer > div {border-style:groove;
                       width:15em;height:12em;padding:1em;}")),
      div(style="display:flex;gap:7em;",
          class="mycontainer",
      div(tags$label(tags$u("Full")),
          textOutput("barcode_full")),
      div(tags$label(tags$u("First")),
          textOutput("barcode_first"),
          imageOutput("svgplot_1")),
      div(tags$label(tags$u("Second")),
          textOutput("barcode_second"),
          imageOutput("svgplot_2"))
      )
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  
  barcode_split <- eventReactive(input$button,{
    stringr::str_split(input$barcode,pattern = ";",simplify = TRUE)
  })
  
  barcode_first <- reactive({
    pluck(req(barcode_split()) ,
          1)
  })
  
  barcode_second <- reactive({
    pluck(req(barcode_split()) ,
          2)
  })

  
  QR_first <- eventReactive(barcode_first(),{
    qrcode::qr_code(barcode_first())
  })
  
  
  QR_second<- eventReactive(barcode_second(),{
    qrcode::qr_code(barcode_second())
  })
  
  rendfunc <- function(txt,qr){
    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))
    )
  }
  
  output$svgplot_1 <- renderImage({

    qr <- req(QR_first())
    txt <- shiny::isolate(req(barcode_first()))

    rendfunc(txt,qr)
  
  }, deleteFile = TRUE)
  
  output$svgplot_2 <- renderImage({
    qr <- req(QR_second())
    txt <- shiny::isolate(req(barcode_second()))

    rendfunc(txt,qr)
    
  }, deleteFile = TRUE)
  
  #outputs
  output$barcode_full <- {(
    renderText(input$barcode)
  )}
  output$barcode_first <- {(
    renderText(req(barcode_first()))
  )}
  output$barcode_second <- {(
    renderText(req(barcode_second()))
  )}
  
}

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

Wow thank you very much. looks very nice i should be able to work with it :slight_smile:

This topic was automatically closed 7 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.