플레이어의 이름과 통계를 입력 한 다음 해당 통계에 대해 플레이어가 속한 백분위 수를 반환하는 간단한 Shiny 앱을 만들고 있습니다. 현재 '통계'위젯으로 인해 오류가 발생하는 문제가 발생했습니다 (제목 참조).
사용중인 패키지와 데이터 샘플은 다음과 같습니다.
library(shiny)
library(dplyr)
library(mosaic)
player <- c("John", "Mike", "Devon", "Greg", "Bruce", "Zachary", "Jack", "Graham", "Jordan", "Sandy")
team <- c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B")
wins <- c(1:10)
losses <- c(10:1)
sampledata <- data.frame(player, team, wins, losses)
앱에는 (1) 플레이어 이름 입력, (2) 통계 선택, (3) 선택 실행의 세 가지 위젯이 있습니다.
출력은 한 줄의 텍스트입니다.
여기 있습니다ui.r:
ui <- fluidPage(
titlePanel("Percentile Generator"),
sidebarLayout(
sidebarPanel(
textInput("playerfind",
"Player:",
value = "Devon"),
selectInput("stat1", "Select Statistic:",
choices = list("wins", "losses", "ties"),
selected = "wins"),
actionButton("action", label = "Generate Percentile!")
),
mainPanel(
textOutput("percentmachine")
))
)
서버는 조금 더 복잡합니다. 1 단계 sample data
는 입력을 기반으로 1x3 데이터 프레임을 필터링 하고 생성합니다. 2 단계는 매트릭스에서 필요한 값을 가져와 저장합니다. 마지막으로 3 단계는 2 단계의 입력을 받아 백분위 수를 생성합니다.
server <- function(input, output) {
step1 <- reactive({sampledata %>%
transmute(player, stat = zscore(input$stat1)) %>%
filter(player == input$playerfind)})
step2 <- reactive({step1()[1,2]})
step3 <- reactive({round(pnorm(step2())*100, digits = 1)})
output$percentmachine <- renderText ({
input$action
isolate(paste(input$playerfind,
"had more",
input$stat1,
"than",
step3(),
"percent of players."))})
}
이 오류는 input$stat1
1 단계 에서 발생했다고 생각합니다. 이 입력을 '승리'와 같은 특정 통계로 대체하면 통계를 변경할 수 없지만 반짝이는 앱이 정상적으로 실행됩니다. 나는 이것으로 꽤 오랫동안 어려움을 겪어 왔기 때문에 여기서 물어볼 것이라고 생각했습니다.
미리 감사드립니다! xD
당신은 먹이 character
받는 함수에 값을 numeric
.
반짝이는 / 반응하는 환경 밖에서 시뮬레이션하겠습니다.
input <- list(playerfind="Devon", stat1="wins")
# I don't have mosaic installed
zscore <- function( x, na.rm=getOption("na.rm", FALSE) ) ( x - mean(x, na.rm=na.rm)) / sd(x, na.rm=na.rm)
sampledata %>%
transmute(player, stat = zscore(input$stat1))
# Warning in mean.default(x, na.rm = na.rm) :
# argument is not numeric or logical: returning NA
# Error in x - mean(x, na.rm = na.rm) (from #1) :
# non-numeric argument to binary operator
다음을 사용하여 트릭으로이 문제를 해결할 수 있습니다 get
.
sampledata %>%
transmute(player, stat = zscore(get(input$stat1)))
# player stat
# 1 John -1.4863011
# 2 Mike -1.1560120
# 3 Devon -0.8257228
# 4 Greg -0.4954337
# 5 Bruce -0.1651446
# 6 Zachary 0.1651446
# 7 Jack 0.4954337
# 8 Graham 0.8257228
# 9 Jordan 1.1560120
# 10 Sandy 1.4863011
### which is effectively this
zscore(sampledata$wins)
# [1] -1.4863011 -1.1560120 -0.8257228 -0.4954337 -0.1651446 0.1651446
# [7] 0.4954337 0.8257228 1.1560120 1.4863011
(그리고 filter
필요에 따라).
이 문제를 해결하는 또 다른 방법은 과잉 일 수도 있지만 프로젝트의 다른 작업에 대한 통찰력을 제공하는 경우 데이터를 전체에서 긴 것으로 재구성 할 수 있습니다. 이미를 사용하고 있으므로 dplyr
다음을 포함하겠습니다 tidyr
.
library(tidyr)
### this is just a demo of reshaping from wide to long
sampledata %>%
pivot_longer(c(-player, -team), names_to = "winlose", values_to = "val")
# # A tibble: 20 x 4
# player team winlose val
# <fct> <fct> <chr> <int>
# 1 John A wins 1
# 2 John A losses 10
# 3 Mike B wins 2
# 4 Mike B losses 9
# 5 Devon A wins 3
# 6 Devon A losses 8
# 7 Greg B wins 4
# 8 Greg B losses 7
# 9 Bruce A wins 5
# 10 Bruce A losses 6
# 11 Zachary B wins 6
# 12 Zachary B losses 5
# 13 Jack A wins 7
# 14 Jack A losses 4
# 15 Graham B wins 8
# 16 Graham B losses 3
# 17 Jordan A wins 9
# 18 Jordan A losses 2
# 19 Sandy B wins 10
# 20 Sandy B losses 1
### this is the actual work
sampledata %>%
pivot_longer(c(-player, -team), names_to = "winlose", values_to = "val") %>%
filter(winlose == input$stat1) %>%
mutate(z = zscore(val))
# # A tibble: 10 x 5
# player team winlose val z
# <fct> <fct> <chr> <int> <dbl>
# 1 John A wins 1 -1.49
# 2 Mike B wins 2 -1.16
# 3 Devon A wins 3 -0.826
# 4 Greg B wins 4 -0.495
# 5 Bruce A wins 5 -0.165
# 6 Zachary B wins 6 0.165
# 7 Jack A wins 7 0.495
# 8 Graham B wins 8 0.826
# 9 Jordan A wins 9 1.16
# 10 Sandy B wins 10 1.49
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다