代码之家  ›  专栏  ›  技术社区  ›  SeGa

如何在文字中与官员形象作边界

  •  0
  • SeGa  · 技术社区  · 5 年前

    右边是传单地图。

    到目前为止我能做到,但我也希望 做一个边界

    在我的例子中,边框只有图像的一半大小,因此看起来只有左侧有边框。

    我怎样才能使边界像图像一样大?

    library(shiny)
    library(officer)
    library(leaflet)
    library(mapview)
    
    ui <- fluidPage(
      leafletOutput("leafletmap"),
      downloadLink("downloadWord", "Download Word Docx")
    )
    
    getLeafletMap <- function() {
      leaflet() %>%
        addTiles() %>% 
        addPopups(-93.65, 42.0285, "Here is the <b>Department of Statistics</b>, ISU")
    }
    
    server <- function(input, output, session) {
      output$leafletmap <- renderLeaflet({
        getLeafletMap()
      })
    
      output$downloadWord <- downloadHandler(
        filename = 'Report.docx',
        content = function(file) {
          ## Map #########################
          map <- getLeafletMap()
          mapshot(x = map, file=paste0(getwd(), "/map.png"),
                  remove_controls = c("layersControl"))
    
          ## Title & Texts #########################
          subtitle <- "Report Map"
          str5 <- "Lorem ipsum dolor sit amet, ligula iaculis mollis lacus consectetur, urna vitae potenti tortor!
          Sit commodo, venenatis leo at et. Ligula ac pulvinar sollicitudin gravida, lobortis lectus ligula et.
          Nisl tristique est erat lectus. Sodales egestas amet ac, ultricies nulla eu, risus blandit."
    
          ## Styles #########################
          text_style_title <- fp_text(font.size = 20, bold = FALSE, font.family = "Arial", color = "#808080")
          text_style <- fp_text(font.size = 10, bold = FALSE, font.family = "Arial")
          par_style <- fp_par(text.align = "justify")
    
          ## Make Word Docs #########################
          doc <- read_docx() %>%
            body_add_fpar(fpar(ftext("Report with Map", prop = text_style_title), fp_p = par_style)) %>%
            body_add_par("", style = "Normal") %>% # blank paragraph
            body_end_section_continuous() %>%
    
            body_add_fpar(fpar(ftext(str5, prop = text_style), fp_p = par_style)) %>%
            body_add_fpar(fpar(external_img(src = paste0(getwd(), "/map.png"), height = 3, width = 4.52),
                               fp_p = fp_par(text.align = "right", padding.top = 6,
                                             border = fp_border(width = 1, color = "red")))) %>%
            body_end_section_columns(widths = c(1.5,2), sep = FALSE, space = 0.2) %>%
            print(doc, target = file)
        }
      )
    }
    
    shinyApp(ui, server)
    
    0 回复  |  直到 5 年前
        1
  •  4
  •   Sven    5 年前

    我能做到的 flextable , vline hline . 一个免责声明,因为我不能使用 hline公司 对于 i=0 i=0 .

    library(flextable)
    library(officer)
    
    inputFolder <- "test/"
    std_border = fp_border(color="orange")
    
    testtable <- data.frame("text" = c("",""), "image" = c("",""))
    
    testft <- flextable(testtable) %>%
      delete_part(part = "header") %>%
      theme_box() %>%
      compose(i = 2, j = 2, value= as_paragraph(as_image(src = paste(inputFolder, "smiley.png", sep = ""), width = 1, height = 1))) %>%
      compose(i = 2, j = 1, value = as_paragraph(as_chunk("Just some random text"))) %>%
      border_remove() %>%
      vline(i=2, j=1, border = std_border) %>%
      vline(i=2, j=2, border = std_border) %>%
      hline(i=1, j=2, border = std_border) %>%
      hline(i=2, j=2, border = std_border)
    

    这给了我以下信息:

    enter image description here

    当然,你可以根据需要设置文本、图像和边框的格式。

    编辑:

    当然是 officer 部分只是:

    doc <- read_docx() %>%
    body_add_flextable(testft)