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)

firebelf
  • 5
  • 3
  • my question is similar from yours please help me https://stackoverflow.com/questions/65282962/error-non-numeric-argument-to-binary-operator?fbclid=IwAR3NmSsJz_yXrq_EH8LxHSsAxp21OOWeJSjKp39ILQODpLKbSZR_zJd0RnY – foxtrot Dec 14 '20 at 10:56

1 Answers1

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)
Ash
  • 1,463
  • 1
  • 4
  • 7
  • Thank you, this worked. May I just ask what you mean when you say the file is not validated as an xlsx? – firebelf Mar 16 '20 at 02:38
  • @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. – Ash Mar 16 '20 at 02:45
  • 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 Mar 16 '20 at 02:57
  • @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. – Ash Mar 16 '20 at 20:09
  • Thank you for all the help, it was much appreciated – firebelf Mar 17 '20 at 21:04