0

I have this automated script that produces a table with frequencies of "thetarget" tokens by year:

library(quanteda)

vec <- c("Apple", "Google")    

out <- map(vec, ~ 
         df %>%
  filter(str_detect(collectionName, .x)) %>%
  filter(str_detect(Year, paste(years, collapse = "|"))) %>%
  corpus(text_field = "text") %>%
  tokens() %>%
  tokens_select(thetarget) %>%
  dfm() %>%
  dfm_group(groups = "Year") %>%
  convert(to = "data.frame")
)

names(out) <- sub("^(...).*\\s+(\\S)$", "\\1\\2", vec)

Using

 View(out$Apple)

Produces the corresponding table.

I am trying to automate the export of these tables as a pdf or jpeg with the name of the file being "Apple" for example.

Is there a way to do this?

TIA

J.Doe
  • 165
  • 7
  • rmarkdown has a pdf output type, which can take dynamic file names (https://bookdown.org/yihui/rmarkdown-cookbook/dynamic-yaml.html). Have a look at `KableExtra` or `gt` packages for pretty tables. As for saving data.frames as png, see https://stackoverflow.com/questions/23365096/r-save-table-as-image – Donald Seinen Oct 16 '21 at 08:30

1 Answers1

0

I can't execute your code chunk (problem with function convert, from which library is it?) - but it isn't a problem.

Exist a lot of solutions, but, f.e., you can use packages gt or flextable for this task (pile of output types).

First of all, install the webshot and PhantomJS. And after you can install other packages (gt and flextable). See examples:

(gt)

tab_1 <-
    gtcars %>%
    dplyr::select(model, year, hp, trq) %>%
    dplyr::slice(1:5) %>%
    gt(rowname_col = "model") %>%
    tab_stubhead(label = "car") %>%
    gtsave("tab_1.png", expand = 10, path = "********")

(flextable)

   ft <- flextable(head(mtcars))
   save_as_image(x = ft, path = "********\\image_name.png")
manro
  • 3,529
  • 2
  • 9
  • 22
  • Thanks for the comment. I am using the quanteda package for "convert". I install the "gt" package and could not get it to work. Do you know if I can use the "gt" script after this line "convert(to = "data.frame")"? – J.Doe Oct 16 '21 at 12:30
  • @J.Doe I tried now to execute your code, but had an error: ```Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "function"``` I need to read how to fix it. – manro Oct 16 '21 at 13:55
  • Which packages do use also: ```purrr and dplyr```? – manro Oct 16 '21 at 14:09
  • @J.Doe it is a conflict because of stats and dplyr ```filter```. I can't fix( You can give to me a fixed code or another one. – manro Oct 16 '21 at 15:18
  • I'm using dplyr. Not using stats (I think, I'm still novice at R). The script you suggested doesn't work with dplyr? – J.Doe Oct 18 '21 at 04:48
  • @J.Doe Yes, see an error above, because of ```dplyr::filter() masks stats::filter()``` . But after using ```stats::filter``` one can see a new error. Did you test the functionality of this code chunk? – manro Oct 18 '21 at 09:40
  • I've decided to export them as excel sheets and got it to work. Appreciate the help though! – J.Doe Oct 22 '21 at 10:01
  • @J.Doe No problems. I learned about a new interesting library ```quanteda```. It is useful for me too. – manro Oct 22 '21 at 12:16