简体   繁体   中英

unable to build a model using a shiny application

I am trying to create a shiny app where user will upload a csv file and based on user picked columns, shiny app will create model, show the plot of the model and summary. It looks like, when I pick the columns xv and yv values are not populated from the data frame to build the model. I realize that I have to provide a minimal example but I am very new to shiny and really dont know where the problem may be. The following is the whole code, any help is greatly appreciated.

This is the code:

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

======

Error on the console:

Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning: Error in plot.window: need finite 'xlim' values
Stack trace (innermost first):
    81: plot.window
    80: localWindow
    79: plot.default
    78: plot
    77: plot
    76: renderPlot [C:\shiny\file/ui.R#98]
    68: output$plot
     1: shiny::runApp

该错误是由于:在usl.model ,您返回了gp ,但是您应该返回usl.model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM