2016-10-04 127 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

相關問題