【问题标题】:Generating word report in shiny using officeR使用officeR在闪亮中生成word报告
【发布时间】:2020-01-18 00:40:37
【问题描述】:

我想用shiny将一组日期和天数作为输入,计算这些天之间的天数,然后在word文档中生成这些操作的输出。代码在我只使用 R 时有效,但是当我尝试以闪亮的方式运行它时,它失败了 - 因为没有生成文档。

我当前的代码如下。

####Setup####
  library(shiny)  
  library(needs)
  library(tidyverse)
  library(lubridate)
  library(officer)
  library(ical)


####Generate page####
ui <- fluidPage(
  titlePanel("Shiny Example"),

  sidebarLayout(
    sidebarPanel(
      #Input for Date Range
      dateInput("date_begin", label = h3("Start Date"), 
                language= "de",  
                format = "dd-mm-yyyy"),
      dateInput("date_end", label = h3("End Date"), 
                language= "de",
                format = "dd-mm-yyyy"),

      #Week Days 
      checkboxGroupInput("weekdays", label = h3("Weekdays"), 
      choices = list("Monday" = 2, 
                     "Tuesday" = 3, 
                     "Wednesday" = 4, 
                     "Thursday" = 5, 
                     "Friday" = 6, 
                     "Saturday" = 7, 
                     "Sunday" = 1), hr())
      ),

    mainPanel(
      textOutput("begin"),
      verbatimTextOutput("Days"),
      downloadButton('downloadData', 'download')

    )
  )
)

server <- function(input, output) {

  ####Calculations####
  #Find all days between the dates

  output$begin <- renderText({ 
    paste("Days between", 
          input$date_begin,  "and", 
          input$date_end, "statt")
  })

   output$downloadData <- downloadHandler(
     filename = function(){"download.docx"},

     content = function(file) {

      ###Generate Date Vector###
       #Get Dates
       myDates <-seq(from = input$date_begin, to = output$date_end, by = "days")
       #Identify Date of Weekday
       select.vec<-which(wday(myDates) %in% c(output$weekdays))
       #Subset Data by Weekday
       date.vec<-myDates[select.vec]

       #Generate Number Vector
       number.vec<-seq(1,length=length(date.vec))

       #Generate Vector of Heading Names (German Format)
       name.vec1<-paste("Day",number.vec,sep=" ")

      ###Generate File###
          doc.full<-read_docx("")     

    #Write date in for loop
    for (i in 1:length(date.vec)){
      #Generate German output for date
      temp.date<-paste(day(date.vec[i]),
                       month(date.vec[i]),
                       year(date.vec[i]), 
                       sep=".")
      doc.full<-doc.full %>% 
      body_add_par(value = name.vec1[i], style =  "Sitzung") %>% 
      body_add_par(value = temp.date, style =  "Datum") 
    }

    #Write Output
          doc.full%>% 
            print(target = file)
    }
   )

}

# Run the application 
shinyApp(ui = ui, server = server)


【问题讨论】:

    标签: r shiny officer


    【解决方案1】:

    有几个错误。

      #Get Dates
      myDates <-seq(from = input$date_begin, to = output$date_end, by = "days")
      #Identify Date of Weekday
      select.vec<-which(wday(myDates) %in% c(output$weekdays))
    

    替换为

      #Get Dates
      myDates <-seq(from = input$date_begin, to = input$date_end, by = "days")
      #Identify Date of Weekday
      select.vec<-which(wday(myDates) %in% c(input$weekdays))
    

    接下来,read_docx("") 不起作用,因为没有文件 ""。要使用默认模板,请执行read_docx()

    但是如果你使用默认模板,那么

        doc.full <- doc.full %>% 
          body_add_par(value = name.vec1[i], style =  "Sitzung") %>% 
          body_add_par(value = temp.date, style =  "Datum") 
    

    不起作用,因为默认模板中没有样式SitzungDatum

    【讨论】:

    • 成功了 - 谢谢!
    猜你喜欢
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2021-07-31
    • 2019-08-22
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多