2017-09-26 163 views
-1

我想知道是否可以使用带有滑块输入的交互式图形中的nearPoints()获取行数据。我app.R文件看起来像:带滑块输入的闪亮nearPoints()

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) 

闪亮nearPoints()是没有这个滑块输入可以正常使用。当我使用滑块输入时,我无法获取行数据直到max。有没有使用滑块输入的方法?任何帮助表示赞赏。

+0

你尝试过使用renderUI吗? – amrrs

+0

只是为了检查我的理解,您希望'input $ plot1_click'中的值在'sliderInput'中使用?如果这就是你想要的,你需要将'sliderInput'函数移动到'server.R'中的'renderUI'输出函数中。然后你可以用'uiOutput'在'ui.R'中渲染它。这里是[相关文档](https://shiny.rstudio.com/reference/shiny/latest/renderUI.html) –

+0

滑块输入是“Obs”,它控制传递给函数的行数。 “输入$ plot1_click”是鼠标点击的点。 –

回答

0

以下代码适用于我。看起来nearPoints由于aes(x = test[,2], y = test[,1])声明无法分辨出您的数据集的哪些列被显示。另一个可能的解决方案应该是设置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()获得所有可用数据集的列表。