-1

Reformulating my question, I´m trying to synthetize a data frame reactively, with a selectinput = c("col_1","col_2","col_3","col_4","col_5")

My dataframe looks something like this
Date . Store_ID . Sales . Stock . ETC

I need to be able to sum all the data in the same stores, with the different user selected columns.

Using the mtcars dataframe as an example to work with, my objective is to have a table like this

SelectInput = disp
cyl - disp
4 - sum(every 4 cylinders disp)
6 - sum(every 6 cylinders disp)
8 - sum(every 8 cylinders disp)

SelectInput = qsec
cyl . qsec
4 . sum(every 4 cylinders qsec)
6 . sum(every 6 cylinders qsec)
8 . sum(every 8 cylinders qsec)

library(shiny)

library(tidyverse)

ui <- bootstrapPage(
  selectInput(
    "col",
    "Column",
    colnames(mtcars),
    selected = "mpg"),
  plotOutput("histCentile", height = 200)
)

server <- function(input, output) {
  data <- reactive({
    mtcars() %>%
      group_by(cyl = cyl) %>%
      pull(input$col) %>%
      sum()
  })

  output$histCentile <- renderPlot({
    hist(data()$[[input$col]],
         main = "Graph",
         xlab = "Units",
         xlim = range(data()$[[input$col]]),
         col = '#00DD00',
         border = 'white')
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Rafael
  • 23
  • 5
  • And your question is? – Maurits Evers Jul 26 '18 at 21:48
  • Sorry, just added it – Rafael Jul 26 '18 at 21:50
  • What do you want to do with `StoresGraph`? What is not working? – Maurits Evers Jul 26 '18 at 21:58
  • It is a dataframe I use to create graphs, this datarfame is already subsetted in StoresInBounds(), what I need to do is to Sum the data from a column(which I want the user to be able to choose from), as of right now, it works if I put the name of a specific column in the sum. It stops working when I put the input$GraphI in the sum. I.E. : sum(col_1) - it works, sum(input$GraphI) - it doen´t work – Rafael Jul 26 '18 at 22:05
  • Take a look at the minimal reproducible example I give below; this should put you on track. It's difficult to give specific help without a [fully reproducible code example including sample data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). At the moment you've just given us some disconnected code snippets without much context. Take a look at my answer below to better understand what a "minimal & reproducible code example" is supposed to look like. – Maurits Evers Jul 26 '18 at 22:08
  • I´m sorry, I tried reformulating my question using your same example, I hope it can help a little in understanding where i´m trying to get to – Rafael Jul 26 '18 at 22:37
  • I'm afraid I don't understand at all what you're after. Isn't the code example at the bottom of your post identical to the example from my answer? Or am I missing something? I don't understand what you want to do: `reactive` returns a reactive value. You need this reactive value for what? To do what? You don't give any code to elucidate your goal. In my example below, I show how to use the reactive `data` to update text. You can adjust the code to show a plot instead. Does that not help? – Maurits Evers Jul 26 '18 at 22:48
  • Answering in order, yes it was identical; what I wanted to accomplish was shown in the example above; I'm creating a reactive histogram with this data, I want my graph to be able to change according to the column the user chooses in the Input, I need the data in the different columns to be summed up as I have repeated values in my first column(just as in the cyl colum). – Rafael Jul 26 '18 at 23:02
  • Ok, I've updated my answer to include a second example. Please take a look. Please note that your example still doesn't make a lot of sense, because you're trying to plot a histogram based on a single number (the sum across values for a specific column). – Maurits Evers Jul 27 '18 at 00:38
  • Thank you so much for your help Maurits!, you're a lifesaver, and I'm sorry for not being able to explain myself, i hope my answer helps understand where I was trying to get to. – Rafael Jul 27 '18 at 15:29
  • No problem and you're very welcome @Rafael. Please consider closing this question by upvoting and setting the green check mark next an answer. – Maurits Evers Jul 28 '18 at 02:04

2 Answers2

0

I'm not sure what you're trying to do but here is a minimal reproducible example using reactive to filter data based on the selectInput variable.

library(shiny)
library(tidyverse)

ui <- bootstrapPage(
    selectInput(
        "col",
        "Column",
        colnames(mtcars),
        selected = "mpg"),
    textOutput("selected_col")
)

server <- function(input, output) {
    data <- reactive({
        mtcars %>% pull(input$col) %>% sum()
    })

    output$selected_col <- renderText({
        sprintf("The sum of column %s is %f", input$col, data())
    })
}

# Run the application
shinyApp(ui = ui, server = server)

Explanation: In data we sum the values from input$col based on the selectInput selection. data is therefore a reactive value, which we show in output$selected_col.


Update

There are a few issues with your updated code example:

  1. In the reactive block, you're summarising data to give a single number. Plotting a histogram based on a single number makes no sense. Secondly, there is a typo: it should be mtcars not mtcars(); and lastly, group_by(cyl = cyl) is unnecessary as you don't do any group-wise calculation afterwards (it should also be group_by(cyl)).
  2. You don't actually need a reactive block here at all, since you can do the filtering in renderPlot directly but I guess that is a matter of personal preference.

The following dynamically updates a histogram based on the selected column from selectInput

library(shiny)
library(tidyverse)

ui <- bootstrapPage(
    selectInput(
        "col", 
        "Column", 
        colnames(mtcars),
        selected = "mpg"),
    plotOutput("histo")
)

server <- function(input, output) {
    data <- reactive({
        mtcars %>% pull(input$col)
    })

    output$histo <- renderPlot({
        hist(data())
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

Update - Solution

Thanks to Maurits Evers help, and some research, I managed to do what I wanted to

library(shiny)
library(tidyverse)

ui <- bootstrapPage(
  selectInput(
    "col", 
    "Column", 
    colnames(mtcars),
    selected = "mpg"),
  plotOutput("histo")
)

server <- function(input, output) {
  
  data <- reactive({
    Graphby <- input$col
    with(mtcars,aggregate(qsec,list(cyl=cyl),sum))
    aggregate(mtcars[[Graphby]],list(cyl=mtcars$cyl),sum) 
    
  })
  
  output$histo <- renderPlot({
    hist(data()$x)
  })
}

 

# Run the application 
shinyApp(ui = ui, server = server)

What it does is an interactive histogram by merging groups of data like this in a reactive way, by choosing different columns.

Community
  • 1
  • 1
Rafael
  • 23
  • 5