Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 闪亮应用程序:根据从用户界面发送到服务器的变量绘制数据,其中一个变量是日期_R_Shiny_Shinyapps - Fatal编程技术网

R 闪亮应用程序:根据从用户界面发送到服务器的变量绘制数据,其中一个变量是日期

R 闪亮应用程序:根据从用户界面发送到服务器的变量绘制数据,其中一个变量是日期,r,shiny,shinyapps,R,Shiny,Shinyapps,我正在尝试更改shinyApp,因此我添加了两个输入,以便在需要绘制数据时进行选择。我的数据中有一列是日期。我无法绘制数据图表。我试图考虑日期字段,但ShinyApp变量类型可能有问题,因为我试图在一个单独的示例中绘制数据,结果成功了。我注意到最初的示例使用粘贴来格式化用户界面发送到服务器的input$x,使用函数regFormula()。我试图改变结构,甚至摆脱它。仍然有错误。此外,我还尝试使用zoo软件包,该软件包在应用程序之外的单独示例中再次起作用 以下是使用dput的数据帧: stru

我正在尝试更改shinyApp,因此我添加了两个输入,以便在需要绘制数据时进行选择。我的数据中有一列是日期。我无法绘制数据图表。我试图考虑日期字段,但ShinyApp变量类型可能有问题,因为我试图在一个单独的示例中绘制数据,结果成功了。我注意到最初的示例使用粘贴来格式化用户界面发送到服务器的
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: 服务器.R:
功能(输入、输出){

regFormula下面是一个要测试的示例-认为这可能有助于可视化

添加了一行以更改
日期
格式和
as.Date
-如果加载文件后需要转换,可以将其移动到
反应式
表达式

我有两个示例来显示绘图-一个是使用
regFormula
使用
重新格式化
来创建函数,另一个是使用可选的
反应式
表达式,以防您要选择特定变量或进行其他操作

听起来您可能想尝试将
日期设置为默认值。这可以通过在
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)
您需要的不是
plotOutput
,而是:

library(plotly)
plotlyOutput("regPlot1")
对于其中一个绘图,可以包括
renderPlotly

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

output$regPlot1一些突出的问题-您需要使用括号来访问数据,方法是执行此操作
df[input$y]
。但是,这将返回一个
data.frame
。要获取值,您需要索引第一列(也是唯一一列)
df[input$y][,1]
。类似地
as.Date(df$(input$x),%d/%m/%y”)
应为
as.Date(df[输入$x],格式=“%d/%m/%Y”)
。请注意
%Y
而不是
%Y
。将Y大写表示4位数的年份格式,您已经有了。小写的Y表示2位数的年份格式。感谢您指出问题。我应用了您的评论,我得到了:'as.Date中的错误。默认值:不知道如何将'x'转换为类“Date”'您是否尝试在应用程序中将x输入值设置为'Date'?是的,但在我从下拉列表运行应用程序后。我不知道如何更改默认加载x和y。它会拾取第一个值。我收到错误:[:类型为'Close'的对象中的错误不是子附件我们可以在聊天中讨论:
plotlyOutput("regPlot1")
output$regPlot1 <- renderPlotly({
  ggplotly(ggplot(data = myData(), aes_string(x = input$x, y = input$y)) +
             geom_point(size = 1, color = "blue"))
})