csv - Update on File Upload Shiny R -
i trying add file upload feature shiny app. ideally, want user able upload csv file , select variable want use dropdown menu. when app starts, have dataset preloaded , dropdown displays correct variable names dataset. however, when try upload different csv file, cannot seem dropdown update display variables contained in uploaded csv. here's code:
server.r
library(shiny) library(ggplot2) infile <- mtcars shinyserver(function(input, output) { tabledata <- reactive({ infile <<- input$file1 if (!is.null(infile)){ read.csv(infile$datapath, header=input$header, sep=input$sep, quote=input$quote) } else { infile <<- mtcars } }) #for x values output$opt.x <- renderui({ selectinput("xcolumn", "x column plot", names(infile)) }) }) ui.r
library(shiny) shinyui(fluidpage( titlepanel("app"), sidebarlayout( sidebarpanel( fileinput('file1', 'choose csv file', accept = c( '.csv', '.tsv') ), uioutput("opt.x") ), mainpanel() )) i've tried placing output$opt.x assignment inside reactive function reads in file, didn't seem help.
hi don't pass variable global environnement, use shiny reactive feature instead, server should :
library(shiny) library(ggplot2) # test data # write.csv(x = iris, file = "iris.csv", row.names = false) # write.csv(x = data.frame(v1 = 1:10, v2 = rnorm(10)), file = "test.csv", row.names = false) shinyserver(function(input, output) { tabledata <- reactive({ infile <- input$file1 if (!is.null(infile)){ read.csv(infile$datapath) } else { mtcars } }) #for x values output$opt.x <- renderui({ selectinput("xcolumn", "x column plot", names(tabledata())) }) output$your_data <- rendertable({ head(tabledata()) }) }) and replace main panel :
mainpanel( tableoutput("your_data") ) i remove params in read.csv since input not defined in ui.
edit
try app, it's not shiny solution jquery/js 1 (the difference in ui) (see this answer) :
library("shiny") ui <- fluidpage( titlepanel("app"), sidebarlayout( sidebarpanel( fileinput('file1', 'choose csv file', accept = c('.csv','.tsv') ), tags$script( '$( "#file1" ).on( "click", function() { this.value = null; });' ), uioutput("opt.x") ), mainpanel( tableoutput("your_data") ) ) ) server <- function(input, output) { tabledata <- reactive({ infile <- input$file1 if (!is.null(infile)){ read.csv(infile$datapath) } else { mtcars } }) #for x values output$opt.x <- renderui({ selectinput("xcolumn", "x column plot", names(tabledata())) }) output$your_data <- rendertable({ head(tabledata()) }) } shinyapp(ui = ui, server = server) for testing used :
write.csv(x = data.frame(v1 = 1:10, v2 = rnorm(10)), file = "test.csv", row.names = false) write.csv(x = data.frame(v11 = 11:20, v12 = rnorm(10), abc = letters[1:10]), file = "test.csv", row.names = false)
Comments
Post a Comment