2016-10-04 124 views
-1

我花了两天试图创建一个Shiny应用程序无济于事。运行这些示例没什么问题,但是当我想根据自己的喜好对其进行修改时,我只会遇到不断的错误和功能缺失。我的第一个R闪亮的应用程序

我有一个简单的数据集,包含100个X观测值和100个Y观测值。我想用滑块输入来绘制X和Y的直方图。我也想在X上创建Y的散点图。我非常感谢这里的帮助。

我不是新来的R,但我是新来的闪亮。有没有一种方法可以使用ggplot来创建视觉效果?

非常感谢。

+1

从内置的R功能以及闪亮的示例模板开始。使用'shiny :: renderPlot()'。在'renderPlot()'里面,用你在文章中提到的各个参数返回'hist()'(对于直方图)和'plot()'(对于散点图)。看起来X和Y是静态的,所以你可以将它们存储在全局范围内。 – nilsole

+0

谢谢你的回复。 :) – Seanosapien

回答

1

这是两个不同布局的快速示例。使用其中一个ui.R当然。把global.R与ui.R和server.R

ui.R V1

library(shiny) 
    library(ggplot2) 
    shinyUI(fluidPage(

     titlePanel("Quick Shiny Example"), 
     sidebarLayout(
     sidebarPanel(
      sliderInput("xBins", 
         "Number of bins for X variable:", 
         min = 1, 
         max = 50, 
         value = 30), 
      sliderInput("yBins", 
         "Number of bins for Y variable:", 
         min = 1, 
         max = 50, 
         value = 30) 
     ), 
     mainPanel(
      plotOutput("xDistPlot"), 
      plotOutput("yDistPlot"), 
      plotOutput("xyScatterPlot") 
     ) 
    ) 
    )) 

ui.R V2

library(shiny) 
    library(ggplot2) 
    shinyUI(fluidPage(
      titlePanel("Quick Shiny Example"), 
      fluidRow(
        column(width = 4, 
          sliderInput("xBins", 
             "Number of bins for X variable:", 
             min = 1, 
             max = 50, 
             value = 30) 
        ), 
        column(width = 4, 
          sliderInput("yBins", 
             "Number of bins for Y variable:", 
             min = 1, 
             max = 50, 
             value = 30) 
        ), 
        column(width = 4) 
      ), 
      fluidRow(
        column(width = 4, 
          plotOutput("xDistPlot") 
        ), 
        column(width = 4, 
          plotOutput("yDistPlot") 
        ), 
        column(width = 4, 
          plotOutput("xyScatterPlot") 
        ) 
      ) 
    )) 

server.R同一文件夹

library(shiny) 
    library(ggplot2) 

    shinyServer(function(input, output) { 

     output$xDistPlot <- renderPlot({ 
     g <- ggplot(df, aes(x = x)) 
     g <- g + geom_histogram(bins = input$xBins) 
     g 
     }) 
     output$yDistPlot <- renderPlot({ 
       g <- ggplot(df, aes(x = y)) 
       g <- g + geom_histogram(bins = input$yBins) 
       g 
     }) 
     output$xyScatterPlot <- renderPlot({ 
       g <- ggplot(df, aes(x = x, y = y)) 
       g <- g + geom_point() 
       g 
     }) 

    }) 

global.R

df <- data.frame(
    x = rnorm(100), 
    y = rnorm(100)*2 

+0

绝对太棒了。非常感谢先生。 – Seanosapien

1

这里是我的回答,与XY随机数,只是作为一个快速的想法。添加ggplot到这应该很容易。

library(shiny) 

ui <- shinyUI(
    fluidPage(
    sliderInput("nrBinsX", "Number of bins to display for X", min = 2, max = 10, value = 5), 
    plotOutput("histX"), 
    sliderInput("nrBinsY", "Number of bins to display for Y", min = 2, max = 10, value = 5), 
    plotOutput("histY"), 
    plotOutput("scatterXY") 
) 
) 

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

    dataFrame <- data.frame (
    "X" = sample(100,100,replace = T), 
    "Y" = sample(100,100,replace = T) 
) 

    getHist <- function (var,nr){ 
    return (hist(
     x = var, 
     breaks = seq(0,100,100/nr), 
     freq = T 
    )) 
    } 

    output$histX <- renderPlot({ 
     return(
     getHist(var = dataFrame$X, 
       nr = input$nrBinsX 
     )) }) 

    output$histY <- renderPlot({ 
     return(  return(
     getHist(var = dataFrame$Y, 
       nr = input$nrBinsY 
     ) 
    )) }) 

    output$scatterXY <- renderPlot({ 
    return(
     plot(x = dataFrame$X, 
      y = dataFrame$Y) 
    ) 
    }) 

}) 

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

谢谢。我花了大量的时间在线搜索一个清晰,结构良好的例子。 :) – Seanosapien

相关问题