【问题标题】:Shiny dataframe column selection and plotting闪亮的数据框列选择和绘图
【发布时间】:2019-11-11 15:18:24
【问题描述】:

我正在尝试创建一个闪亮的交互式网络应用程序,但我遇到了与绘图相关的渲染问题。我的目标是上传一个 csv 文件,可以选择要绘制的列(在 y 轴上),并且可以选择绘制数据的数据范围。这是我的代码:

library(dplyr)
library(shiny)
library(networkD3)
library(igraph)
library(ggplot2)

db = as.data.frame(read.csv("./util_df.csv"))

EmotionalDB = as.data.frame(read.csv("./MainDB.csv"))
my_list = as.list(names(EmotionalDB))
my_list = my_list[c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)]

ui <- fluidPage(
  column(12, wellPanel(
    dateRangeInput('dateRangeEmotions',
                   label = 'Filter emotions by date',
                   start = as.Date('2019-05-20') , 
                   end = as.Date('2019-05-26')
    )
  )),
  
  selectInput("data1",
              label = "Choose an Emotion",
              choices = my_list
              
  ),
  
  plotOutput("Emotions")
)

server <- function(input, output, session) {
 
  output$Emotions <- renderPlot({
    x <- EmotionalDB$Date
    y <- EmotionalDB$Anger
    plot(main="Emotions", x, y, type="l", xlim=c(input$dateRangeEmotions[1],input$dateRangeEmotions[2]), xaxt = "n")
    axis.Date(side = 1, at = x, format = "%Y-%m-%d")
  })
}

shinyApp(ui = ui, server = server)

不幸的是,情节中没有输出。这是一张图片:

这是我的数据集的示例:

"Date","Anger","Anticipation","Disgust","Fear","Joy","Negative","Positive","Sadness","Surprise","Trust"
"2019-05-20",12521,14652,2687,5164,13085,18309,23214,12882,12091,18623
"2019-05-21",13073,14988,3170,5773,13191,18988,24747,12973,12005,19435
"2019-05-22",15085,18608,3428,6475,16354,22671,30028,15765,15347,23680
"2019-05-23",23586,32597,5092,10084,24827,34827,44475,24468,23021,35440
"2019-05-24",61955,74395,10963,19597,65097,88223,104236,67361,59611,86375
"2019-05-25",19017,23540,4170,8595,19640,29740,34746,21793,18817,27907
"2019-05-26",9379,11909,1849,4535,10046,14791,17525,10757,9306,14095

谁能帮帮我?

【问题讨论】:

  • 您能否提供示例数据以使其可重现(例如,您的 csv 文件中的 EmotionalDB 样本)?
  • 当然!!我已经添加了一个示例。

标签: r plot shiny


【解决方案1】:

几个可以尝试的建议:

确保 EmotionalDB 中的 Date 变量是 Date 类型。

EmotionalDB$Date <- as.Date(EmotionalDB$Date)

尝试使用反应式根据情绪和 2 个日期的输入选择来对数据进行子集化。

server <- function(input, output, session) {

  selectedData <- reactive({
    subset(EmotionalDB[ , c("Date", input$data1)], Date >= input$dateRangeEmotions[1] & Date <= input$dateRangeEmotions[2])
  })

  output$Emotions <- renderPlot({
    sd <- selectedData()
    plot(sd, main="Emotions", type="l", xaxt = "n")
    axis.Date(side = 1, at = sd$Date, format = "%Y-%m-%d")
  })
}

【讨论】:

    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 2018-10-01
    • 2018-09-20
    • 2017-03-07
    • 2018-05-04
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多