我正在尝试创建一个Shiny交互式Web应用程序,但是我有一个与绘图相关的渲染问题。我的目标是上传一个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
有人可以帮助我吗?
答案 0 :(得分:0)
尝试一些建议:
确保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")
})
}