【问题标题】:getting object not found error in shiny在闪亮中获取对象未找到错误
【发布时间】:2016-04-05 21:10:10
【问题描述】:
library(shiny)
library(shinydashboard)
library(leaflet)
library(data.table)
library(ggplot2)
library(usl)

ui <- pageWithSidebar(
  headerPanel("CSV Viewer"),
  sidebarPanel(
    fileInput('file1', 'Choose CSV File',
              accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
    tags$hr(),
    checkboxInput('header', 'Header', TRUE),
    fluidRow(
      column(6,radioButtons("xaxisGrp","X-Axis:", c("1"="1","2"="2"))),
      column(6,checkboxGroupInput("yaxisGrp","Y-axis:", c("1"="1","2"="2")))
    ),
    radioButtons('sep', 'Separator',
                 c(Comma=',', Semicolon=';',Tab='\t'), ','),
    radioButtons('quote', 'Quote',
                 c(None='','Double Quote'='"','Single Quote'="'"),'"'),
    uiOutput("choose_columns")
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Data", tableOutput('contents')),
      tabPanel("Plot",plotOutput("plot")),
      tabPanel("Summary",uiOutput("summary"))

    )
  )
)


####server

server <- function(input, output,session) {
  dsnames <- c()
  u<-
  data_set <- reactive({
    inFile <- input$file1
    data(specsdm91)
    if (is.null(inFile))
      return(specsdm91)

    data_set<-read.csv(inFile$datapath, header=input$header, 
                       sep=input$sep, quote=input$quote)
  })

  output$contents <- renderTable({data_set()})

  observe({
    dsnames <- names(data_set())
    cb_options <- list()
    cb_options[ dsnames] <- dsnames
    updateRadioButtons(session, "xaxisGrp",
                       label = "X-Axis",
                       choices = cb_options,
                       selected = "")
    updateCheckboxGroupInput(session, "yaxisGrp",
                             label = "Y-Axis",
                             choices = cb_options,
                             selected = "")
  })
  output$choose_dataset <- renderUI({
    selectInput("dataset", "Data set", as.list(data_sets))
  })

  usl.model <- reactive({

      df <- data_set()
     # print(df)
      df2 <- df[,c(input$xaxisGrp, input$yaxisGrp)]

      #gp <- NULL
      if (!is.null(df)){

        xv <- input$xaxisGrp
        yv <- input$yaxisGrp
        print(xv)
        print(yv)
        if (!is.null(xv) & !is.null(yv)){

          if (sum(xv %in% names(df))>0){ # supress error when changing files

            usl.model <- usl(as.formula(paste(yv, '~', xv)), data = df)
           return(usl.model())

          }
        }
      }
      #return(gp)
    }
  )


  ##plot
  output$plot = renderPlot({

   plot(usl.model())

  } )

  ##
 # output$summary <- renderUI({

  #  summary(usl.model())

  #}) 

  ##

  output$choose_columns <- renderUI({

    if(is.null(input$dataset))
      return()
    colnames <- names(contents)
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  }) 
}
shinyApp(ui, server)

如您所见,我打印了 df。有什么想法吗?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    编辑:请给出一个可重现的数据示例。没有它,很难想象数据类型是什么。总的来说,我认为你可能有两个问题:

    1. 什么是gp?您需要返回的不是gp,而是usl.model
    2. 您的 renderUI 没有传递 UI 对象(即,输出函数不知道如何处理)。

    您的问题是usl.model 实际上并没有保存在任何地方,因为它在renderPlot 中被调用,它只返回情节。由于您希望 usl.model 被两个函数使用,您应该采用以下方法之一。

    方法一:

    usl.model 定义一个反应函数,并在您的两个输出函数中引用它。

    usl.model <- reactive({
        # some calculations probably ending in
        usl.model <- usl(as.formula(paste(yv, '~', xv)), data = df)
        usl.model
    })
    

    这允许您将模型输出引用为usl.model()括号很重要!),例如

    output$plot <- renderPlot( plot(usl.model(), add=TRUE) )
    

    方法2

    创建一个 reactiveValues() 变量来存储您在绘图函数中计算的 usl.model

    usl.model <- reactiveValues(data = NULL)
    
    output$plot = renderPlot({
        # some calculations probably ending in
        usl.model$data <- usl(as.formula(paste(yv, '~', xv)), data = df)
    })
    

    然后您可以在任何地方引用您的模型输出为usl.model$data

    方法二可能更糟糕,因为它需要先运行绘图函数。

    【讨论】:

    • 我已完成您的建议,但仍有问题。我已经用函数和错误更新了原始帖子。
    • 请给出一个可重现的例子。没有它,很难想象数据类型是什么。总的来说,我认为您可能有两个问题: 1. gp 是什么?您需要返回的不是gp,而是usl.model 2. 您的renderUI 没有传递UI 对象(即输出函数不知道如何处理)。
    • 我只是更新了整个代码。现在我返回usl.model,得到:警告:错误:嵌套太深:无限递归/选项(表达式=)?没有可用的堆栈跟踪
    • 您已经通过在函数本身的定义中引用函数 usl.model() 来诱导递归!但是,您的对象存储在变量usl.model 中,没有括号。请去掉括号。但是您是否考虑过如果代码未包含在 if 状态中会返回什么?
    • 可能是renderTextrenderPrint
    猜你喜欢
    • 1970-01-01
    • 2019-12-21
    • 2015-04-13
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多