闪亮的应用程序:根据从 UI 发送到服务器的变量绘制数据,其中一个变量是日期
shiny App : plot data based on sent variables from UI to server, one of the variable is date
我正在尝试更改 shinyApp here,因此我添加了两个输入以供您在绘制数据图表时选择。我的数据的一列是日期。我无法绘制数据图表。我试图分解日期字段,但这可能是 ShinyApp 变量类型的问题,因为我试图在一个单独的示例上绘制数据图并且它起作用了。我注意到原始示例使用函数 regFormula ()
使用粘贴来格式化 UI 发送到服务器的 input$x
。我试图改变结构,甚至摆脱它。仍然有错误。另外,我尝试使用 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')
)
)
)
server.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)
}
)
}
我在这里缺少什么?
这是一个测试示例 - 认为这可能有助于可视化。
添加了一行以将 Date
格式更改为 as.Date
- 如果在加载文件后需要转换,则可以将其移至 reactive
表达式。
我有两个示例来显示绘图 - 一个使用 regFormula
使用 reformulate
创建一个函数,第二个使用可选的 reactive
表达式以防你想要 select 特定变量或做其他操作。
听起来您可能想尝试将 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"))
})
我正在尝试更改 shinyApp here,因此我添加了两个输入以供您在绘制数据图表时选择。我的数据的一列是日期。我无法绘制数据图表。我试图分解日期字段,但这可能是 ShinyApp 变量类型的问题,因为我试图在一个单独的示例上绘制数据图并且它起作用了。我注意到原始示例使用函数 regFormula ()
使用粘贴来格式化 UI 发送到服务器的 input$x
。我试图改变结构,甚至摆脱它。仍然有错误。另外,我尝试使用 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')
)
)
)
server.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)
}
)
}
我在这里缺少什么?
这是一个测试示例 - 认为这可能有助于可视化。
添加了一行以将 Date
格式更改为 as.Date
- 如果在加载文件后需要转换,则可以将其移至 reactive
表达式。
我有两个示例来显示绘图 - 一个使用 regFormula
使用 reformulate
创建一个函数,第二个使用可选的 reactive
表达式以防你想要 select 特定变量或做其他操作。
听起来您可能想尝试将 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"))
})