错误:对象'输出'找不到

时间:2017-10-16 07:21:39

标签: r shiny

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 个答案:

答案 0 :(得分:0)

我已将您的示例复制到名为&#34; app&#34;的文件夹中的单个文件app.R中。不要忘记使用library加载相关的包!此外,请勿在{{1​​}}选项中断行,因为这将用于列名称。

通过selectInput中的switch()检索输入列的名称,可以直接通过Shiny直接处理您使用d进行的操作。

最后,不要忘记在脚本中导入数据集(plot_ly())。

d

enter image description here

虽然我不认为这是您在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) 代码中add_trace()所期望的输出。