r-shine应用程序:根据从UI发送到服务器的变量绘制数据,其中一个变量是日期



我正在尝试更改这里的shinyApp,所以我添加了两个输入,以便在您想要绘制数据图时进行选择。我的数据中有一列是日期。我没能把数据画出来。我试图考虑日期字段,但这可能是ShinyApp变量类型的问题,因为我试图将数据绘制在一个单独的示例上,结果成功了。我注意到最初的示例是使用粘贴来格式化由UI发送到服务器的input$x,使用函数regFormula ()。我试着改变结构,甚至去掉它,但仍然有错误。此外,我尝试使用zoo软件包,该软件包在应用程序之外的另一个示例中再次起作用。

这是使用dput:的数据帧

structure(list(Dead = c(0L, 0L, 0L, 0L, 0L, 1L, 9L, 0L, 0L, 0L
), Case = c(120L, 70L, 50L, 40L, 39L, 20L, 18L, 13L, 9L, 2L), Recovered = c(30L, 
0L, 18L, 13L, 19L, 10L, 0L, 16L, 0L, 1L), Critical = c(0L, 0L, 0L, 
0L, 8L, 4L, 0L, 3L, 2L, 0L), Date = c("18/03/2020", "17/03/2020", 
"16/03/2020", "15/03/2020", "14/03/2020", "13/03/2020", "12/03/2020", 
"11/03/2020", "10/03/2020", "09/03/2020")), class = "data.frame", row.names = c(NA, 
10L))

如果使用str(df)从上述数据中检查日期,则日期字段将为chr类型。甚至我在加载数据之前也尝试过使用as.factor(df$Date)来更改它,但没有希望。

ui.R:

fluidPage(
title = 'Download a PDF report',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput('x', 'Choose X-axis data',
choices = names(dataMOH)),
selectInput('y', 'Choose Y-axis data',
choices = names(dataMOH)),
radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport') ),
mainPanel(
plotOutput('regPlot')
)
)
)

服务器.R:

function(input, output) {
regFormula <- reactive({
# as.formula(paste(input$x,'~ Date'))
as.formula(paste(input$x,input$y))
})
output$regPlot <- renderPlot({
par(mar = c(4, 4, .1, .1)) # margin lines
#  plot(regFormula(), data = df, pch = 19) #df : dataframe name 
#  plot(zoo(df$Case,as.Date(df$Date,"%d/%m/%y")))
plot(zoo(df$(input$y),as.Date(df$(input$x),"%d/%m/%y")))
})
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-Report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- normalizePath('report.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()))
file.rename(out, file)
}
)
}

我在这里缺少什么?

这里有一个测试示例,认为这可能有助于可视化。

添加了一行以使用as.Date更改Date格式-如果加载文件后需要转换,则可以将其移动到reactive表达式。

我有两个例子来显示绘图——一个是regFormula使用reformulate创建函数,另一个是使用可选的reactive表达式,以防您想要选择特定变量或进行其他操作。

听起来您可能想尝试将Date设置为默认值。这可以通过在selectInput中使用selected来完成。

希望这能有所帮助。

library(shiny)
dataMOH$Date = as.Date(dataMOH$Date, format = "%d/%m/%Y")
ui <- fluidPage(
title = 'Download a PDF report',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput("x", "Choose X-axis data", choices = names(dataMOH), selected = "Date"),
selectInput("y", "Choose Y-axis data", choices = names(dataMOH)),
radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'), inline = TRUE),
downloadButton('downloadReport')
),
mainPanel(
tabsetPanel(
tabPanel("Plot 1", plotOutput("regPlot1")),
tabPanel("Plot 2", plotOutput("regPlot2"))
)
)
)
)
server <- function(input, output, session) {
regFormula <- reactive({
reformulate(termlabels = input$x, response = input$y)
})
myData <- reactive({
dataMOH[, c(input$x, input$y)]
})
output$regPlot1 <- renderPlot({
par(mar = c(4, 4, .1, .1)) # margin lines
plot(myData())
})
output$regPlot2 <- renderPlot({
par(mar = c(4, 4, .1, .1)) # margin lines
plot(regFormula(), data = dataMOH)
})
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-Report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- normalizePath('report.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()))
file.rename(out, file)
}
)
}
shinyApp(ui, server)

对于plotly图,您需要:

library(plotly)

您需要的不是plotOutput

plotlyOutput("regPlot1")

对于其中一个绘图,可以包含renderPlotly:

output$regPlot1 <- renderPlotly({
ggplotly(ggplot(data = myData(), aes_string(x = input$x, y = input$y)) +
geom_point(size = 1, color = "blue"))
})

最新更新