I am trying to build an app that lets the user to upload an excel file, choose a worksheet and some data (i.e column) and make graphs.
I have prepared a dummy app to show my probelms. I have two of them...
- The update of the input for the worksheet name is not working. Althogh the input field is updated by
updateSelectInputit always rewrite to the default value (I think there is a nesting issues but I try to solve it for more then two days, without any luck...) - I don't know how to set the input of the worksheet to be able to receive any name later, now it always jumps back to the first WS (If I set
choices = c('')orchoices = 1it gives an error that there is no such WS)
library(shiny)
library(xlsx)
library(readxl)
#++++++++++++++++++++++++
# create dummy excel
first <- data.frame(ID_1 = 1:5, a1 = letters[1:5], a2 = sample(1:10, 5))
second <- data.frame(ID_1 = 1:10, b1 = letters[6:15], b2 = sample(1:30, 10),b3 = sample(1:30, 10))
third <- data.frame(ID_1 = 1:8, b1 = letters[6:13], b2 = sample(5:30, 8), b3 = sample(5:30, 8))
write.xlsx(first, file = "dummy.xlsx", sheetName = "first", row.names = FALSE, append = FALSE)
write.xlsx(second, file = "dummy.xlsx", sheetName = "second", row.names = FALSE, append = TRUE)
write.xlsx(third, file = "dummy.xlsx", sheetName = "third", row.names = FALSE, append = TRUE)
#++++++++++++++++++++++++
not_sel="not selected"
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("xls_input","choose file",accept=c(".xlsx")),
selectInput("ws_var", "choose WS", choices = c("second")),
selectInput("data_var","choose cloumn", c(not_sel)),
actionButton("run_button","cacluate",icon=icon("play")),
),
mainPanel(
textOutput("calc")
)
)
)
server <- function(input, output){
xdata <- reactive({
req(input$xls_input)
infile<-input$xls_input
observeEvent(input$ws_var,{
choices_ws <- excel_sheets(path = infile$datapath)
updateSelectInput(inputId = "ws_var", choices = choices_ws)
})
read_excel(infile$datapath,input$ws_var)
})
observeEvent(xdata(),{
choices <- names(xdata())
updateSelectInput(inputId = "data_var", choices = choices)
})
output$calc <- eventReactive(input$run_button,{
xdata_<-xdata()
xdata_var_<-input$data_var
calc_data <- sum(xdata_[[xdata_var_]])
})
}
shinyApp(ui = ui, server = server)