2017-09-24 114 views
0

我是R和Shiny包的新手。我有一个csv文件,4 col和600行,我想用ggplot2绘制一些图。闪亮的滑块输入从csv文件读取行

我的UI和服务器代码是这样的:

dt<-read.csv('file.csv') 
server <- function(input, output) { 
    output$aPlot <- renderPlot({ 
    ggplot(data = dt, aes(x = Col1, y = Col2, group = 'Col3', color = 'Col4')) + geom_point() 
    }) 
} 
ui <- fluidPage(sidebarLayout(
sidebarPanel(
    sliderInput("Obs", "Log FC", min = 1, max = 600, value = 100) 
), 
mainPanel(plotOutput("aPlot")) )) 

在这里,我可以得到ggplot输出,但我不知道如何使用此滑块输入来控制的行数被读取,即,我想要这个"Obs"输入来定义要在图中使用的Col1的大小。

回答

1

尝试这样的事情,例如这里是mtcars数据集:

library(shiny) 
library(ggplot2) 

dt <- mtcars[,1:4] 

ui <- fluidPage(
    sidebarPanel(
    sliderInput("Obs", "Log FC", min = 1, max = nrow(dt), value = nrow(dt)-10) 
), 
    mainPanel(plotOutput("aPlot")) 
) 

server <- function(input, output) { 

    mydata <- reactive({ 
    dt[1:as.numeric(input$Obs),] 
    }) 

    output$aPlot <- renderPlot({ 
    test <- mydata() 
    ggplot(data = test, aes(x = test[,1], y = test[,2], group = names(test)[3], color = names(test)[4])) + geom_point() 
    }) 
} 

shinyApp(ui = ui, server = server) 
+0

非常感谢,它的工作完美。如果我想在这个应用程序中添加另一个滑块,我必须做什么..!?我的意思是一个data.frame内部反应存储滑块输入.. –

+0

我不清楚你想如何添加一个滑块。请提供另一个问题的详细例子,并关闭/接受这个问题 –

-1

服务器更改为:

server <- function(input, output) { 
    observe({ 
    dt_plot <- dt[1:input$Obs,] 

    output$aPlot <- renderPlot({ 
ggplot(data = dt_plot, aes(x = Col1, y = Col2, group = 'Col3', color = 'Col4')) + geom_point() 
    }) 
}) 
} 
+1

通常它不是一个好主意,做了很多的计算之内'observe' –

+0

正确,'反应'更好 –