0

I am working on a dashboard where I am planning on displaying different reports by using the drop down option. If I am getting the input from excel/csv it works fine and data is displaying. But if convert the same excel/csv to data frame and it is not working. Please see the below code.

The below code works fine.


library(DT)
ui <- fluidPage(fluidRow(selectInput (
  "TheFile", "File",
  choices = c("test1.csv", "test2.csv")
)),

mainPanel(DT::dataTableOutput("dataTable")))

server <- function(input, output, session) {
  myCSV <- reactive({
    read.csv(input$TheFile)
  })
  
  output$dataTable <- renderDT(myCSV(),
                               class = "display nowrap compact",
                               filter = "top")
  
}
shinyApp(ui, server)

This code doesn't work

     
      library(shiny) 
     
      library(DT)   
      
     library(readxl)
     
      library(shinyWidgets)
     
      tbl1 <- read.csv('test1.csv')
     
      tbl2 <- read.csv('test2.csv')
     
      ui <- fluidPage(
        fluidRow(
          selectInput("TheFile", "File",choices = c("tbl1","tbl2"))),
     
        mainPanel(
          DT::dataTableOutput("tbl")
        )
      )
     
      server <- function(input, output,session)
      {
     
        myfile <- reactive({data.frame(input$TheFile)
     
        }) 
     
        output$tbl <- DT::renderDT(
          myfile()     
        )
      }shinyApp(ui, server) ```

--------formatting the output -------------

  ``` library(shinydashboard)
  library(shiny) 
  library(DT)  
  library(shinyWidgets)
  library(leaflet)
  library(DBI)
  library(dplyr)
  library(readxl)
  ui <- dashboardPage(
     dashboardHeader(disable = TRUE),
     dashboardSidebar(disable = TRUE),
     dashboardBody(
        fluidPage(
           tags$div(align = "center",style = "color:#3090C7;font-size: 50px;","TITLE"),
           tags$style(HTML("
    .tabbable > .nav > li > a {background-color:#728FCE;  color:black; width: 200PX;} ")),
           
           tabsetPanel(tabPanel("About",                         
                                tags$p(tags$h4("WELCOME PAGE" ))                    
                                
           ),
           
           navbarMenu(" DataTables ",
                      
                      tabPanel("MainTab1",
                               
                               tabsetPanel(
                                  tabPanel("Subtab1",
                                           br(),
                                           tabsetPanel(
                                              tabPanel("TAB1", 
                                                       
                                                       selectInput("FILE", "Select the Report:",choices = c("tbl1","tbl2")),
                                                       
                                                       mainPanel(
                                                          DT::dataTableOutput("table_data")
                                                       )
                                              )
                                              
                                           )
                                           
                                  ),# outside tabpanel
                                  
                                  tabPanel("Subtab2"),
                                  tabPanel("Subtab3")
                               )
                               
                      )
                      
                      
           ),
           
           
           tabPanel("Contact Us")
           
           )
        )
     )
     
  )
  
  server <- function(input, output,session) {
     
     df <-  reactive ({
        switch(input$FILE,"tbl1" = data.frame(lapply(subset(read_excel('test1.xlsx'),select= -c(ROWNUMBER,ORDER_ROWS)),as.character)),
               
               
               "tbl2" = data.frame(lapply(subset(read_excel('test2.xlsx'),select= -c(ROWNUMBER,ORDER_ROWS)),as.character))
        )
        
     })
     
     # output$table_data <-   DT::renderDataTable({
     #    DT::datatable(df,
     #                  rownames = FALSE,
     #                  caption = "TOTAL Sales ",
     #                
     #                  editable = TRUE,
     #                  filter = list(position = "top",clear = FALSE, plain = FALSE),
     #                  extensions = 'Buttons',
     #                  options = list(lengthMenu = c(2, 30, 50), pageLength = nrow(tbl1),# displaying all rows
     #                                
     #                                 scrollY = 1600, scroller = TRUE, scrollX = T,
     #                                 paging = TRUE,
     #                                 searching = TRUE,
     #                                 fixedColumns = TRUE,
     #                                 autoWidth = TRUE,
     #                                 ordering = TRUE,
     #                                 dom = 'Bfrtip',#IF tB displays only few values
     #                                 buttons = c('copy', 'csv', 'excel','print'),
     #                                 modifier = list(page = "current")
     #                                 
     #                  ),
     #                  class = "display nowrap compact" # style
     #    )
     #    
     #    DT::datatable(df,
     #                  rownames = FALSE,
     #                  caption = "TOTAL Orders  ",### title ###
     #                  # filter = "top",#displaying top row fileter with navbar
     #                  editable = TRUE,
     #                  filter = list(position = "top",clear = FALSE, plain = FALSE),
     #                  extensions = 'Buttons',
     #                  options = list(
     #                    lengthMenu = c(2, 30, 50),pageLength = nrow(tbl2),# displaying all rows
     #                    # scrollX = TRUE,
     #                    scrollY = 1600, scroller = TRUE, scrollX = T,
     #                    paging = TRUE,
     #                    searching = TRUE,
     #                    fixedColumns = TRUE,
     #                    autoWidth = TRUE,
     #                    ordering = TRUE,
     #                    dom = 'Bfrtip',#IF tB displays only few values
     #                    buttons = c('copy', 'csv', 'excel','print'),
     #                    modifier = list(page = "current")
     #                    
     #                  ),
     #                  class = "display nowrap compact" # style
     #    )
     #  })
     
     output$table_data <-   DT::renderDataTable({
           df()
     })
   }
  # # run the app
  shinyApp(ui, server) ```


Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Pop
  • 23
  • 4
  • Try to use `get()`, like `get(input$TheFile)` – YBS May 11 '21 at 17:30
  • Hi @peter, I am trying to format my output with all captions, scroll, download buttons. placing the code below. Can you please help me on this. Using the reactive function and switch i was able to switch the outputs. – Pop Jun 04 '21 at 19:33
  • Does this answer your question? [How to call an object with the character variable of the same name](https://stackoverflow.com/questions/9083907/how-to-call-an-object-with-the-character-variable-of-the-same-name) – Ronak Shah Jun 10 '21 at 01:19

0 Answers0