2017-10-16 142 views
0

我知道一些R,現在想創建第一個簡單的應用程序。下面是數據幀我有:閃亮:創建和理解一個簡單的應用程序

X

  Var1 Freq MotherAlive Deaths 
1 -0.3574849239 128   1  61 
2 -0.3426329835 125   2 111 
3 -0.2279137719 110   2  96 
4 -0.2038604979 81   1  63 
5 -0.2004248235 94   2 119 
6 -0.1866284306 153   2 128 
7 -0.1762490299 124   1 109 
8 -0.1736353449 87   2  91 
9 -0.1624588718 51   2 134 
10 -0.1503921713 88   1 105 
11 -0.1501143228 151   1 140 
12 -0.1492320254 128   2 126 
13 -0.1484292603 97   2  78 
14 -0.1431025682 116   2 105 
15 -0.1339971398 52   1 133 
16 -0.1336509151 43   1 106 
17 -0.1329288171 98   1 122 
18 -0.1174806726 104   2 119 
19 -0.1170459477 111   1 139 
20 -0.1063311927 140   2 106 
21 -0.1063101037 118   1 138 
22 -0.1052133391 64   2 125 
23 -0.1009333742 78   1  85 
24 -0.0932270424 112   2 140 
25 -0.0765785108 59   1  96 
26 -0.0748168224 72   1 110 
27 -0.0725537833 70   2  91 
28 -0.0702932290 111   2  63 
29 -0.0689926554 51   2  77 
30 -0.0607975570 111   1 125 
31 -0.0583357003 138   2  68 
32 -0.0535012169 53   1  75 
33 -0.0505603465 96   2  67 
34 -0.0392230996 105   1  67 
35 -0.0373462508 99   1  65 
36 -0.0355835995 75   2 128 
37 -0.0316295745 55   1  92 
38 -0.0313050685 115   1 120 
39 -0.0241317373 62   2 124 
40 -0.0214325431 89   2 134 
41 -0.0210084740 79   2 104 
42 -0.0179134498 53   2 104 
43 -0.0117510238 129   1 100 
44 -0.0086331499 75   1  83 
45 0.0008978007 57   2  79 
46 0.0076018541 60   1 127 
47 0.0124470800 45   2  96 
48 0.0192847770 68   2 110 
49 0.0195626326 84   1  76 
50 0.0277658621 93   2  78 
51 0.0324838136 66   2  93 
52 0.0427433106 109   2 110 
53 0.0542162036 61   2  73 
54 0.0617228677 104   1 136 
55 0.0732288400 113   2  73 
56 0.0805244157 52   1  61 
57 0.0933868304 141   1  68 
58 0.0962572600 47   1 111 
59 0.0992007270 98   1  74 
60 0.1126662494 137   1  67 
61 0.1175718095 91   2  65 
62 0.1193833698 76   2 110 
63 0.1321158054 56   1 122 
64 0.1346171097 142   1 136 
65 0.1461559695 80   2  60 
66 0.1551994333 160   1  76 
67 0.1645405730 147   1  67 
68 0.1672903379 124   1  71 
69 0.1680942594 108   2  87 
70 0.1805845470 67   2  63 
71 0.1899103023 85   1  78 
72 0.1988461353 150   2  97 
73 0.2034001399 64   2 129 
74 0.2182855107 90   1  94 
75 0.2200093156 101   1 125 
76 0.3053271995 104   2 112 
77 0.3337327069 72   2 124 
78 0.3668031727 100   1  75 
79 0.4302071402 151   2  69 

我想是相當簡單的,我想:我想兩個滑塊,從Rye_Prices週期和MotherAlive(他們的生產和工作偏差到目前爲止),我希望他們根據他們的價值顯示死亡人數,出生人數或兩者。但是,顯然有一些小概念,我認爲我不太明白。我寫了以下內容:

library(shiny) 

if (interactive()) { 
    options(device.ask.default = FALSE) 

    ui <- fluidPage( 
    titlePanel("Events"), 
    sidebarLayout(  
     sidebarPanel(
     sliderInput("Var1", "Deviation from Rye_Prices-Cycle", min = -0.5, max = 0.5, value = 0), 
     sliderInput("MotherAlive", "MotherAlive", min = 1, max = 2, value = 1) 
    ), 
     mainPanel(
     plotOutput("EventPlot") 
    ) 
    ) 
) 

    server <- function(input, output) { 
    output$EventPlot <- renderPlot({ 
     plot(x[,input$Var1], 
       main=input$Var1, 
       ylab="Freq", 
       xlab="Deaths") 
    }) 
    } 

    shinyApp(ui = ui, server = server) 
} 

再次,我認爲有一些我不明白的基本概念。有人可以幫我解釋我應該做什麼嗎?

謝謝你們。

親切的問候: 本傑明

+0

在問題的前一個表述中也有一個「嗨傢伙」......我這次忘了;) –

+0

唯一的錯誤是在服務器端:'錯誤在plot:object'x'not found' 。你只是沒有在你的應用中初始化你的數據框'x',因此它不能被繪製。什麼是'x'? –

+0

謝謝Antoine。你有什麼建議,如何做到最好? –

回答

1

它沒有意義的,用閃亮的這類情節在我看來。您希望使用Shiny的原因是什麼?您提到的兩個滑塊的實際用途是什麼?除了對數據進行子集化處理(例如,checkboxInput()MotherAlive == 12上繪製時),我沒有看到與Shiny有關的其他內容。

我會嘗試僅顯示數據中包含的所有信息x只有一個圖。我現在用的是ggplot2基礎設施:

ggplot(x) + 
    geom_point(aes(x = Deaths, y = Freq, size = Var1, colour = as.factor(MotherAlive))) + 
    geom_line(aes(x = Deaths, y = Freq, colour = as.factor(MotherAlive))) + 
    labs(col = "MotherAlive") + theme_bw() 

enter image description here

這是頻率死亡數量的功能,MotherAlive12的每個值,與控制colour自變量),並且其中size的點表示Rye_Prices-Cycle(命名爲Var1在您的數據集)。

你還希望從Shiny那裏得到什麼?

+0

謝謝安託萬。我同意你的看法,你的圖表完成這項工作。我的想法只是我想學習閃亮。所以我創建了這個數據框,然後嘗試繪製一些東西。我會嘗試你的建議與複選框,並提出更周到的東西。 –

+0

有很多更好的方法來學習閃亮,與專門的數據集!查看Rstudio製作的這個[示例列表](https://shiny.rstudio.com/gallery/)等等。所有這些例子都可以複製。 –

相關問題