How to manipulate reactive expressions like a normal dataframe?

1,233 views
Skip to first unread message

YC

unread,
May 29, 2017, 1:29:51 PM5/29/17
to Shiny - Web Framework for R
Hi guys,

I'm quite new with using Shiny. The code below is actually part of an Rmarkdown file with Shiny coding in it.

I have an issue in handling reactive expressions within Shiny. Basically after calling a reactive expression, I can render or plot the expressions. What I cannot easily do is to manipulate that data after calling it.

I've managed to render and output load_table(). This just tells it to load the csv file on a button click. See code below:

```{r UI inputs}
wellPanel(
  fileInput("dataset", label = "Select a .csv File to upload",
            accept=c("text/csv",
                     "text/comma-separated-values,text/plain",
                     ".csv")),

actionButton(inputId = "loadbutton", label = "Load Data")
)

dataTableOutput("df")

```


```{r Server Functions}
load_table <- eventReactive(input$loadbutton, {
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$dataset

    if (is.null(inFile)){
      return(NULL)}
    else {
      read.csv(inFile$datapath)}
    })

output$df <- renderDataTable({
  load_table()
})
```

However, once the user selects and loads the data - I can't seem to manipulate it because the reactive expressions doesn't work like a dataframe. What I want to do next is to create filters so my users can sift through load_table(). But R never seems to treat it like a normal table. And I can't get the table without using a reactive function.

I know there are solutions out there on StackExchange, and there are issues similar to mine but I can never get it to really work.


Zac

Joe Cheng

unread,
Jun 1, 2017, 4:29:55 AM6/1/17
to YC, Shiny - Web Framework for R
Write another reactive expression that reads load_table() and performs the various filtering operations on it, and returns the resulting data frame. For example, if input$columns had the names of the columns to show, you'd do

filtered <- reactive({
  df <- load_table()

  # Show only selected columns
  if (!is.null(input$columns)) {
    stopifnot(all(input$columns %in% names(df)))
    df <- df[,input$columns]
  }

  # apply other filters...

  df
})


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/1881ae49-a0c4-41f4-a054-422d93bd9d42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages