【问题标题】:error: Object 'output' not found错误:找不到对象“输出”
【发布时间】:2017-10-16 07:21:38
【问题描述】:

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 &lt;- renderPlotly({ : object 'output' not found

我无法纠正错误

【问题讨论】:

  • 你有server &lt;- function(input, output, session){
  • @akrun no 我为 ui 和 server 创建了单独的选项卡,所以不包括这个
  • 你的例子不可复现,所以其他人无法在他们的机器上尝试
  • 我已经复制了这个例子。现在的问题是你没有在任何地方初始化你的 d 数据框......所以仍然有错误
  • 现在我已经添加了数据,你现在可以试试吗@AntoinePissoort

标签: r shiny


【解决方案1】:

我已在名为“app”的文件夹中的单个文件app.R 中复制了您的示例。不要忘记使用library 加载相关包!另外,不要在selectInput 选项中换行,因为这将用于列名。

您对switch() 所做的事情可以通过Shiny 直接处理,方法是从d 中的plot_ly() 中检索输入列的名称。

最后,不要忘记在脚本中导入数据集 (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)

虽然我认为这不是您在 plot_ly 代码中对 add_trace() 的预期输出。

【讨论】:

  • 你能帮我在情节中添加跟踪吗@Antoine Pissoort
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 2015-04-13
  • 2014-08-21
  • 1970-01-01
  • 2017-09-28
  • 2018-06-06
相关资源
最近更新 更多