闪亮的错误消息"选择了未定义的列"加载数据后出现但很快消失

时间:2017-08-15 01:38:44

标签: r file ggplot2 upload shiny

我正在尝试创建一个简单的闪亮应用程序,可以从csv文件加载数据并显示具有最佳拟合线的ggplot。

我有一个文件上传器小部件,当我上传一个文件,比如csv时,会弹出一条红色的警告信息(我想......它消失得太快了,我并不是100%肯定我读过它正确)"在数据框中选择了未定义的列"。

然后它消失并出现一个情节。所以我的应用程序几乎完全按照我的工作方式工作,我只想避免出现短暂的警告。

我知道这是由于我的renderPlot函数(在我的server.R文件的底部)。我只是不确定要改变什么来防止错误发生。我注意到一系列失败的尝试让这个问题消失了。

闪亮的应用已托管herethis是我用来测试它的csv文件。

我的服务器.R代码:

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

我的ui.R脚本:

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

  )
)
))

提前感谢您尝试提供帮助。

0 个答案:

没有答案