闪亮的应用程序,画笔功能与滑块相结合

Shiny app, brush feature combined with slider

我对我闪亮的应用程序有疑问,它是 2 级响应。在第一级,用户 select 在滑块上输入一个值,然后创建一个图。 该图再次具有反应性,因为您可以 select 使用画笔的一部分点,然后在下面的 table 中表示。 问题是,selected 值,基于滑块值的计算不显示。 这是我正在尝试的代码:

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)

我怀疑服务器部分有问题。即计算 carat*input$w1 改变了基础数据。 Shiny 迷路了,无法识别要在 table.

的输出中显示的数据

到目前为止的一般想法:我需要实现一个新的 data.frame,其中包含一个变量 carat*input$w1 并参考这个 data.frame。不幸的是我没能得到这个 运行.

有什么想法吗?

您可以创建一个包含加权克拉的新数据框,并使用这个新数据框进行绘图

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)