r - Shiny error message "undefined column selected" appears after loading data but quickly disappears -
i trying create simple shiny app can load data csv file , display ggplot best fit line.
i have file uploader widget , when upload file, such csv, red warning message briefly pops saying (i think... disappears i'm not 100% sure read correctly) "undefined column selected in dataframe".
it disappears , plot appears. app working how i'd work, avoid brief warning appearing.
i know due renderplot function (at bottom of server.r file). i'm not sure change prevent error occurring. commented out bunch of failed attempts make problem go away.
the shiny app hosted here , this csv file i've been using test it.
my server.r code:
library(shiny) library(ggplot2) # define server logic required draw 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 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 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 match selected y variable observeevent(input$y, { updateselectizeinput(session,"yname", label = 'vertical axis variable name', choices = input$y, selected = input$y) }) #update x , y variables... had same problem when used # input$x , input$y inside of renderplot x <- reactive({input$x}) y <- reactive({ input$y }) #names axes , 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 slider input number of bins sidebarlayout( sidebarpanel( # input: select 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 display ---- radiobuttons("disp", "display", choices = c(head = "head", = "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 plot of generated distribution mainpanel( plotoutput("plot"), tableoutput("contents") ) ) ))
thanks in advance trying help.
Comments
Post a Comment