firebelf
firebelf

Reputation: 5

R-Shiny: Select input reactive on file input

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)

Upvotes: 0

Views: 2835

Answers (1)

Ash
Ash

Reputation: 1513

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)

Upvotes: 0

Related Questions