0

I'm trying to create a dashboard in Shiny where the user can select a month/year and perform some forecasting analysis on that time period. At this point, I have something that runs and I'm able to bring in a reactive dataset using a fileInput() call. The problem I'm having is subsetting the data by a selected part number. So I bring in the .csv with incidentDate, partNumber, and demand (columns of data) and now I want to subset the data using a selected part number (using textInput as my reactive value).

data <- reactive({
  file1 <- input$file
  if(is.null(file1)){return()} 
  f = read.csv(file=file1$datapath, header = TRUE, stringsAsFactors = FALSE)
})

The code above is how I read in the .csv from my computer. That dataset is now a reactive dataset that I can list as a table using data().

output$table <- renderTable({
  if(is.null(data())){return()}
  "how do I call the f from the above reactive function here?"
})

If I use a textInput box using "part" as my pointer to what the user inputs, how do I subset the data() to only include rows that match that part number? In regular R, I would just call data$partNumber = '7800123489' and it would subset, but I'm not sure how to do it with a reactive dataset that I brought in using Shiny.

Matt C
  • 51
  • 4
  • What have you tried that's failed? Depending on what you need, you could name the object you read in and then creating a subset using the textInput box via `input$name_of_textInput` in the same `reactive` function call. Or just make a new object based on the subset of `data()` based on `input$name_of_textInput`... – aosmith Sep 15 '15 at 18:49
  • I haven't tried much because I really don't know where to go from here. I see what you mean though. I created the text input in my main panel `textInput("part", "Enter Part Number")`, which would then be `input$part`...but I'm still confused as to how I subset the data using that call? If I wanted to do it inside of the `reactive` function, where do I call `input$part`? – Matt C Sep 15 '15 at 19:12
  • `datasetname[datasetname$part == input$part,]`? – aosmith Sep 15 '15 at 19:15
  • In my case, the datasetname for my reactive .csv input is `data' and if I add `data[data$partNumber == input$part] to the end of the function above, I get the error `object of type 'closure' is not subsettable`. I see where you are going with the logic though. – Matt C Sep 15 '15 at 19:43
  • If you are working within the reactive function in your OP, then you haven't give your dataset a name... So name it when you read it in, `dat = read.csv(...`. While not identical to your problem, [this answer](http://stackoverflow.com/a/20569512/2461552) shows a basic example of doing multiple steps with a dataset within a reactive function. – aosmith Sep 15 '15 at 20:43
  • Thanks @aosmith. I've read through that example a few times, and I can't seem to apply it to what I'm doing. I added a comment to the second block of code above as I'm having trouble calling the dataset `f = read.csv(...` in future output tables. – Matt C Sep 16 '15 at 15:08

1 Answers1

1

I figured this out with the help of another post and @aosmith, so thanks to all for the help.

Once I read in the .csv, I subset it in the reactive function and then call data() in my output table. I can type a part number into the Part textInput and then once I select the file to read in, the table is filled with only values that include that part number.

IN UI SECTION

mainPanel(
  textInput("part", "Enter Part Number"),
  uiOutput("tb")
)

IN SERVER SECTION

server <- function(input, output) {
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read <- read.csv(file=file1$datapath, header = TRUE, stringsAsFactors = FALSE)
    f <- subset(read, partNumber %in% input$partNumber)
    return(f)
})

 output$table <- renderTable({
    if(is.null(data())){return()}
    data()
})

So the key to this code is reading in the file, then creating a subset f using the response from textInput using %in% input$part and returning f to data().

Community
  • 1
  • 1
Matt C
  • 51
  • 4