0

I am not sure about how to combine in shiny actionInputs with actionButtons. What I want is to select a variable "colour" with an actionInput, select with an actionButton "Player1" or "Player2" and with these, read the data "colour_blue.dat", "colour_red.dat", "colour_yellow.dat" or "colour_green.dat". Each one of these data contains the variables "Date", "Player1" and "Player2". So what I want is to represent on a plot the Player selected of the selected colour.

I just know how to create the plot with one of the colours, but I am very lost on how to change the data of the colour when changing the selectInput value while calling the actionButton variable.

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)

What I expect is that when I select "Red" and "Player_1" I get a plot with the variable Player1 of the dataset "colour_red.dat". If I change to "Player_2" then it represents this new variable, and if I change "Red" to "Blue", it plots the same variable but of the data "colout_blue.dat".
Any help will be greatly appreciate.

Rai
  • 113
  • 12
  • Can you provide the data here with `dput` or on github? The code also has got some errors. I think I can help you with that problem. – DSGym Jun 11 '19 at 13:20
  • @DSGym Yes, I'll do it right now. Thanks. Should I copy the output of dput on the code? – Rai Jun 11 '19 at 13:55
  • Done, probably not as it should be since I don't know how to indicate what is an output... – Rai Jun 11 '19 at 14:10

0 Answers0