r - 在 Shiny 的 react 结构中子集数据帧

标签 r shiny

我无法在 react 性 Shiny 结构中对数据帧进行子集化并将其显示为表格。如果我尝试只显示数据框,我可以但无法进行子集化和显示。我相信它必须以这种方式做点什么input$用来。

请帮忙,我对 Shiny 很陌生

数据集

dput(b)
structure(list(Date = c("1-Jan", "2-Jan", "3-Jan"), Month = c("Jan", 
"Jan", "Jan"), Days = c("Thu", "Fri", "Sat"), A = c(30712L, 26842L, 
21640L), B = c(26505L, 25906L, 22929L), C = c(22128L, 26814L, 
22091L), D = c(30994L, 23935L, 20048L), E = c("38%", "51%", "37%"
), F = c(71L, 70L, 71L), G = c(91L, 114L, 104L), H = c(77L, 98L, 
91L), I = c(-4621L, -463L, 291L), J = c("-32.00%", "-3.30%", 
"2.00%")), .Names = c("Date", "Month", "Days", "A", "B", "C", 
"D", "E", "F", "G", "H", "I", "J"), class = "data.frame", row.names = c(NA, 
-3L))

ui.R
library(shiny)
library(shinythemes)

shinyUI(fluidPage(                  
  sidebarLayout(   
    sidebarPanel(
      selectInput(inputId = "col", label = "Columns",choices = colnames(b[4:8]),selected = colnames(b[4]))),

    mainPanel(dataTableOutput(outputId = "table")))))

server.R
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)

b<-read.csv("newb.csv",header=TRUE,sep=",",stringsAsFactors=FALSE)

shinyServer(function(input, output) {

  datum<-reactive({
    d<-b[,input$col]
    return(d)
  })

  output$table<-renderDataTable({datum()})

 })

最佳答案

发生这种情况是因为您只选择了 b 的一列数据框。这个子集的结果是一个向量,而不是一个数据框,所以 renderDataTable无法渲染它。

您可以在响应式(Reactive)表达式中将向量转换为数据框:

  datum<-reactive({
    d <- b[,input$col]
    d <- as.data.frame(d)
    colnames(d) <- input$col
    d
  })

使用@docendo discimus 的评论更简单的解决方案:
datum<-reactive({ d<-b[,input$col,drop=FALSE] 
d })

关于r - 在 Shiny 的 react 结构中子集数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29299827/

相关文章:

javascript - shiny点击DT后弹窗

R Shiny : how to change values in a reactiveValues object

R:source() 和源文件的路径

r - makePSOCKcluster(names = spec, ...) 错误 : Cluster setup failed. 3 个工作人员中的 3 个无法连接

使用 RMarkdown 将带有颜色和 unicode 字符的 kableExtra 表渲染为 PDF

r - abline() 的问题

r - 将行值(文本)与列名和返回值匹配

html - 如何将 Shiny 的应用程序嵌入到 Rmarkdown html 文档中

web-services - 如何保存(不上传)从 Shiny 界面创建的文件?

r - 使用鼠标点击 Shiny 的交互式绘图