How to do autofill of data in shiny app using excel file using fileInput

I am trying to read a data set using the browse option to upload data from pc. After that, I want to display file content to be automatically filled by using first column entry.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      fileInput("file_upload","Uplaod Data",buttonLabel = "Browse..",placeholder = "No file Selected")
    ),
    fluidRow(
      column(3,
             uiOutput("p1_ui")
      ),
      column(3,
             uiOutput("p2_ui")
      ),
      column(3,
             uiOutput("p3_ui")
      )
    ),
    fluidRow(
      column(6,
             h3("Uploaded DATA"),
             DTOutput("uploaded_data_table")
      ),
      column(6,
             h3("Selected DATA"),
             DTOutput("selected_data_table")
      )
    )
  )
)

server <- function(input, output,session) {
  values <- reactiveValues(
    upload_state = NULL
  )
  data_upload_fun<-eventReactive(input$file_upload,{
    req(input$file_upload)
    if(values$upload_state=='reset'||is.null(values$upload_state))
    {
      data_in<-read_xlsx(input$file_upload$datapath
                    )
      values$upload_state <- 'uploaded'
      data_in
    }  
  })
  output$uploaded_data_table <- renderDT({
    DT::datatable(data_upload_fun())
  })
  output$p1_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p1", choices = NULL, label = 'WorklistNo')
   } 
    else
    {
      data_upload_fun()
      selectInput("p1", choices = uploade_data$WorklistNo, label = 'WorklistNo')
    }
  })

  output$p2_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p2", choices = NULL, label = 'Status')
    }
    else
    {
      data_upload_fun()
      status<-data_in[data_in$WorklistNo==input$p1,2]
      selectInput("p2", choices = as.list(status), label = 'Status')
    }
  })
  output$p3_ui<-renderUI({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      selectInput("p3", choices = NULL, label = 'Plant')
    }
    else
    {
      data_upload_fun()
      plant<-data_in[data_in$WorklistNo==input$p1 & data_in$Status==input$p2,3]
      selectInput("p3", choices = as.list(plant), label = 'Plant')
    }
  })

  output$selected_data_table<-renderDT({
    if(is.null(values$upload_state)|| values$upload_state=='reset')
    {
      returnValue()
    }
    else
    {
      data_upload_fun()
      data_to_show<-data_in[data_in$WorklistNo==input$p1 & data_in$Status==input$p2 & data_in$Plant== input$p3, ]
      DT::datatable(data_to_show)
    }
  })

}

shinyApp(ui, server)

Hi there! Happy to look into the issue, but I'm having trouble getting the code running. The first issue was:

Error in DTOutput("uploaded_data_table") :
could not find function "DTOutput"

Do you mind restarting R ("Session > Restart R" in RStudio) and then trying to run the app? It should throw errors until you have all the necessary library() calls (I suspect read_xlsx will need one, too).

Also, I'll confess that I'm not an Excel user so I don't have an Excel file handy. If you don't mind posting a sample Excel file that's causing these problems for you, I'd be happy to try it out.

Thanks for your reply.
I found answer for this...!!

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.