0
votes

I have a question about my shiny app, which is 2 level responsive. At the first level the user selects a value on the slider, which than creates a plot. The plot is reactive again, since you can select a portion of the points with a brush which is then represented in a table below. The problem is, that the selected values, based on the calculation with the slider value does not show. Here is the code I am trying:

server <- function(input,output, session) {

  library(ggplot2) 
  library(DT) 

  output$plot <- renderPlot({
    ggplot(diamonds, aes(price, carat*input$w1)) + geom_point()
  })

  diam <- reactive({

    user_brush <- input$user_brush
    mysel <- brushedPoints(diamonds, user_brush)
    return(mysel)

  })

  output$table <- DT::renderDataTable(DT::datatable(diam()))
}

ui <-   fluidPage(
  sliderInput(inputId = "w1",
              label = "weight on carat",
              value = 5, min = 0, max = 40.0),
  plotOutput("plot", brush = "user_brush"),
  dataTableOutput("table")
)

shinyApp(ui = ui, server = server)

I suspect the problem in the server part. Namely the calculation carat*input$w1 alters the underlying data. Shiny gets lost and can not identify the data to be displayed in the output of the table.

General ideas so far: I need to implement a new data.frame with a variable for carat*input$w1 and refer to this data.frame. Unfortunately I was not able to get this running.

Any ideas?

1

1 Answers

0
votes

You can create a new data frame that contains weighted carat and use this new data frame for plotting

library(shiny)
library(ggplot2)

server <- function(input,output, session) {

  weighted.diamonds <- reactive(
    cbind(diamonds, weighted_carat = diamonds$carat*input$w1)
  )

  output$plot <- renderPlot({
    ggplot(weighted.diamonds(), aes(price, weighted_carat)) + geom_point()
  })

  diam <- reactive({

    user_brush <- input$user_brush
    mysel <- brushedPoints(weighted.diamonds(), user_brush)
    return(mysel)

  })

  output$table <- DT::renderDataTable(DT::datatable(diam()))
}

ui <-   fluidPage(
  sliderInput(inputId = "w1",
              label = "weight on carat",
              value = 5, min = 0, max = 40.0),
  plotOutput("plot", brush = "user_brush"),
  dataTableOutput("table")
)

shinyApp(ui = ui, server = server)