2017-03-08 121 views
1

我正试图建立一个闪亮的应用程序来重新卷动6面骰子。 a有2个动作按钮。我想实现,每次点击重放按钮将在SAME(!!)图上添加另一个概率线。互动剧情

每个probabilityline应该是另一种颜色(因此,如果你点击它的30倍,你会看到6的30个不同颜色的problines的结果)

与重启按钮,你可以删除所有打印图表在从一开始就可以尝试的阴谋(所以如果一行是altime打印也可以)。

下面是啥子我已创建至今:

library(ggplot2) 
library(ggthemes) 
library(extrafont) 

ui <- fluidPage(

# Application title 
titlePanel("Roll the dice"), 

# Sidebar with a go_button and a restart_button 
sidebarLayout(
sidebarPanel(
    actionButton("reroll", 
       "ReRoll the dice", 
       width = "100%"), 
    br(), 
    br(), 

    actionButton("restart", 
       "Restart", 
       width = "100%") 
), 

# Show a plot 
mainPanel(
    plotOutput("plot") 
) 
) 
) 

server <- function(input, output) { 

output$plot <- renderPlot({ 

input$reroll 

a <- 1:6 
b <- 1:1000 
eyes <- sample(a,1000,replace=T) 
six <- eyes == 6 
c <- cumsum(six)/1:1000 
df <- data.frame(b , c) 


gl <- geom_line(aes(x = b , y = c), size = 0.9, linetype="dashed", colour = "red", alpha = 0.5) 

p <- ggplot(data = df) + 
     xlim(0, 1000) + 
     ylim(0, 1) + 
     labs(x="Number of Throws", y="Probability of 6") + 
     ggtitle("Approximation of Diceprobability") + 

     theme_fivethirtyeight() + scale_colour_fivethirtyeight() + 
     theme(axis.title = element_text(family="Atlas Grotesk Regular"), 
       legend.position="bottom", legend.direction="horizontal", 
       legend.title=element_blank(), 
       plot.title=element_text(family="Atlas Grotesk Medium"), 
       legend.text=element_text(family="Atlas Grotesk Regular"), 
       text=element_text(family="DecimaMonoPro")) 

p + gl 

}) 
} 

shinyApp(ui=ui, server = server) 

问题:

  1. 我如何可以绘制在同一个图(不同的线,你知道...我点击30倍它在同一个图中构建30行)

  2. 如何编程它,每行有另一种颜色?

  3. 我该如何“删除”它与我的重启按钮可以有另一个尝试?

我会很高兴每个帮助在那里:-)

非常感谢。

回答

0

我不知道你想什么acheive,在所有但:

  1. 要更新一个情节我会成立,其持有的剧情对象的反应值。然后,我会使用并观察事件来观看按钮按下。在这个事件里面添加你的新行。

  2. 然后,您可以为其他按钮创建一个observeEvent,以用空图替换您的绘图对象。

1
  • 使用observeEvent触发按钮。
  • 使用reactiveValues来存储绘图和颜色计数器。
  • 请勿使用该色阶,因为它只有三种颜色。

保持ui,因为它是那么让server

server <- function(input, output) { 
    # start plot 
    p_blank <- ggplot() + 
    xlim(0, 1000) + 
    ylim(0, 1) + 
    labs(x="Number of Throws", y="Probability of 6") + 
    ggtitle("Approximation of Diceprobability") + 
    theme_fivethirtyeight() + 
    theme(legend.position="bottom", legend.direction="horizontal", 
      legend.title=element_blank()) 

    #reactive values 
    reac <- reactiveValues() 
    reac$p_lines <- p_blank 
    reac$counter <- 0 

    # button functionality 
    observeEvent(input$reroll, 
       { 
       input$reroll 
       a <- 1:6 
       b <- 1:1000 
       eyes <- sample(a,1000,replace=T) 
       six <- eyes == 6 
       c <- cumsum(six)/1:1000 
       df <- data.frame(b, c, counter = reac$counter) 
       gl <- geom_line(aes(x = b , y = c, col = factor(counter)), df, 
           size = 0.9, linetype="dashed", alpha = 0.5) 
       reac$p_lines <- reac$p_lines + gl 
       reac$counter <- reac$counter + 1 
       }) 

    observeEvent(input$restart, 
       { 
       reac$p_lines <- p_blank 
       reac$counter <- 0 
       }) 

    # draw the plot 
    output$plot <- renderPlot(reac$p_lines) 
} 

enter image description here

+0

非常感谢!这就是我的搜索!谢谢 :-) –