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.