问题
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<-(fluidPage(
titlePanel("Auto Fill"),
sidebarPanel(
autoFillDF<- fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")),
# actionButton("go", "update"),
selectizeInput("p1", choices = autoFillDF$WorklistNo, selected = NULL, label = 'WorklistNo'),
selectizeInput("p2", choices = NULL, label = 'Status'),
selectizeInput("p3", choices = NULL, label = 'Plant'),
),
mainPanel(
DT::dataTableOutput('table')
)
)
)
server<-(function(input, output, session) {
updateApp <- reactive({
data <- autoFillDF
data <- data[data$WorklistNo %in% input$p1,]
updateSelectizeInput(session, 'p2', choices = data$Status, selected = data$Status, server = TRUE)
updateSelectizeInput(session, 'p3', choices = data$Plant, selected = data$Plant, server = TRUE)
data
})
shinyApp(ui = ui, server = server)
output$table <- DT::renderDataTable(
DT::datatable(updateApp())
)
})
shinyApp(ui = ui, server = server)
This is my data set look like. I have to read all the column data. I want to put WorklistNo and rest should be autofill.
Thanks in advance.
回答1:
I have tried to understand your code and requirement and as per my understanding i have tried to build a simple solution to complete it...Please take a look on that..
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))
{
df<<-read.csv(input$file_upload$datapath,
header = TRUE)
values$upload_state <- 'uploaded'
df
}
})
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 = 'WLID')
}
else
{
data_upload_fun()
selectInput("p1", choices = df$WLID, label = 'WLID')
}
})
output$p2_ui<-renderUI({
if(is.null(values$upload_state)|| values$upload_state=='reset')
{
selectInput("p2", choices = NULL, label = 'PLANT')
}
else
{
data_upload_fun()
plant<-df[df$WLID==input$p1,2]
selectInput("p2", choices = as.list(plant), label = 'PLANT')
}
})
output$p3_ui<-renderUI({
if(is.null(values$upload_state)|| values$upload_state=='reset')
{
selectInput("p3", choices = NULL, label = 'STATUS')
}
else
{
data_upload_fun()
status<-df[df$WLID==input$p1 & df$PLANT==input$p2,3]
selectInput("p3", choices = as.list(status), label = 'STATUS')
}
})
output$selected_data_table<-renderDT({
if(is.null(values$upload_state)|| values$upload_state=='reset')
{
returnValue()
}
else
{
data_upload_fun()
data_to_show<-df[df$WLID==input$p1 & df$PLANT==input$p2 & df$STATUS== input$p3, ]
DT::datatable(data_to_show)
}
})
}
shinyApp(ui, server)
UI LOOKS LIKE:
DATA Looks like
Please let me know if that works and anything to change...
来源:https://stackoverflow.com/questions/56863566/how-to-do-autofill-of-data-in-shiny-app-using-excel-file