0

I am doing the following:

  1. using R ShinyUI, get client inputs on ranges of variables A, B, C;
  2. in R ShinyServer, read in a csv file, and using the client inputs to slice the csv, and get the portion that I need;
  3. Perform a loop calculation on the csv, calculate various statistics from the loop output, and plot all these statistics.

Pseudo code:

data = read.csv('file.csv')

shinyServer(function(input, output) {

  data <- reactive({ 
  data = data[data$A<INPUT1 & data$B> INPUT2 & data$C<INPUT3,]
  })

 for (i in 1:dim(data)[1]){
   result1[i] = xxx
   result2[i] = xxx
  }


  output$plot <- renderPlot({
  plot(result1)
  })

 })

The above code does not work. I want to know:

  1. How to correctly incorporate user input and get the variable "data,"
  2. How to plot result1 and result2 from output$plot

Thanks!

Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29
user321694
  • 21
  • 1
  • 1
  • 3
  • I'm not totally clear on your question, but assuming the "INPUT"s are shiny UI elements, you access them with `input$inputname`. To read in a csv you need a `fileInput()` in your UI and something to catch it in server like `data <- read.csv(input$inputname$datapath)`. See here: http://shiny.rstudio.com/gallery/upload-file.html – Rupert Oct 26 '16 at 23:03

1 Answers1

0

The for loop should be inside a the renderPlot, so each time the input$month changes, the reactive data will change and then the for lop will update your variables. If you have the for loop outside a reactive expression, it will be executed only once when the app starts, but after changes in the input.

Below is simple example based on the pseudo code you provide in your original question to illustrate the possible solution.

library(shiny)

ui <- shinyUI( fluidPage(
  fluidRow(
    column(4,
      numericInput("input1", "Speed >", 8),
      numericInput("input2", "Dist >", 15)
    ),
    column(8, 
      plotOutput("plot")
    )
  )
))

server <- shinyServer(function(input, output) {
  dat0 <- cars

  data <- reactive({ 
    dat0[dat0$speed > input$input1 & dat0$dist > input$input2,]
  })

  output$plot <- renderPlot({
    s <- dim(data())[1] 
    result1 <- numeric(s)
    result2 <- numeric(s)
    for (i in 1:s){
     result1[i] <- data()[i, 1]
     result2[i] <- data()[i, 2]
    }
    plot(result1, result2)
  })

})

shinyApp(ui = ui, server = server)
Geovany
  • 5,389
  • 21
  • 37