我不确定如何将闪亮的 actionInputs 与 actionButtons 结合起来。我想要的是使用 actionInput 选择变量“color”,使用 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)
我期望的是,当我选择“Red”和“Player_1”时,我会得到一个带有数据集“colour_red.dat”的变量 Player1 的图。如果我更改为“Player_2”,那么它代表这个新变量,如果我将“Red”更改为“Blue”,它会绘制相同的变量,但数据为“colout_blue.dat”。
任何帮助将不胜感激。