Shiny error message “undefined column selected” appears after loading data but quickly disappears

时光怂恿深爱的人放手 提交于 2019-12-11 14:31:58

问题


I am trying to create a simple shiny app that can load data from a csv file and display a ggplot with a best fit line.

I have a file uploader widget and when I upload a file, such as a csv, a red warning message briefly pops up saying (I think... it disappears so quickly I'm not 100% sure I read it correctly) "undefined column selected in dataframe".

It then disappears and a plot appears. So my app is working almost exactly how I'd like it to work, I would just like to avoid the brief warning from appearing.

I know it is due to my renderPlot function (at the bottom of my server.R file). I'm just not sure what to change to prevent the error from occurring. I commented out a bunch of failed attempts to make this problem go away.

The shiny app is hosted here and this is the csv file I've been using to test it.

My server.R code:

library(shiny)
library(ggplot2)
# Define server logic required to draw a histogram
shinyServer(function(input, output,session) {

data <- reactive({
  req(input$file)

  df <- read.csv(input$file$datapath,
                 header = input$header,
                 sep = input$sep,
                 quote = input$quote)

  if(input$disp == "head") {
    return(head(df))
  }
  else {
    return(df)
  }
  })



output$contents <- renderTable({
  req(data())
  data()

})

#UPDATE SELECT OPTIONS TO MATCH DATAFRAME COLUMN NAMES
observeEvent(input$file, {
  vars <- colnames(data())

  updateSelectInput(session, "x", 
                    label = "Horizontal Axis Variable Column", 
                    choices = vars, selected = vars[1])

  updateSelectizeInput(session, "xname", 
                       label = 'Horizontal Axis Variable Name', 
                       choices = vars,selected = vars[1],
                       options = list(create = TRUE),server = TRUE)

  updateSelectInput(session, "y", 
                    label = "Vertical Axis Variable Column", 
                    choices = vars, selected = vars[2])

  updateSelectizeInput(session, "yname", 
                       label = 'Vertical Axis Variable Name', 
                       choices = vars,selected = vars[2],
                       options = list(create = TRUE),server = TRUE)
})

#Update x variable name to match selected x variable.
observeEvent(input$x, {
  updateSelectizeInput(session,"xname", 
                       label = 'Horizontal Axis Variable Name',
                       choices = input$x, selected = input$x)
})

#update y variable name to match selected y variable
observeEvent(input$y, {
  updateSelectizeInput(session,"yname", 
                       label = 'Vertical Axis Variable Name',
                       choices = input$y, selected = input$y)
})

#update x and y variables... had same problem when I used 
# input$x and input$y inside of renderPlot
x <- reactive({input$x})
y <- reactive({
  input$y
})

#names for axes and title labels
xname <- reactive({input$xname})
yname <- reactive({input$yname})
title <- reactive({input$title})


output$plot <- renderPlot({
  if (is.null(x()) | is.null(y())) {
    return()
  }

# req(data())
# req(ncol(data() >= 2))
# req(x())
# req(y())

  ggplot(data = data(), aes(x = data()[,x()], y = data()[,y()])) +
    geom_point() + 
    xlab(xname()) +
    ylab(yname()) +
    ggtitle(title()) +
    if(input$options == "Display Best Fit Line"){
      geom_smooth(method = 'lm', se = FALSE, formula = y ~ x)
    }
})
})

my ui.R script:

library(shiny)

shinyUI(fluidPage(

# Application title
titlePanel("Shiny Graphing App"),

# Sidebar with a slider input for number of bins 
sidebarLayout(
  sidebarPanel(
    # Input: Select a file ----
    fileInput("file", "Choose CSV File",
              multiple = TRUE,
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",
                         ".csv")),

    # Horizontal line ----
    tags$hr(),

    # Input: Checkbox if file has header ----
    checkboxInput("header", "Header", TRUE),

    # Input: Select separator ----
    radioButtons("sep", "Separator",
                 choices = c(Comma = ",",
                             Semicolon = ";",
                             Tab = "\t"),
                 selected = ","),

    # Input: Select quotes ----
    radioButtons("quote", "Quote",
                 choices = c(None = "",
                             "Double Quote" = '"',
                             "Single Quote" = "'"),
                 selected = ""),

    # Horizontal line ----
    tags$hr(),

    # Input: Select number of rows to display ----
    radioButtons("disp", "Display",
                 choices = c(Head = "head",
                             All = "all"),
                 selected = "head"),
    textInput(inputId = "title", label = "Plot Title", value = ""),
    selectInput(inputId = "x", 
                label = "Horizontal Axis Variable Column", choices = c()),
    selectInput(inputId = "y", 
                label = "Vertical Axis Variable Column", 
                choices = c()),
    selectizeInput(inputId = 'xname', 
                   label = 'Horizontal Axis Variable Name', 
                   choices = c(),
                   options = list(create = TRUE)),
    selectizeInput(inputId = 'yname', 
                   label = 'Vertical Axis Variable Name', 
                   choices = c(),
                   options = list(create = TRUE)),
    radioButtons(inputId = 'options', 
                 label = 'Options', 
                 choices = c("Display Best Fit Line","Display Points Only"))


  ),

  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("plot"),
    tableOutput("contents")

  )
)
))

Thanks in advance for trying to help.

来源:https://stackoverflow.com/questions/45685364/shiny-error-message-undefined-column-selected-appears-after-loading-data-but-q

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!