SHINY APP with two .csv files in same page/panel


#1

I have a requirement to read 2 separate .CSV files in a single shiny page where i need to get the column data as per the selected column from drop down.
Using below code i managed to developed the R code as per the requirement , but it is only for one .csv file, but i need to have the second .csv file as well in the same page.

Tried using Fluidpage and fluidrow i didn’t managed to get the desired output .
Could you please help me in this.

Thanks in advance.

below is the code:
library(shiny)

ui <- fluidPage(
h2(‘The uploaded file data’),
dataTableOutput(‘mytable’),
fileInput(‘file’, ‘Choose info-file to upload’,
accept = c(
‘text/csv’,
‘text/comma-separated-values’,
‘text/tab-separated-values’,
‘text/plain’,
’.csv’,
’.tsv’
)
),

Taken from: http://shiny.rstudio.com/gallery/file-upload.html

tags$hr(),
checkboxInput(‘header’, ‘Header’, TRUE),
radioButtons(‘sep’, ‘Separator’,
c(Comma=’,’,
Semicolon=’;’,
Tab=’\t’),
’,’),
radioButtons(‘quote’, ‘Quote’,
c(None=’’,
‘Double Quote’=’"’,
‘Single Quote’="’"),
’"’),
actionButton(“choice”, “incorporate external information”),

selectInput(“columns”, “Select Columns”, choices = NULL), # no choices before uploading

column(dataTableOutput(“table_display”),width=4)
)

server <- function(input, output, session) { # added session for updateSelectInput

info <- eventReactive(input$choice, {
inFile <- input$file
# Instead # if (is.null(inFile)) … use "req"
req(inFile)

# Changes in read.table 
f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
vars <- names(f)
# Update select input immediately after clicking on the action button. 
updateSelectInput(session, "columns","Select Columns", choices = vars)

f

})

output$table_display <- renderDataTable({
f <- info()
f <- subset(f, select = input$columns) #subsetting takes place here
f
})
}
shinyApp(ui, server)

Result =

expected output sholuld be like ==


#2

To get your input in side by side, you can put them into separate columns. The UI for such code is below, you would just need to fill in any server logic.

ui <- fluidPage(
    
    fluidRow(
        column(width = 6,
               h2('The uploaded file data'),
               
               dataTableOutput('mytable1'),
               
               fileInput('file1', 'Choose info-file to upload',
                         accept = c(
                             'text/csv',
                             'text/comma-separated-values',
                             'text/tab-separated-values',
                             'text/plain',
                             '.csv',
                             '.tsv'
                         )
               )
        ),
        column(width = 6,
               h2('The uploaded file data'),
               
               dataTableOutput('mytable2'),
               
               fileInput('file2', 'Choose info-file to upload',
                         accept = c(
                             'text/csv',
                             'text/comma-separated-values',
                             'text/tab-separated-values',
                             'text/plain',
                             '.csv',
                             '.tsv'
                         )
               )
        )
    )
)

Result:


#3

I am getting below error while running the R code:
could you please help me in fixing the code please ?
Thanks in advance

library(shiny)

ui <- fluidPage(

  • fluidRow(
  • column(width = 6,
    
  •    h2('The uploaded file data'),
    
  • dataTableOutput(‘mytable1’),
  • fileInput(‘file1’, ‘Choose info-file to upload’,
  •         accept = c(
    
  •           'text/csv',
    
  •           'text/comma-separated-values',
    
  •           'text/tab-separated-values',
    
  •           'text/plain',
    
  •           '.csv',
    
  •           '.tsv'
    
  •         )
    
  •  )
    
  • ),
  • Taken from: http://shiny.rstudio.com/gallery/file-upload.html

  • tags$hr(),
  • checkboxInput(‘header’, ‘Header’, TRUE),
  • radioButtons(‘sep’, ‘Separator’,
  •            c(Comma=',',
    
  •              Semicolon=';',
    
  •              Tab='\t'),
    
  •            ','),
    
  • radioButtons(‘quote’, ‘Quote’,
  •            c(None='',
    
  •              'Double Quote'='"',
    
  •              'Single Quote'="'"),
    
  •            '"'),
    
  • actionButton(“choice”, “incorporate external information”),
  • selectInput(“columns”, “Select Columns”, choices = NULL), # no choices before uploading
  • column(dataTableOutput(“table_display1”),width=4)
  • )
  • server <- function(input, output, session) {
    Error: unexpected symbol in:

    server”

info <- eventReactive(input$choice, {

  • inFile <- input$file1
    
  • # Instead # if (is.null(inFile)) ... use "req"
    
  • req(inFile)
    
  • # Changes in read.table 
    
  • f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
    
  • vars <- names(f)
    
  • # Update select input immediately after clicking on the action button. 
    
  • updateSelectInput(session, "columns","Select Columns", choices = vars)
    
  • f
    
  • })

output$table_display1 <- renderDataTable({

  • f <- info()
    
  • f <- subset(f, select = input$columns) #subsetting takes place here
    
  • f
    
  • })
    Error in output$table_display1 <- renderDataTable({ :
    object ‘output’ not found

}
Error: unexpected ‘}’ in "}"
shinyApp(ui, server)
Error in shinyApp(ui, server) : object ‘server’ not found


#4

You need to be careful with your parentheses and curly braces. The code you provided were missing some to close the UI fluidPage function and the sever function. The code below fixes these issues.

Also, try to put all your code into one code block so it is easier for everyone to read.

library(shiny)

ui <- fluidPage(
  fluidRow(
    column(width = 6,
           h2('The uploaded file data'),
           dataTableOutput('mytable1'),
           fileInput('file1', 'Choose info-file to upload',
                     accept = c(
                       'text/csv',
                       'text/comma-separated-values',
                       'text/tab-separated-values',
                       'text/plain',
                       '.csv',
                       '.tsv'
                     )
           ),
           tags$hr(),
           checkboxInput('header', 'Header', TRUE),
           radioButtons('sep', 'Separator',
                        c(Comma=',',
                          Semicolon=';',
                          Tab='\t'),
                        ','),
           radioButtons('quote', 'Quote',
                        c(None='',
                          'Double Quote'='"',
                          'Single Quote'="'"),
                        '"'),
           actionButton("choice", "incorporate external information"),
           selectInput("columns", "Select Columns", choices = NULL), # no choices before uploading
           column(dataTableOutput("table_display1"),width=4)
    )
  )
)

server <- function(input, output, session) {
  
  info <- eventReactive(input$choice, {
    inFile <- input$file1
    # Instead # if (is.null(inFile)) ... use "req"
    req(inFile)
    # Changes in read.table 
    f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
    vars <- names(f)
    # Update select input immediately after clicking on the action button. 
    updateSelectInput(session, "columns","Select Columns", choices = vars)
    f
  })
  
  output$table_display1 <- renderDataTable({
    f <- info()
    f <- subset(f, select = input$columns) #subsetting takes place here
    f
  })
}

shinyApp(ui, server)

#5

sure i will be bit careful from now and thank you so much for your help Kentm


#6

Hi Kentm,
I managed to develop the code as per my requirement , but only place i got stuck is 2 .csv outputs are displaying one below the other . But it supposed to be side by side.

Please find the code below and please help me out how to overcome this problem with Shiny App.

Thanks in advance.

library(shiny)
ui <- fluidPage(
fluidRow(
column(width = 6,
h2(‘The uploaded file data’),
dataTableOutput(‘mytable1’),
fileInput(‘file1’, ‘Choose info-file to upload’,
accept = c(
‘text/csv’,
‘text/comma-separated-values’,
‘text/tab-separated-values’,
‘text/plain’,
’.csv’,
’.tsv’
)
)
),
column(width = 6,
h2(‘The uploaded file data’),
dataTableOutput(‘mytable2’),
fileInput(‘file2’, ‘Choose info-file to upload’,
accept = c(
‘text/csv’,
‘text/comma-separated-values’,
‘text/tab-separated-values’,
‘text/plain’,
’.csv’,
’.tsv’
)
)
),
column(width=6,checkboxInput(‘header’, ‘Header’, TRUE)),
column(width=6,checkboxInput(‘header’, ‘Header’, TRUE)),
column(width=6,radioButtons(‘sep’, ‘Separator’,
c(Comma=’,’,
Semicolon=’;’,
Tab=’\t’),
’,’)),
column(width=6,radioButtons(‘sep’, ‘Separator’,
c(Comma=’,’,
Semicolon=’;’,
Tab=’\t’),
’,’)),
column(width=6,radioButtons(‘quote’, ‘Quote’,
c(None=’’,
‘Double Quote’=’"’,
‘Single Quote’="’"),
’"’)),
column(width=6,radioButtons(‘quote’, ‘Quote’,
c(None=’’,
‘Double Quote’=’"’,
‘Single Quote’="’"),
’"’)),
column(width=6,actionButton(“choice”, “incorporate external information1”)),
column(width=6,actionButton(“choice2”, “incorporate external information2”)),
column(width=6,selectInput(“columns”, “Select Columns”, choices = NULL)),
column(width=6,selectInput(“columns2”, “Select Columns”, choices = NULL)),
fluidRow(
column(width=3,div(style = “height:15px”),dataTableOutput(“table_display1”))
),
fluidRow(
column(width=3,div(style = “height:15px”),dataTableOutput(“table_display2”))
)
)
)
server <- function(input, output, session) {
info <- eventReactive(input$choice, {
inFile <- input$file1
req(inFile)
f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
vars <- names(f)
updateSelectInput(session, “columns”,“Select Columns”, choices = vars)
f
})
info2 <- eventReactive(input$choice2, {
inFile2 <- input$file2
req(inFile2)
f2 <- read.table(inFile2$datapath, header = input$header, sep = input$sep, quote = input$quote)
vars <- names(f2)
updateSelectInput(session, “columns2”,“Select Columns”, choices = vars)
f2
})

output$table_display1 <- renderDataTable({
f <- info()
f <- subset(f, select = input$columns)
head(f)
})
output$table_display2 <- renderDataTable({
f2 <- info2()
f2 <- subset(f2, select = input$columns2)
head(f2)
})
}
shinyApp(ui, server)

ACTUAL OUTPUT:

EXPECTED should be side by side but i am not getting desired output with the code i shared above .


#7

Items that you want in the same row need to be placed in the same fluidRow() call. In the code you provided you have the following:

fluidRow(
    column(width=3,div(style = “height:15px”),dataTableOutput(“table_display1”))
),
fluidRow(
    column(width=3,div(style = “height:15px”),dataTableOutput(“table_display2”))
)

which will place the tables in two rows (i.e. one under the other). Also in your code, near the top, you have:

fluidRow(
    column(width = 6,
           h2(‘The uploaded file data’),
           dataTableOutput(‘mytable1’),
           fileInput(‘file1’, ‘Choose info-file to upload’,
                      accept = c(
                          ‘text/csv’,
                            ‘text/comma-separated-values’,
                            ‘text/tab-separated-values’,
                            ‘text/plain’,
                            ’.csv’,
                            ’.tsv’
                      )
           )
    ),
    column(width = 6,
           h2(‘The uploaded file data’),
           dataTableOutput(‘mytable2’),
           fileInput(‘file2’, ‘Choose info-file to upload’,
                      accept = c(
                          ‘text/csv’,
                            ‘text/comma-separated-values’,
                            ‘text/tab-separated-values’,
                            ‘text/plain’,
                            ’.csv’,
                            ’.tsv’
                      )
           )
    )
)

This is the code that is actually placing the items in two columns in the same row. Both columns are in the same fluidRow() call. I would recommend putting what you want into each of those column() calls. For example, you can change the dataTableOutput(‘mytable1’) to dataTableOutput(‘table_display1’) since that is the table being created in your server code.