r - 为什么 R 不应用具有反应值的 mutate ?
问题描述
我想要一个闪亮的应用程序,其中为某些数字变量创建 z 分数,然后根据滑块的用户输入,每个 z 分数乘以相应的用户输入。然后我想索引(总和)那些调整后的 z 分数,然后按该索引对数据表进行排序。
我以为我将用户输入存储在反应值(x1 和 x2)中,然后在 data.frame 的构造中调用这些值,并将调整后的 z 分数提供给 renderDataTable。
我收到此错误:
UseMethod(“mutate_”)中的错误:没有适用于“mutate_”的适用方法应用于类“c('reactiveExpr','reactive')”的对象
这是我的代码:
library(tidyverse)
library(shiny)
library(googlesheets4)
Combine.Data <- range_read("https://docs.google.com/spreadsheets/d/1EdkRNdmdpCzp4OB-IdRYWhiE3tpDkcOrvKHMxkBfX7w/edit#gid=0", col_names = TRUE)
Combine.Data = as.data.frame(Combine.Data)
# Define UI for application that draws a datatable
ui <- fluidPage(
# Application title
titlePanel("SSAT App"),
# Sidebar with a slider input for weights
sidebarLayout(
sidebarPanel(
sliderInput("Variable1_Weight",
"Variable 1 Weight:",
min = .01,
max = 1.0,
value = 0.5),
sliderInput("Variable2_Weight",
"Variable 2 Weight:",
min = .01,
max = 1.0,
value = 0.5)
),
# Show a plot of the generated table
mainPanel(
dataTableOutput("Main.Chart")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
#Generate User Weights Score from user input weights
x1 <- reactive({input$Variable1_Weight})
x2 <- reactive({input$Variable2_Weight})
DF <- Combine.Data %>%
mutate(Forty_zscore = scale(Combine.Data$FORTY, center = TRUE, scale = TRUE)) * x1 %>%
mutate(Bench_zscore = scale(Combine.Data$BENCH, center = TRUE, scale = TRUE)) * x2 %>%
mutate(SSAT_Score = Forty_zscore + Bench_zscore)
#Show the datatable
output$Main.Chart = DT::renderDataTable({DF
})
}
# Run the application
shinyApp(ui = ui, server = server)
还有一个截断的数据库:structure(list(
FIRST NAME = c("Sean", "Devin", "Amari", "Bryson",
"Daquan", "Kel", "Wavely", "Cole", "Karl", "Tyjah", "Cameron",
"Roland", "Matt", "Aaron", "Jeffrey", "Kibwe", "Mark", "Trayevon",
"Frank", "Arkeem", "Merritt", "Terrell", "Kalil", "Gabriel",
"Garrett", "Adam", "jaylen", "Justin", "Noan", "Nami", "Sal",
"Owen", "Richie", "Paul", "Anthony", "Jevon", "Jevon", "Adonus",
"Alfred", "Emmanuel", "Brandon", "Dominic", "Godwin", "Tyson",
"Marquise", "Elisha", "Johnny", "Michael", "Dan"),
LAST NAME = c("Davis",
"Dixon", "Sims", "Caven", "Wheeler", "Lee", "Crawford", "Prodoehl",
"Mofar", "During", "Hooper", "Hockenbery", "Evich", "Derwin",
"Hughes", "Galloway", "Evich", "Johnson", "Kennedy", "Pearson",
"Heaton", "Lindsey", "Cooper", "Berger", "Johnson", "Melville",
"frazier", "Williams", "Rohrer", "Brown", "Sirleaf", "Babylon",
"Goslee", "Massey Jr.", "Owens", "Hamilton", "Hamilton", "Taylor",
"Johnson", "Ekop", "Jones", "Zanoni", "Ibezim", "Tregoning",
"Marshall", "McDonald", "Britton", "Arneson", "Melville"), SCHOOL = c("Gwynn Park",
"Woodlawn High", "Meade High", "Howard", "Kenwood", "Gwynn Park",
"Meade High", "Westminster", "Eleanor Roosevelt", "Gwynn Park",
"Howard", "Middletown", "Middletown", "Middletown", "St Charles",
"Gwynn Park", "Middletown", "St Charles", "Northwestern PG",
"Meade High", "Eastern Tech", "Gwynn Park", "Meade High", "Middletown",
"Middletown", "North Carroll", "Western Tech", "Annapolis", "Liberty",
"Gwynn park", "Seneca Valley", "Westminster", "Gwynn Park", "St Charles",
"Catonsville", "St Charles", "St Charles", "Tuscarora", "Gwynn Park",
"Long Reach", "Middletown", "Linganore", "Gaithersburg", "Linganore",
"Silver Oak Academy", "Eleanor Roosevelt", "Linganore", "Linganore",
"North Carroll"), Grade = c("Junior", "Senior", "Sophomore",
"Junior", "Junior", "Sophomore", "Junior", "Sophomore", "Junior",
"Senior", "Junior", "Freshman", "Sophomore", "Sophomore", "Sophomore",
"Sophomore", "Sophomore", "Sophomore", "Junior", "Junior", "Sophomore",
"Sophomore", "Sophomore", "Junior", "Junior", "Junior", "Sophomore",
"Junior", "Sophomore", "Sophomore", "Junior", "Freshman", "Junior",
"Sophomore", "Junior", "Freshman", "Sophomore", "Sophomore",
"Junior", "Sophomore", "Junior", "Senior", "Junior", "Junior",
"Junior", "Sophomore", "Junior", "Junior", "Junior"), POSITION = c("CB",
"WR", "WR,CB", "WR,CB", "RB", "CB", "WR,CB", "WR,CB", "RB,LB",
"WR,CB", "WR,S", "RB", "QB", "RB,S", "WR,S", "RB", "CB", "Skills",
"WR", "WR,CB", "G", "S", "RB", "CB", "WR", "LB", "RB", "QB",
"Skills", "S", "RB", "WR.LB", "CB", "LB", "WR", "QB", "QB", "Skills",
"QB", "Skills", "S", "FB,LB", "S", "WR", "WR", "Skills", "Dl",
"CB", "RB"), WEIGHT = c(137, 163, 153, 173, 179, 130, 145, 164,
205, 140, 167, 172, 167, 169, 142, 143, 173, 133, 185, 141, 162,
168, 172.4, 143, 192, 141, 156, 179, 129, 169, 195, 158, 151,
160, 157, 148, 148, 160, 174, 172, 173, 171, 161, 150, 175, 192,
207, 152, 133), FORTY = c(4.45, 4.72, 4.69, 4.66, 4.59, 4.79,
4.69, 4.8, 4.66, 4.61, 4.65, 4.97, 4.87, 4.75, 4.73, 4.6, 5.02,
5.07, 4.93, 4.75, 4.87, 4.75, 4.87, 5.16, 4.97, 4.83, 4.83, 4.63,
4.56, 4.95, 4.91, 4.99, 4.68, 4.72, 4.76, 5.09, 5.09, 4.95, 4.98,
4.99, 4.75, 4.91, 4.85, 4.97, 4.68, 4.84, 5.19, 4.87, 4.94),
BROAD = c(112, 106, 94, 106, 105, 95, 112, 106, 106, 116,
117, 96, 111, 118, 110, 97, 104, 96, 106, 111, 101, 107,
98, 98, 104, 90, 97, 102, 106, 107, 107, 98, 108, 106, 95,
94, 94, 102, 113, 110, 116, 102, 109, 108, 112, 104, 98,
103, 88), BENCH = c(3, 6, 0, 13, 12, 0, 0, 1, 20, 0, 6, 12,
14, 0, 0, 4, 11, 0, 6, 1, 2, 7, 9, 11, 20, 0, 0, 0, 0, 9,
16, 6, 7, 16, 11, 0, 0, 1, 4, 5, 9, 18, 10, 0, 0, 11, 13,
13, 0),
P. AGILITY= c(3.91, 4.01, 4.06, 4.06, 4.07, 4.12,
4.12, 4.12, 4.12, 4.13, 4.13, 4.14, 4.15, 4.18, 4.19, 4.19,
4.21, 4.22, 4.23, 4.25, 4.25, 4.25, 4.25, 4.25, 4.27, 4.28,
4.28, 4.28, 4.28, 4.28, 4.28, 4.29, 4.29, 4.29, 4.3, 4.31,
4.31, 4.31, 4.31, 4.31, 4.31, 4.31, 4.32, 4.34, 4.34, 4.34,
4.34, 4.34, 4.35)), row.names = c(NA, -49L), class = c("tbl_df",
"tbl", "data.frame"))
解决方案
推荐阅读
- php - 根据数据库在表中放一个类
- c - Flutter (Dart) ffi - 应用程序在处理外部库方法期间冻结
- c# - 使用 yamldotnet 反序列化 yaml 时出错 - 找不到属性
- php - 是否可以在数组上使用变异器?拉拉维尔
- python - 关键错误:[Int64Index([...]dtype='int64')] 都不在 [columns] 中
- python - 从多进程损坏文件记录?
- ios - 基于网页元颜色ios设置状态栏颜色
- java - 如何跳过特定的循环循环?
- javascript - 错误:等待仅在异步函数中有效
- html - 浏览器是否默认在后台将 HSL 转换为 RGB?