我正试图构建一个闪亮的应用程序,重新定位一个6边的骰子。A有2个动作按钮。我想要实现的是,每次单击reroll按钮都会在相同的(!!)上添加另一个概率线。阴谋。
每条概率线都应该是另一种颜色(因此,如果你点击它30次,你会看到30种不同颜色的6线作为结果)。
使用“重新启动”按钮,您可以擦除图中所有打印的图形,可以从头开始尝试(因此,如果一行是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)问题:
我很乐意在那里提供每一个帮助:)
非常感谢。
发布于 2017-03-08 15:21:40
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)
}

发布于 2017-03-08 15:02:25
我不知道你到底想做什么,但是:
https://stackoverflow.com/questions/42673890
复制相似问题