2017-10-16 87 views
0

Data錯誤:對象「輸出」未找到

收到錯誤:

output object not found 

不知道爲什麼會這樣,我已經做了過去的儀表盤同樣的東西那個時候沒有出現錯誤。 輸出對象指定,但它仍然不能檢測

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") 
     )))) 

收到錯誤:Error in output$plot1 <- renderPlotly({ : object 'output' not found

我不是能夠糾正錯誤

+1

你有'服務器< - 功能(輸入,輸出,會話){' – akrun

+0

@akrun沒有我創建單獨的標籤爲UI和服務器,所以不包括這 –

+0

那麼我該如何解決這個問題? –

回答

0

我已將您的示例複製到名爲「app」的文件夾內的單個文件app.R中。不要忘記使用library加載相關軟件包!此外,不要在selectInput選項中斷行,因爲這將用於列名稱。

您使用switch()所做的事情可以通過從dplot_ly()中檢索輸入列的名稱直接通過Shiny處理。

最後,不要忘記在腳本中導入數據集(d)。

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

雖然我不認爲這是你從add_trace()plot_ly代碼預期輸出。

+0

你能幫我添加跟蹤積極@Antoine Pissoort –