Reactive error Shiny

I keep getting this error

Error in .getReactiveEnvironment()$currentContext() :
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.)

SHINY

library(shiny)
library("shinyWidgets")

# Define UI for application that draws a histogram
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Index of Consumer Prices in Europe"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput("companiex","Select Company" , 
                  choices = c("AAPL",'MSFT','INTC', 'CSCO','AMZN','GOOGL', 'FB')), 
      
      checkboxInput("selectsecondcompany",label = "Select another company?"),
      conditionalPanel("input.selectsecondcompany == true",
                       selectInput("secondcompany","Select second company:",c("AAPL",'MSFT','INTC', 'CSCO','TSM','AMZN','GOOGL', 'FB','INTC','NFLX','ADBE','NVDA','TMUS','TSLA')))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("overall", height = "350px", hover=hoverOpts(id = "plot_hover", delayType = "debounce",clip=TRUE, nullOutside = FALSE)),
      textOutput('hover_info')      
      
    )
  )  
)    


)



SERVER

library(pdfetch)
library(shiny)
library(tidyverse)
library(rsdmx)
library(zoo)
library(data.table)
library(plotly)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  
  tickers<-c("AAPL",'MSFT','INTC', 'CSCO','AMZN','GOOGL', 'FB','TSLA')
  close_values<-pdfetch_YAHOO(tickers,
                              
                              fields = c("close"),
                              
                              
                              from = as.Date("2012-05-18"),
                              
                              to = Sys.Date(), interval = "1d")
  
  df2<-data.frame(date=as.Date(index(close_values)), close_values)
  
  p <- ggplot(close_values, aes(x = as.Date(index(close_values)), y = close_values[,input$companiex])) +
    geom_line(alpha = 0.4) 
    ggplotly(p, tooltip = c("close"))
  
  output$hover_info <- renderText({
    cat('Values')
    print(paste(as.Date(input$plot_hover$x),input$plot_hover$y))
  })  
  
  output$overall <- renderPlot(p)
  
  
})

Notice that this expression:

p <- ggplot(close_values, aes(x = as.Date(index(close_values)), y = close_values[,input$companiex])) +
geom_line(alpha = 0.4)
ggplotly(p, tooltip = c("close"))

Depends on the reactive value input$companiex, so it must be read in a reactive context (e.g., renderPlot(), renderText(), reactive(), observe(), etc). In other words, you need to move that code inside renderPlotly():

output$overall <- renderPlot({
  p <- ggplot(close_values, aes(x = as.Date(index(close_values)), y = close_values[,input$companiex])) +
geom_line(alpha = 0.4)
  ggplotly(p, tooltip = c("close"))
})
1 Like

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