有条件地在数据帧中子集和计算一个新的变量 [英] Conditionally subsetting and calculating a new variable in dataframe in shiny

查看:85
本文介绍了有条件地在数据帧中子集和计算一个新的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试在Shiny中计算一个新变量,这个变量是有条件地计算在另一个变量上的。做:

  mydata<  -  data.frame(cbind(x = 1,y = 1:10))
值< - 10#from user input
mydata $ z [mydata $ y> = 5]< - mydata $ y [mydata $ y> = 5] +值
mydata $ z [mydata $ y < 5]< - mydata $ y [mydata $ y< 5] - 值

这是我的ui.R文件:

  #Library 
库(闪亮)

#定义UI每加仑应用程序里程
shinyUI(pageWithSidebar

#应用程序标题
headerPanel(Test),

sidebarPanel(
numericInput(value,输入值:,10)
),

mainPanel(
tableOutput(table)



这是我的server.R文件:

  #Libraries 
库(闪亮)

#Load Data
mydata< - data.frame(cbind(x = 1,y = 1:10))

#定义服务器逻辑
ShinyServer(function(input,output){

mydata $ z [mydata $ y> = 5]< - reactive({
mydata $ y + input $ value
})

mydata $ z [mydata $ y <5]< - reactive({
mydata $ y - input $值
})

输出$ table< - renderTable({
mydata
})

})

使用这个Shiny代码,我收到以下错误:


向量分配中的mydata $ z [mydata $ y> = 5]中的错误

blockquote>

我尝试过不同的子集和分配方式,但我被困住了。你的帮助是非常感谢!

解决方案

我不知道你清楚什么 ()正在做...我建议您阅读更多闪亮的文档,特别是查看示例 - '03_reactivity'可能会是一个很好的。



无论如何,这里是你的代码如何适应你想做的。这不是最好的方式,但希望它能帮助您看到目前存在的问题。



我只更改了 server.R 文件,并且没有更改任何代码来构建新变量。构造新变量的代码位于反应式函数内,然后需要调用该函数来实际创建表。

  #Libraries 
库(闪亮)

#Load Data
mydata < - data.frame(cbind(x = 1,y = 1:10))

#定义服务器逻辑
shinyServer(function(input,output){

newdata< - reactive({
mydata $ z [mydata $ y> = 5]< - mydata $ y [mydata $ y> = 5] + input $ value
mydata $ z [mydata $ y< 5]< - mydata $ y [mydata $ y <5] - 输入$ value
return(mydata)
})

输出$ table< - renderTable({
newdata()
})

})


I am trying to calculate a new variable in a dataframe in Shiny that is calculated conditionally on another variable.

Here's a small example of what I'm trying to do:

mydata <- data.frame(cbind(x = 1, y = 1:10))
value <- 10 #from user input
mydata$z[mydata$y >= 5] <- mydata$y[mydata$y >= 5] + value
mydata$z[mydata$y < 5] <- mydata$y[mydata$y < 5] - value

Here's my ui.R file:

#Library
library("shiny")

# Define UI for miles per gallon application
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Test"),

  sidebarPanel(
    numericInput("value", "Enter Value:", 10)
  ),

  mainPanel(
    tableOutput("table")
  )
)
)

Here's my server.R file:

#Libraries
library(shiny)

#Load Data
mydata <- data.frame(cbind(x = 1, y = 1:10))

# Define server logic
shinyServer(function(input, output) {

  mydata$z[mydata$y >= 5] <- reactive({
    mydata$y + input$value
  })

  mydata$z[mydata$y < 5] <- reactive({
    mydata$y - input$value
  })

  output$table <- renderTable({
    mydata
  })

})

With this Shiny code, I receive the following error:

Error in mydata$z[mydata$y >= 5] <- reactive({ : invalid type/length (closure/0) in vector allocation

I've tried different ways of subsetting and assigning, but I'm stuck. Your help is greatly appreciated!

解决方案

I'm not sure that you're clear on what reactive() is doing... I'd recommend reading a bit more of the shiny documentation, and in particular to have a look at the examples - '03_reactivity' would probably be a good one.

Regardless, here is how your code can be adapted to do what you want. It's not the best way to do this, but hopefully it will help you see where there are problems at the moment.

I've only changed the server.R file, and haven't changed any of the code for how you're constructing the new variable. The code to construct the new variable goes inside a reactive function - you then need to call the function to actually make the the table.

#Libraries
library(shiny)

#Load Data
mydata <- data.frame(cbind(x = 1, y = 1:10))

# Define server logic
shinyServer(function(input, output) {

  newdata <- reactive({
    mydata$z[mydata$y >= 5] <- mydata$y[mydata$y >= 5] + input$value
    mydata$z[mydata$y < 5] <- mydata$y[mydata$y < 5] - input$value
    return(mydata)
  })

  output$table <- renderTable({
   newdata()
  })

})

这篇关于有条件地在数据帧中子集和计算一个新的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆