performing operations after uploading a CSV file in shiny [R] -
i have made upload button via uploading .csv file[dataset] in shiny.
dataframe <- data.frame(a=c(2,3,4,5,6,7,3,7,8,9,2),b=c(3,7,8,9,2,1,2,3,4,5,6),c=c(1,1,1,2,2,1,1,1,1,2,2))
in ui.r :
fileinput('datafile', 'choose csv file', accept=c('csv', 'comma-separated-values','.csv')), actionbutton("uploaddata", "upload"),
i want perform operations such "a+b" , "a-b" on dataset , add new column in dataset of actionbutton.
problem 1: tried use tableoutput() function display data in shinyapp displaying data dataframe in r console instead.
in server.r:
data <- dataframe %>% group_by(c) %>% mutate(a) %>% mutate(b) %>% mutate(add = (a+b)) %>% mutate(sub = (a-b))
problem 2: want use data input making ggplots of (add, sub)
you this:
ui.r
library(shiny) shinyui(fluidpage( fileinput('datafile', 'choose csv file', accept=c('csv', 'comma-separated-values','.csv')), tableoutput('table'), plotoutput('plot') ))
server.r
library(shiny) library(ggplot2) shinyserver(function(input, output,session) { dataframe<-reactive({ if (is.null(input$datafile)) return(null) data<-read.csv(input$datafile$datapath) data<- data %>% group_by(c) %>% mutate(a) %>% mutate(b) %>% mutate(add = (a+b)) %>% mutate(sub = (a-b)) data }) output$table <- rendertable({ dataframe() }) output$plot <- renderplot({ if(!is.null(dataframe())) ggplot(dataframe(),aes(x=x,y=add))+geom_point() }) })
i removed action button because fileinput
load file directly once user has chosen it. in server.r
, loaded dataframe
stored in reactive value, can call dataframe()
when want make plot/render table dataframe.
you might want code check csv file has , b column etc.
Comments
Post a Comment