1
votes

Data

getting error:

output object not found 

don't know why this is happening ,i have done the same stuff on past dashboard that time no error occurred. Output object is specified ,but still it cant detect

Server.R

d <-read_excel("data/ds.xlsx",sheet = 1)
output$plot1 <- renderPlotly({
data <- switch(input$var, 
           "Beginning Stocks (1000 MT)"= d$`Beginning Stocks (1000 MT)`
           ,"Area Harvested (1000 HA)" = d$`Area Harvested (1000 HA)`
           ,"Yield (MT/HA)" = d$`Yield (MT/HA)`
           ,"Production (1000 MT)" = d$`Production (1000 MT)`
           ,"MY Imports (1000 MT)" =d$`MY Imports (1000 MT)`

        )
   data1 <- switch(input$var1,
                 "Beginning Stocks (1000 MT)"= d$`Beginning Stocks (1000 
                 MT)`
              ,"Area Harvested (1000 HA)" = d$`Area Harvested (1000 HA)`
              ,"Yield (MT/HA)" = d$`Yield (MT/HA)`
              ,"Production (1000 MT)" = d$`Production (1000 MT)`
              ,"MY Imports (1000 MT)" =d$`MY Imports (1000 MT)`
                          )
      plot_ly(d, x =~d$`Attributes (Unit)`, y = ~data,type= "scatter",mode = 
      'markers+lines',  marker = list(size = 10),name=input$var) %>%
      add_trace(x = ~d$`Attributes (Unit)`, y = ~data1,type="scatter"
      ,mode="markers+lines",name=input$var1)
         })

UI.R

   navbarPage("Barley Dashboard", 
       tabPanel("Balance sheet",
                fluidPage(theme = shinytheme("united"),
          mainPanel(fluidRow( column(8,selectInput("var",selected="select",
          label = "Choose first variable",                                        
         choices = c( "Beginning Stocks (1000 MT)","Area Harvested (1000 
         HA)","Yield (MT/HA)","Production (1000 MT)","MY Imports (1000 
         MT)")
                            )
                            ),
        column(8,selectInput("var1", selected = "select",
         label = "Choose second variable",choices = c("Beginning Stocks 
         (1000 MT)", "Area Harvested (1000 HA)","Yield (MT/HA)","Production 
           (1000 MT)","MY Imports (1000 MT)")
                            )
                            )
                            ),
           plotlyOutput("plot1")
         ) ) ) )

getting error :Error in output$plot1 <- renderPlotly({ : object 'output' not found

I am not able to rectify the error

1
Do u have server <- function(input, output, session){ - akrun
@akrun no i created separate tabs for ui and server ,so not included this - Rishabh Kanojia
Your example is not reproducible, so others can't try it on their machine - akrun
I have reproduced the example. The problem now is that you didn't initiale your d dataframe anywhere... So there's still error from that - Antoine Pissoort
now i have added the data ,can you try it now @AntoinePissoort - Rishabh Kanojia

1 Answers

1
votes

I have reproduced your example in a single file app.R inside a folder named "app". Do not forget to load the relevant packages with library! Also, don't break lines in selectInput choices because this will be used for the column names.

The things you are doing with the switch()'s can be directly handled through Shiny by retrieving the input column's names from d in plot_ly().

Finally, do not forget to import your dataset (d) in your script.

library(shinythemes)
library(plotly)

ui <- navbarPage("Barley Dashboard", 
           tabPanel("Balance sheet",
                    fluidPage(theme = shinytheme("united"),
                              mainPanel(fluidRow( 
                                column(8,
                                       selectInput("var",
                                                   selected="select",
                                                   label = "Choose first variable",                      
                                                   choices = c( "Beginning Stocks (1000 MT)",
                                                                "Area Harvested (1000 HA)","Yield (MT/HA)",
                                                                "Production (1000 MT)","MY Imports (1000 MT)"))
                              ),
                              column(8,selectInput("var1", selected = "select",
                                                   label = "Choose second variable",
                                                   choices = c("Beginning Stocks (1000 MT)",
                                                               "Area Harvested (1000 HA)","Yield (MT/HA)",
                                                               "Production (1000 MT)","MY Imports (1000 MT)")))
                              ),
                              plotlyOutput("plot1")
                              ) ) 
                    ) )

library("openxlsx")
d <- read.xlsx("ds.xlsx", check.names = F)
# Handle the changes of the colnames with dots instead of spaces due to read.xlsx()
names(d) <- gsub(x = names(d), pattern = "\\.", replacement = " ")

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

  output$plot1 <- renderPlotly({
  #browser()
  plot_ly(d, x =~d$`Attributes (Unit)`, y = ~d[,input$var],
          type= "scatter",mode = 'markers+lines',  
          marker = list(size = 10),name=input$var) %>%
    add_trace(x = ~d$`Attributes (Unit)`, y = ~d[,input$var1],type="scatter",
              mode="markers+lines", name=input$var1,yaxis="y2")
})
}

shinyApp(ui=ui, server = server)

enter image description here

Although I don't think this is the output you expected from add_trace() in your plot_ly code.