ggplot2 - How to Specify Columns when a user chooses a file to upload in R? -


i writing r file prompts user upload file , plots data in file user uploads. not know how reference columns (i trying use ggplot2) in code.

the data user upload csv file like, can vary:

        january february march april may burgers    4       5       3     5    2 

i stuck @ ggplot2 part need reference column names.

server.r

library(shiny) library(datasets) library(ggplot2)  x <- read.csv(file.choose())   # define server logic required summarize , view selected dataset shinyserver(function(input, output) {     # generate summary of dataset   output$summary <- renderprint({     dataset <- x     summary(dataset)   })    # show first "n" observations   output$view <- rendertable({     head(x, n = input$obs)   })    # create line plot (i took https://gist.github.com/pssguy/4171750)   output$plot <- reactiveplot(function() {       print(ggplot(x, aes(x=date,y=count,group=name,colour=name))+               geom_line()+ylab("")+xlab("") +theme_bw() +                theme(legend.position="top",legend.title=element_blank(),legend.text = element_text(colour="blue", size = 14, face = "bold")))    }) }) 

ui.r

library(shiny)  # define ui dataset viewer application shinyui(pagewithsidebar(    # application title   headerpanel("sample proj"),    # sidebar controls select dataset , specify number   # of observations view   sidebarpanel(     numericinput("obs", "number of observations view:", 10)    ),    # show summary of dataset , html table requested   # number of observations   mainpanel(     tabsetpanel(       tabpanel("table", tableoutput("view")),       tabpanel("linegraph", plotoutput("plot"))     )   ) )) 

here's working example. took code , modified column numbers can passed ui.r inputs. (i use diamonds dataset in ggplot2 dataframe.)

note have created couple of reactive functions in server.r.

server.r

library(shiny) library(datasets) library(ggplot2)  #x <- read.csv(file.choose()) x <- diamonds  # define server logic required summarize , view selected dataset shinyserver(function(input, output) {    createplot <- function(df, colx, coly) {     x <- names(df)[colx]      y <- names(df)[coly]      ggplot(data=df, aes_string(x = x, y = y) ) + geom_line()   }    y <- reactive({     x   })    # generate summary of dataset   output$summary <- renderprint({     dataset <- x     summary(dataset)   })    # show first "n" observations   output$view <- rendertable({     head(x, n = input$obs)   })    # create line plot (i took https://gist.github.com/pssguy/4171750)   output$plot <- reactiveplot(function() {     df <- y()     print(createplot(df, colx=input$xa, coly=input$ya))   }) }) 

ui.r

library(shiny)  # define ui dataset viewer application shinyui(pagewithsidebar(    # application title   headerpanel("sample proj"),    # sidebar controls select dataset , specify number   # of observations view   sidebarpanel(     numericinput("obs", "number of observations view:", 10)     ,numericinput("xa", "column plot x-axis:", 5)     ,numericinput("ya", "column plot y-axis:", 6)    ),    # show summary of dataset , html table requested   # number of observations   mainpanel(     tabsetpanel(       tabpanel("table", tableoutput("view")),       tabpanel("linegraph", plotoutput("plot"))     )   ) )) 

as separate suggestion, first shiny app working static dataframe, try file.choose() option variable data frames.

hope helps move forward.

updated based on @joran's comment:

my original response using column number inside ggplot's aes environment=environment() argument added. have modified createplot function in server.r use aes_string instead.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -