working with uploaded file in shiny

130 views
Skip to first unread message

Paulo Fernando

unread,
Jun 13, 2013, 10:19:18 AM6/13/13
to shiny-...@googlegroups.com
The function ternaryplot can't use my data inside a .csv file (young, adult, old  20,30,50  40,40,20  10,20,70)

I would like to understand what's happening. 
________________________________________________________________________________________
shinyServer(function(input, output) {
    output$contents <- renderTable({
    
    # input$file1 will be NULL initially. After the user selects and uploads a 
    # file, it will be a data frame with 'name', 'size', 'type', and 'datapath' 
    # columns. The 'datapath' column will contain the local filenames where the 
    # data can be found.
    
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
    read.csv(inFile$datapath, header=input$header)
  })
  
  output$triangularPlot <- renderPlot({
   
    inFile <- input$file1
    

    
    if (is.null(inFile))
      return(NULL)
    
    read.csv(inFile$datapath, header=input$header)
    
    
    # Gera o gráfico com a variável requisitada 
    # Pondera círculos, se selecionado  
    
    ternaryplot(inFile,
                           100,
                           col=cores,
                           bg='white',
                           dimnames_position='edge',
                           grid=T,
                           labels='outside',
                           main='Grafico triangular',
                           prop_size=input$circulos)
})
})
____________________________________________________________________________
#library(shiny)
#library(vcd)
#library(colorspace)
shinyUI(pageWithSidebar(
  headerPanel("Grafico Triangular"),
  sidebarPanel(
    fileInput('file1', 'Escolha o arquivo CSV',
              accept=c('text/csv', 'text/comma-separated-values,text/plain')),
    tags$hr(),
    checkboxInput('header', 'Header', TRUE),
    checkboxInput('circulos', 'Circulos ponderados', FALSE)
    
    ),
  
  mainPanel(
    plotOutput('triangularPlot')
    #tableOutput('contents')
    
  )
))

Thanks

Paulo Fernando

unread,
Jun 13, 2013, 10:47:47 AM6/13/13
to shiny-...@googlegroups.com
I have solved my question reading this comment (link:https://gist.github.com/jcheng5/4050398/#comment-801300)

I am adding a bit of information to this page, since it took me 20 minutes to figure out which is stupid I know, it probably will take someone else it as well.

Most people, like me, will actually want to use the file. When you upload, you do not upload the file but a group of strings, this is what you see when you use the code above. Using the 'datapath' column you are able to use the normal ways to get your data, e.g. read.csv()

Seeing the post linked below made me realize this, using the line:

inFile <- input$files

you get the uploaded data (note this does not contain the file yet) in a data.frame. If you use

dataset<-read.csv(inFile$datapath, header=T, stringsAsFactors =F)

you actually get the data you want in the variable 'dataset'. The variable inFile$datapath contains the path to the file. Now you have the data, play around with it! Using the normal code for the UI you should see your table.

Might be redundant, but since this is one of the first hits on google, I guess it won't hurt.




Reply all
Reply to author
Forward
0 new messages