1

I am trying to take the speed variable from the "car" data set which I am uploading to the application. Basically Under select speed: I would like to have all the numbers appearing in the dataset$speed. Under selecInput, the choices should depend upond the data set I am uploading using fileInput. How can I complete this task. For now I have added the choices as 1,2,3. In theory there should be all values of the speed variable of cars data set.

library(shiny)
library(datasets)
##the file I am uploading 
data(cars)
dataset=write.csv(cars, "dataset.csv")


ui=fluidPage( 

actionButton("upload", "Upload File"),
bsModal("uploadFile", " ", "upload", 
sidebarLayout(
sidebarPanel(
fileInput("file","Choose file to upload")

),
mainPanel(
tableOutput("contents")
)
)
),  



sidebarLayout(

sidebarPanel(

column(3, selectInput("selectElement", "Select speed:", c(1,2,3),multiple = 
T, selectize = F)

)
),

mainPanel(
)
)
)

server=function(input,output,session){

output$contents <- renderTable({

inFile <- input$file

if (is.null(inFile))
return(NULL)

read.csv(inFile$datapath)
})
}

shinyApp(ui,server)

1 Answer 1

1

My apologies in advance for a somewhat incomplete response: see below.

First of all, an answer to your query:

If you have a dataset like cars, to identify the "speed" labels you can do:

labls <- unique(cars$speed)
...
selectInput("selectElement", "Select speed:", labls, multiple = 
                          T, selectize = F)

I was hoping to post a complete example, but the current logic (maybe because of the limited code posted?) does not seems right: how can the app a) leave to the user to select which file to use; and at the same time b) already filter for speed? Of course it is possible that you plan to display datasets that have all a column called "speed", then it would make sense :)

Additionally, but this was not part of your question, you appear to use modal dialogues through the package shinyBS.

Since version 0.14 of shiny (around October 2016) shiny has a very good modal function and personally I think it would be better to use the native function instead.

I thougth to post a simple example derived from your code (but with the selectInput for speed commented out because, as mentioned, it does not appear right in the context of the example posted).

library(shiny)
library(datasets) 
data(cars)
dataset = write.csv(cars, "dataset.csv")  

labls <- unique(cars$speed) # I left this in the code

ui=fluidPage( 
  sidebarLayout(
  sidebarPanel(
         actionButton("upload", "Upload File") 
    ),

  mainPanel(tableOutput("contents") )
))
server=function(input,output,session){
# Show modal when button is clicked.
observeEvent(input$upload, {
  showModal(dataModal())
  })
dataModal <- function(failed = FALSE) {
  modalDialog(
    fileInput('inputId', label=NULL, multiple = FALSE, accept = NULL, width =      NULL, buttonLabel = "Browse...", placeholder = "No file selected")
# , selectInput("selectElement", "Select speed:", labls, multiple = 
#                               T, selectize = F)
  )
}  
output$contents <- renderTable({
if (length(input$inputId )== 0) return(NULL) 
    inFile <- input$inputId   
# if (is.null(input$selectElement )) return(NULL)    
input$inputId 
})
}

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

1 Comment

It works for me. I'm using Chrome. Extract from sessionInfo(). Please check if you have same releases (especially shiny). >sessionInfo()R version 3.3.3 (2017-03-06) (...) other attached packages: [1] shiny_1.0.1 loaded via a namespace (and not attached): [1] R6_2.2.0 htmltools_0.3.5 tools_3.3.3 Rcpp_0.12.10 [5] jsonlite_1.4 digest_0.6.12 xtable_1.8-2 httpuv_1.3.3 [9] mime_0.5

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.