【问题标题】:Shiny nearPoints() with slider input带滑块输入的闪亮 nearPoints()
【发布时间】:2018-03-07 06:59:55
【问题描述】:

我想知道是否可以使用nearPoints() 从带有滑块输入的交互式图表中获取行数据。我的 app.R 文件如下所示:

library('shiny')
library('ggplot2')

dt <-read.csv('file.csv')
ui <- fluidPage(
        plotOutput("plot1", height = 550, click = "plot1_click"),
    fluidRow(
        column(3,
        sliderInput("Obs", "Number of Books", min = 1, max = nrow(up), value = 50)
        ),
        column(3, offset = 3, 
               h4("Legends"),
         verbatimTextOutput("selected")
        )  
      )
    )
server <- function(input, output) {
   mydata <- reactive({
    dt[1:as.numeric(input$Obs),]
  })
  output$plot1 <- renderPlot({
    test <- mydata()
    ggplot(data = test, aes(x = test[,2], y = test[,1])) +  geom_point()
  }) 
  output$selected <- renderPrint({
    file <- mydata()
    nearPoints(file, input$plot1_click, threshold = 10, maxpoints = 1,
               addDist = FALSE)
  })
}
shinyApp(ui = ui, server = server)

Shiny nearPoints() 在没有此滑块输入的情况下完美运行。当我使用滑块输入时,我无法获取行数据,直到最大值。有什么方法可以使用滑块输入吗?任何帮助表示赞赏。

【问题讨论】:

  • 你用 renderUI 试过了吗?
  • 只是为了检查我是否理解,您希望input$plot1_click 的值用于您的sliderInput?如果这是您想要的,您需要将您的sliderInput 函数移动到您的server.R 内的renderUI 输出函数中。然后,您可以使用uiOutputui.R 中渲染它。这是relevant documentation
  • 滑块输入是“Obs”,它控制传递给函数的行数。 “input$plot1_click”是鼠标点击的点。
  • 请发布可重现的示例、数据和绘图?如果不为 OP 提供足够的详细信息来帮助您,就很难回答您的问题。

标签: r shiny slider interactive


【解决方案1】:

以下代码对我有用。由于aes(x = test[,2], y = test[,1]) 语句,nearPoints 似乎无法分辨显示数据集的哪些列。另一种可能的解决方法是在nearPoints 中设置参数xvaryvar

library('shiny')
library('ggplot2')

dt <-mtcars
ui <- fluidPage(
  plotOutput("plot1", height = 550, click = "plot1_click"),
  fluidRow(
    column(3,
           sliderInput("Obs", "Number of Cars", min = 1, max = nrow(dt), value = 50)
    ),
    column(3, offset = 3, 
           h4("Legends"),
           verbatimTextOutput("selected")
    )  
  )
)
server <- function(input, output) {
  mydata <- reactive({
    dt[1:as.numeric(input$Obs),]
  })
  output$plot1 <- renderPlot({
    test <- mydata()
    ggplot(data = test, aes(mpg, wt)) +  geom_point()
  }) 
  output$selected <- renderPrint({
    file <- mydata()
    nearPoints(file, input$plot1_click, threshold = 100, maxpoints = 1,
               addDist = FALSE)
  })
}
shinyApp(ui = ui, server = server)

快速说明:请尝试使用 R 中的默认数据集之一使您的问题中的代码可重现。您可以通过调用 data() 获取所有可用数据集的列表。

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2013-07-14
    • 2016-11-17
    • 2020-07-20
    • 2017-03-03
    • 2015-02-20
    相关资源
    最近更新 更多