Я новичок в R блестящий, и у меня проблемы с корректным отображением моего сюжета Я пытаюсь создать черновик доски. Когда пользователь вводит номер пика, выбирает игрока и нажимает кнопку «Завершить», он обновляет черновик черновика с именем и цветным фоном.
У меня есть код, который правильно работает в базе R. Однако, похоже, что render $ plot не будет выполнять функцию text () или rect (). Я не привязан к построению платы таким образом, поэтому, если есть более простой способ построить многоцветную сетку с именами, которые мне sh лучше с блестящими, я буду рад услышать.
```
---
title: "Draft Board"
output: html_document
runtime: shiny
---
`building baseline data frame`
```{r echo = F}
rm(list=ls())
require(grDevices)
## set up the plot region:
dev.off()
Names<- c("Joe","Arther","Bobby","Carl",
"Emmit","Frank","George","Hugo",
"Kevin","Leo","Matt","Neal")
Uniform<-c("Red","Red","Red","Red",
"Green","Green","Green","Yellow",
"Blue","Blue","Red","Yellow")
Player_Pool<-cbind(Names,Uniform)
#Rows and Columns
Round<-4
Teams<-3
#make basic plot to load draft board on
plot(c(0, Teams*10), c(0, Round*10), type = "n", xlab = "", ylab = "", axes = F)
X <- rep(seq(from= 0, to = (Teams-1)*10, by = 10),Round)
Y <- sort(rep(seq(from = Round*10, to = 10, by = -10),Teams),decreasing = T)
Wide <-10
Height <-10
rect(X, Y, X+Wide, Y-Height, col = "WHITE")
# get draft order correct
Odd<-seq(from=0,to=Round,by=2)
OLeft<-(1+Odd*Teams)
ORight<-(Teams+Odd*Teams)
Even<-seq(from=1,to=Round,by=2)
ELeft<-(1+Even*Teams)
ERight<-(Teams+Even*Teams)
loop<-min(length(OLeft),length(ELeft),length(ORight),length(ERight))
for (i in 1:loop){
if(i == 1){
Picknum<-OLeft[i]:ORight[i]
Picknum<-c(Picknum,(ERight[i]:ELeft[i]))
}else{
Picknum<-c(Picknum,(OLeft[i]:ORight[i]))
Picknum<-c(Picknum,(ERight[i]:ELeft[i]))
}
}
if(Round%%2==1){Picknum<-head(Picknum,n = length(Picknum)-Teams)}
#placement of names and numbers
Hort<-X+Wide
Vert<-Y-Height
Player<-rep("",Teams*Round)
COLOR<-rep("White",Teams*Round)
#empty data frame
TESTING <- cbind(Picknum, Player, COLOR, X, Y, Hort, Vert )
#Assign colors from players
COLOR <- as.character(Player_Pool[,2])
```
Следующая часть в базовом RI может вручную обновить два входа и заполнить плату.
```
#### Manually Loop to fill the board ####
#player and pick selection
CPlayer <- "George"
Cpick <- 1
#indexing row for getting color and name
PP_Row<-which(Player_Pool[,1]==CPlayer)
#indexing for getting which row to put it in
Plot_Row<-which(TESTING[,1]==Cpick)
#update matrix
TESTING[Plot_Row,3]<-as.character(Player_Pool[PP_Row,2])
TESTING[Plot_Row,2]<-as.character(Player_Pool[PP_Row,1])
#Replot with new data
rect(X, Y, X+Wide, Y-Height, col = TESTING[,3])
text(X+1,Y-5,TESTING[,1],cex = 0.7)
text(X+5,Y-5,TESTING[,2],cex = 0.7)
```
Однако, когда я пытаюсь поместить его в «render $ plot», он не обновляется.
```{r echo = FALSE}
library(shiny)
#bulding user interface
ui<-fluidPage(
#draft pick
numericInput(inputId = "Cpick", label = "Draftpick #", value = 1,
min = 1, max = Teams*Round, step = 1 ),
#player name
selectInput(inputId =,"CPlayer", label = "Player",
choices = Player_Pool[,1]),
#submit the pick
actionButton(inputId = "Button",label = "Finalize"),
#OUTPUT
plotOutput(outputId = "Board")
)
#code to run
server<- function (input,output){
eventReactive(input$Button,{
#indexing row for getting color and name
PP_Row<-which(Player_Pool[,1]==CPlayer)
#indexing for getting which row to put it in
Plot_Row<-which(TESTING[,1]==Cpick)
#update matrix
TESTING[Plot_Row,3]<-as.character(Player_Pool[PP_Row,2])
TESTING[Plot_Row,2]<-as.character(Player_Pool[PP_Row,1])
})
output$Board <- renderPlot({
#Replot with new data
#rect(X, Y, X+Wide, Y-Height, col = TESTING[,3])
#text(X+1,Y-5,TESTING[,1],cex = 0.7)
#text(X+5,Y-5,TESTING[,2],cex = 0.7)
})
}
#app to run
shinyApp(ui = ui, server = server)
```