gpt4 book ai didi

r - 使用 Shiny Inputs 在数据集中创建新变量

转载 作者:行者123 更新时间:2023-12-01 13:51:52 26 4
gpt4 key购买 nike

假设我们有一个小的 nfl 球员数据集:

      Team  POS  WT
1 Chiefs K 175
2 Bills K 225
3 Browns CB 175
4 Chargers DT 300
5 Browns TE 220
6 Texans LB 262
7 Cowboys LB 234
8 Patriots DT 309
9 Titans K 218
10 Browns WR 167

我想创建一个 Shiny 的页面,让人们将自己的权重 (WT) 放入其中,并且在数据集中,它将创建每个 nfl 球员的权重与输入权重之间的差异。

到目前为止....不。

ui.R

shinyUI(fluidPage(


titlePanel(h1("Which NFL Player Are You Most Like?", align = "center")),


sidebarLayout(
sidebarPanel(

selectInput("team", "Choose a Team:",
choices = c("All","49ers","Bears","Bengals", "Bills",
"Broncos","Browns","Buccaneers", "Cardinals",
"Charges","Cheifs","Colts", "Cowboys",
"Dolphins","Eagles","Falcons", "Giants",
"Jaguars","Jets","Lions", "Packers",
"Panthers","Patriots","Raiders", "Rams",
"Ravens","Redskins","Saints", "Seahawks",
"Steelers","Texans","Titans", "Vikings")),



selectInput("Position", "What is your Football Position?:",
choices = c("All","C", "CB", "DE", "DT", "FB", "G", "K", "LB", "LS", "NT", "P", "QB", "RB",
"S", "T", "TE", "WR")),

numericInput("Weight", "How Much Do You Weigh? (LBS):", NA)


),

mainPanel(
p("Use the panel at the right to input your own information. Below will be the name or names of players you are most like. If you only want to use
a subset of variables, leave the others blank or set to 'All'."),

#Right now I just want to know that it worked, so show me the updated dataset where there is the Weight_diff column
tableOutput("view")

)
)
))

server.R

library(shiny)
NFLdat<-structure(list(Team = structure(c(10L, 4L, 6L, 9L, 6L, 30L, 12L,
22L, 31L, 6L), .Label = c("49ers", "Bears", "Bengals", "Bills",
"Broncos", "Browns", "Buccaneers", "Cardinals", "Chargers", "Chiefs",
"Colts", "Cowboys", "Dolphins", "Eagles", "Falcons", "Giants",
"Jaguars", "Jets", "Lions", "Packers", "Panthers", "Patriots",
"Raiders", "Rams", "Ravens", "Redskins", "Saints", "Seahawks",
"Steelers", "Texans", "Titans", "Vikings"), class = "factor"),
POS = structure(c(7L, 7L, 2L, 4L, 16L, 8L, 8L, 4L, 7L, 17L
), .Label = c("C", "CB", "DE", "DT", "FB", "G", "K", "LB",
"LS", "NT", "P", "QB", "RB", "S", "T", "TE", "WR"), class = "factor"),
WT = c(175L, 225L, 175L, 300L, 220L, 262L, 234L, 309L, 218L,
167L)), .Names = c("Team", "POS", "WT"), row.names = c(NA,
10L), class = "data.frame")

shinyServer(function(input, output) {



observe({
if(is.na(input$Weight)) NFLdat$Weight_diff<-abs(data$WT-data$WT)
else NFLdat$Weight_diff<-abs(data$WT-((as.numeric(input$Weight))))
})


#Or alternatively
inputWT <- reactive({
input$Weight
})

NFLdat$Weight_diff2<-abs(data$WT-inputWT)

output$view <- renderTable({
NFLdat
})



output$new <-inputWT
})

谢谢!!!

最佳答案

您可能想要创建一个响应式表达式,该表达式返回准备好呈现的 NFLdat 子集。我猜你的 shinyServer 应该是这样的:

 shinyServer(function(input, output) {

NFLdat.2show <- reactive({
#browser()
if (is.na(input$Weight)) return()

bmask.team <- if (input$team!='All') NFLdat$Team==input$team else TRUE
d <- NFLdat[bmask.team,]
d$Weight_diff <- abs(d$WT-input$Weight)

d
})

output$view <- renderTable({
NFLdat.2show()
})

})

关于r - 使用 Shiny Inputs 在数据集中创建新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27827338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com