r - 筛选 Shiny R中的 react 数据集

标签 r shiny dplyr data.table reactive-programming

我在过滤依赖于几个数字输入的 react 性数据集时遇到了一些麻烦。有什么办法可用吗?我在下面提供了类似的代码,该代码复制了我要实现的任务。

ui.R
library(shiny)
shinyUI(fluidPage(
  titlePanel("abc"),
  sidebarLayout(
    sidebarPanel(
      numericInput("first", "A",0),
      numericInput("second", "B",0),
      numericInput("third", "C",0),
      numericInput("fourth", "D",0),
      numericInput("fifth", "E",0),
      actionButton("mybutton","Submit")
    ),
    mainPanel(
      tableOutput("mytable")
      )
  )
  )) 



server.R

library(data.table)
library(shiny)
library(stats)
shinyServer(function(input, output) {
  a<-c("A","B","C","D","E","F","G")
  b<-c("10","20","30","40","50","60","70")
  ab<-data.frame(a,b)
  a<-c("A","B","C","D","E")
  input_vector<-reactive({
    c(input$first,input$second,input$third,input$fourth,input$fifth)
  })
  newdata<-reactive({
    data.frame(a,input_vector())
  })
  merged_data<-reactive({
    merge(newdata(),ab,by.newdata=a)
  })
  mutated_data<-reactive({
    library(dplyr)
    merged_data%>%                                                        #using merged()%>% gives error "Warning in Ops.factor(function ()  : ‘*’ not meaningful for factors"
      mutate(newvalue=input_vector*b)                                 #using merged%>% gives error "no applicable method for 'mutate_' applied to an object of class "reactive"
  })
  output$mytable<-renderTable({
    input$mybutton
    isolate(mutated_data())
  })

})

最佳答案

我认为这是您想要的:

library(data.table)
library(shiny)
library(stats)

shinyServer(function(input, output) {
  a<-c("A","B","C","D","E","F","G")
  #b<-c("10","20","30","40","50","60","70")
  b<-c(10,20,30,40,50,60,70)
  ab<-data.frame(a,b)
  a<-c("A","B","C","D","E")

  input_vector<-reactive({
    c(input$first,input$second,input$third,input$fourth,input$fifth)
  })
  newdata<-reactive({
    data.frame(a,input_vector())
  })
  merged_data<-reactive({
    merge(newdata(),ab,by.newdata=a)
  })
  mutated_data<-reactive({
    library(dplyr)
    mutate(merged_data(),newvalue=input_vector()*b)                       
  })

  output$mytable<-renderTable({
    input$mybutton
    isolate(mutated_data())
  })


})

enter image description here

关于r - 筛选 Shiny R中的 react 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27889231/

相关文章:

r - ggplot2-scale_colour_manual和scale_shape手动问题

r - 如何删除R中的连字符和中间数字

r - Shiny - 创建一个表格,其单元格可以通过单击打开和关闭

r - 从一个 df 绘制点,从另一个 df 绘制误差条

r - 以迭代方式在数据框中创建多个新列

r - 如何替换R中数据框中的值?

R - 将整数转换为日期

r - 如何根据使用 shinyWidgets 在 pickerInput 中键入字母来获得搜索选项?

r - Shiny 的 : How to modify reactive object

r - 计算与其他列的双重类别关联的列中的特定字符。根据频率仓迭代进行