Reactive Value Problem

Hello, please I encounter this error when I run my shiny code.

Warning: Error in : Can't access reactive value 'rad' outside of reactive consumer.
i Do you need to wrap inside reactive() or observer()?
  55: <Anonymous>
Error : Can't access reactive value 'rad' outside of reactive consumer.
i Do you need to wrap inside reactive() or observer()?

I am building an app that calls the Google trend api. I have specified my input objects and wrapped them in reactive() functions. However, I keep getting the error message above when I run my app. Below is the reprex code of the app:


library(shiny)
library(shinyalert)
library(shinythemes)
library(shinycssloaders)
library(shinyjs)
library(gtrendsR)
library(plotly)
library(networkD3)
library(dplyr)
library(ggplot2)

options(spinner.color = "lightblue",
        spinner.color.background = "#ffffff",
        spinner.size = 2)

# Define UI for application that queries google trends

ui <- list(useShinyjs(),navbarPage(windowTitle = "TrendChecker",
                                   title = strong("TrendChecker"),theme = shinytheme(theme = "spacelab"),
                                   tabPanel(title = strong("Trend Over Time"),icon = icon("chart-line"),
                                            sidebarLayout(
                                                sidebarPanel(width = 3,h4(strong("Controls")),hr(),
                                                             textInput("text",strong("Enter Search Term")),
                                                             checkboxGroupInput("check",strong("Select Country(ies)"),choices = c("USA","UK","Germany","Netherlands","Japan"),selected = "USA"),
                                                             radioButtons("rad",strong("Choose Trend Source"),choices = c("Web","News","YouTube","Images"),selected = "News"),
                                                             radioButtons("time",strong("Select Time Frame"),choices = c("Last Hour","Last Four Hours","Last Day","Last Seven Days","Past 30 Days","Past 90 Days","Past 12 Months","Last Five Years"),selected = "Last Seven Days"),
                                                             actionButton("run",strong("Run Query"),icon("caret-right"))
                                                             ),
                                                mainPanel(actionButton("info",strong("About App",icon("info"))),hr(),
                                                          hidden(tags$div(id = "about",h5("The TrendChecker is a web application that enables users to monitor the search popularity of any subject of interest over time,
                                                                                          and across different countries by calling the Google trend api. Search hit of 100 is the indicator of optimum popularity, while other hits are measured relative to the optimum."))),
                                                          withSpinner(plotlyOutput("plot"),type = 8))
                                            ))
    
))

# Define server logic required to run query

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

## APP info button toggle activation
    
observeEvent(input$info,{
    toggle("about")
})    

    
## Create input switch functionality
    

radio_input <- switch(input$rad,
                      "Web" = "web",
                      "News" = "news",
                      "YouTube" = "youtube",
                      "Images" = "images")

radio_time <- switch(input$time,
                     "Last Hour" = "now 1-H",
                     "Last Four Hours" = "now 4-H",
                     "Last Day" = "now 1-d",
                     "Last Seven Days" = "now 7-d",
                     "Past 30 Days" = "today 1-m",
                     "Past 90 Days" = "today 3-m",
                     "Past 12 Months" = "today 12-m",
                     "Last Five Years" = "today+5-y")


## Convert inputs into reactive inputs
    
text_input <- reactive(input$text)
check_input <- reactive(input$check)
radio_input <- reactive(radio_input)
radio_time <- reactive(radio_time)


## Write the function

trend <- function(){
    
    gtrends(keyword = c(text_input()),geo = c(check_input()),gprop = radio_input(),
            time = radio_time())
}
    
## Convert to reactive function

trend2 <- reactive(trend())


## Create interactive plot

output$plot <- renderPlotly({
    input$run
    isolate(ggplotly(trend2()))
})
       
}



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

Thanks in anticipation of your help.

the above would not be two seperate steps.

  text_input <- reactive(input$text)
  check_input <- reactive(input$check)
  radio_input <- reactive(switch(input$rad,
                                 "Web" = "web",
                                 "News" = "news",
                                 "YouTube" = "youtube",
                                 "Images" = "images"))
  radio_time <- reactive(switch(input$time,
                                "Last Hour" = "now 1-H",
                                "Last Four Hours" = "now 4-H",
                                "Last Day" = "now 1-d",
                                "Last Seven Days" = "now 7-d",
                                "Past 30 Days" = "today 1-m",
                                "Past 90 Days" = "today 3-m",
                                "Past 12 Months" = "today 12-m",
                                "Last Five Years" = "today+5-y"))
1 Like

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.