我不确定如何将闪亮的actionInputs与actionButtons结合使用。我想要的是使用actionInput选择变量“ colour”,使用actionButton“ Player1”或“ Player2”进行选择,并使用它们读取数据“ colour_blue.dat”,“ colour_red.dat”,“ colour_yellow.dat”或“ colour_green.dat”。这些数据中的每一个都包含变量“ Date”,“ Player1”和“ Player2”。因此,我要在绘图上表示玩家所选择的颜色。
我只知道如何用一种颜色创建图,但是我对如何在调用actionButton变量时更改selectInput值时如何更改颜色数据感到迷茫。
library(shinydashboard)
library(shiny)
library(ggplot2)
#Read the data
Blue <- read.table("colour_blue.dat",header=TRUE)
Red <- read.table("colour_red.dat",header=TRUE)
Yellow <- read.table("colour_yellow.dat",header=TRUE)
Green <- read.table("colour_green.dat",header=TRUE)
dput(red)
structure(list(Date = 1:28, Player1 = c(4.2, 4.4, 4.5, 4.5, 4.4,
4.6, 4.9, 4.7, 5.1, 5.6, 4.9, 5, 5.5, 6.2, 5.9, 6.9, 6.1, 6.4,
6.4, 6.3, 6.4, 6.6, 7.1, 5.9, 6.3, 6.9, 7, 6.9), Player2 = c(5.2,
5.6, 5.9, 5.8, 6.1, 5.4, 6.1, 5.7, 6.7, 6.5, 6.4, 6.3, 6.7, 7.4,
5.8, 5.7, 5.6, 5.7, 5.7, 6, 5.9, 5.4, 5.8, 6.2, 6.4, 6.3, 6,
6.5)), class = "data.frame", row.names = c(NA, -28L))
dput(blue)
structure(list(Date = 1:28, Player1 = c(6.2, 5.4, 7.5, 5.5, 6.4,
5.6, 7.9, 5.7, 7.1, 6.6, 6.9, 6, 7.5, 7.2, 7.9, 7.9, 6.1, 7.4,
6.4, 7.3, 6.4, 7.6, 6.1, 6.9, 6.3, 7.9, 6, 7.9), Player2 = c(6.2,
6.6, 6.9, 6.8, 7.1, 6.4, 7.1, 6.7, 7.7, 7.5, 6.4, 7.3, 6.7, 6.4,
5.8, 6.7, 5.6, 6.7, 5.7, 7, 5.9, 6.4, 5.8, 7.2, 6.4, 7.3, 6,
7.5)), class = "data.frame", row.names = c(NA, -28L))
dput(green)
structure(list(Date = 1:28, Player1 = c(4.2, 5.4, 6.5, 7.5, 6.4,
5.6, 4.9, 3.7, 4.1, 5.6, 6.9, 7, 6.5, 5.2, 4.9, 5.9, 6.1, 7.4,
6.4, 5.3, 4.4, 3.6, 4.1, 5.9, 6.3, 7.9, 8, 7.9), Player2 = c(5.2,
7.6, 5.9, 3.8, 5.1, 5.4, 6.1, 3.7, 4.7, 4.5, 5.4, 7.3, 8.7, 4.4,
2.8, 4.7, 1.6, 2.7, 9.7, 8, 7.9, 4.4, 3.8, 1.2, 2.4, 4.3, 3,
4.5)), class = "data.frame", row.names = c(NA, -28L))
dput(yellow)
structure(list(Date = 1:28, Player1 = c(9.2, 9.4, 8.5, 7.5, 8.4,
6.6, 7.9, 5.7, 6.1, 4.6, 7.9, 2, 7.5, 5.2, 4.9, 8.9, 1.1, 2.4,
7.4, 7.3, 3.4, 6.6, 2.1, 8.9, 4.3, 4.9, 8, 9.9), Player2 = c(9.2,
9.6, 8.9, 7.8, 6.1, 5.4, 4.1, 3.7, 2.7, 1.5, 0.4, 1.3, 2.7, 3.4,
4.8, 5.7, 6.6, 7.7, 8.7, 9, 8.9, 7.4, 7.8, 3.2, 4.4, 5.3, 6,
6.5)), class = "data.frame", row.names = c(NA, -28L))
#Define the UI
ui <- dashboardPage(
dashboardBody(
fluidRow( width = 12 , column(width = 3 ,
selectInput("colour" , label = "Estaciones" , choices = c("Blue" , "Red"
, "Yellow" , "Green"))),
box(width = 4 ,
actionButton("player1","Player_1", width = '100%'),
actionButton("player2","Player_2", width = '100%')
actionButton("erase","Erase", width = '100%')),
box( width= 12 , plotOutput(outputId = "plot", height = "500px"))
)
)
#Define the server
server <- function(input, output) {
v <- reactiveValues(data = NULL)
observeEvent(input$player1, {
v$data <- Blue$Player1
})
observeEvent(input$player2, {
v$data <- Blue$Player2
})
observeEvent(input$erase, {
v$data <- NULL
})
output$plot <- renderPlot({
if (is.null(v$data)) return()
qplot(x= Date, y=v$data, ylim = c(0,10),
xlab = "Date", ylab = "Skill") + geom_line(color="darkblue")
})
}
shinyApp(ui, server)
我期望的是,当我选择“红色”和“ Player_1”时,我得到了带有数据集“ colour_red.dat”的变量Player1的图。如果我更改为“ Player_2”,则表示该新变量,如果我将“红色”更改为“蓝色”,则它将绘制相同变量,但数据为“ colout_blue.dat”。
任何帮助将不胜感激。