0

I am very new to Shiny and am not sure if I am doing this remotely correct/completely oversimplified. I am trying to pull the column headers from an excel fileInput into a selectInput drop down box. So essentially I would like the options for the select box be determined by the headers of the file input. Then it would link into my equation in the server, which would perform the calculation based on the dataset in the column (the bit in the server with input$col). I appreciate any comments/answers, Thanks

EDIT: at a guess, would I need to use uiOutput and renderUI??

ui

 ui <- fluidPage(theme = shinytheme(),

setBackgroundColor("white"),

titlePanel(img(src = "image.png", height = 125, width = 450)),

(h1("review app", style = "color:#337ab7")),
p("Calculate"),

headerPanel(h3("Input data here", style = "color:#337ab7")), 


sidebarLayout(
sidebarPanel( position =c("left"),  style = "color:#337ab7", 
    numericInput("SL",
                "SL", 1, min=1, max=10),

    numericInput("LT", "LT",0, min=0, max = 52),
    fileInput("file1", 'choose file',
              accept = c(".xlsx") ),
    selectInput("col", "Column", choices = unique(colnames(input$file1)
                                                   )),

   checkboxInput("smooth", "Clean my data", value = FALSE, width = NULL),

    actionButton("action_Calc", label = "Refresh & Calculate", icon("redo"), 
         style="color: #fff; background-color: #337ab7; border-color: #2e6da4"), 
     ),


mainPanel(
    tabsetPanel(
      tabPanel("SS", h1(textOutput("SS"), style = "color:#337ab7")),
      tabPanel("guide",  img(src = "guide.png", height = 200, width = 600)),
      tabPanel("Mydata", div(tableOutput('contents'), style="font-size:55%"))
          ))))

server

 server <- function(input, output) {


  Data <- reactive({
  req(input$file1)
  inFile <- input$file1
  read_excel(inFile$datapath, 1)
})

output$contents <- renderTable(bordered = TRUE, style= "border-color:#337ab7", hover = TRUE, {
  Data()
})


values<- reactiveValues()
observe({
    input$action_Calc
    values$int<- isolate({ if (input$smooth) (round( input$SL*sqrt(input$LT/4)*sd( tsclean(Data()[[input$col]], 
       replace.missing = TRUE, lambda = NULL)) , digits= 2))
       else (round( input$SL*sqrt(input$LT/4)*sd(Data()[[input$col]]), digits = 2)) })})

    output$SS <- renderText({paste("Calculated is", values$int)} )

} shinyApp(ui, server)

1

1 Answer 1

0

updatedSelectInput should do it for you. Below is a minimal example.

To reduce package dependencies I switched to loading .csv rather than .xlsx. Note that the loaded file isn't validated, so if junk goes in you'll get junk out.

library(shiny)

#UI
ui <- fluidPage(

    selectInput('mydropdown', label = 'Select', choices = 'No choices here yet'),

    fileInput('myfileinput', label = 'Select File', accept = c(".csv"))

)

#Server
server <- function(input, output, session) {

    observeEvent(input$myfileinput, {

        mytable <- read.csv(input$myfileinput$datapath)

        updateSelectInput(session, "mydropdown", label = "Select", choices = colnames(mytable))

    })

}

shinyApp(ui = ui, server = server)
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, this worked. May I just ask what you mean when you say the file is not validated as an xlsx?
@firebelf Two different points. The first is that my example reads .csv rather than .xlsx, it makes it a bit simpler. The second is that my example doesn't check that the file is any good, for example it doesn't check if the file has any headers.
Ah I see, .csv is a very simple fix- will implement that now. However, how would you recommend I look into the quality of the data being input? I assume probably a multi stage check of the imported file (missing data, no headers, outliers etc?)?
@firebelf Checking for missing data, no headers, and outliers sounds like a good idea. Feel free to create another question if you would like further help with that.
Thank you for all the help, it was much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.