kable() and gt() copy to clipboard giving weird screenshot - r

I am trying to use the Export -> Copy to Clipboard option after making a kable() or gt() table. When I go to paste elsewhere, the clipboard image taken is a weird mix of my RStudio background with only part of the table. It is like my computer is clipping the wrong part of my screen. I have double checked and this does not happen when I try to do the same thing with ggplot(). I've attached my code as an example and what I receive as my output. I have instead been saving these plots as images (and this works fine!) but I really don't need these plots/just would like to copy them.
hectare <- PlantGrowth %>%
mutate(hectare = weight/10000)
partbtab <- hectare %>%
group_by(group) %>%
summarise("Mean/Yr" = mean(hectare),
"25 Year Average" = `Mean/Yr`*25)
kable(partbtab) %>%
kableExtra::kable_styling()
#this also does the weird screenshot
gt(partbtab)
This is what the copied "plot" ends up as:

Related

print_md in huxtable changes table formatting

I am using the huxtable package to create tables in a PDF rendered in bookdown. The table is formatted exactly the way I want it, up until I run the print_md command, after which a border is moved up row from underneath the column names to underneath the header. Also, the header is moved from a centered position to right-aligned. Check it out:
df <- data.frame(
"colname1" = c("something indicator"),
"colname2" = "[Something](http://www.overleaf.com)",
"colname3" = "[Something again](http://www.overleaf.com)")
df <- df %>%
as_hux() %>%
theme_basic() %>%
set_tb_padding(2)
df <- df %>%
set_contents(1, 2:3, c("colname2", "colname3")) %>%
insert_row("", "Header", "Header", after = 0) %>%
merge_cells(1, 2:3) %>%
set_align(1, everywhere, "center") %>%
set_tb_padding(1, everywhere, 0) %>%
set_bold(1, everywhere)
df
Which gives:
Table is formatted correctly. But. You'll notice that the URLs are not formatted correctly. It should only be showing the part within the brackets, which when clicked will take you to the site in parentheses.
This can be remedied with the following code:
df %>% print_md()
Which gives:
Now the URLs look like they should, but the border has erroneously moved up a row, and "Header" is now right-aligned instead of center-aligned. How do I stop that from happening?
Don't ask me why it works. But changing print_md() to set_markdown() fixed both the border and alignment problems.
EDIT: I'm adding #dash2's comment to this answer.
The reason print_md() was causing problems is because it converts the table to markdown format, which R Markdown then reads and produces a table from. So some features (alignment) get lost in translation. It'd be better to print the table in the intended output format, be it Latex, HTML or whatever you're using, instead of markdown.
But the cells with markdown hyperlinks need to be respected still - print_md() is just the wrong way to go about it. Instead, use set_markdown(). This will ensure that, within huxtable itself, cells with markdown code are interpreted as markdown before the table is printed. The printed table will then retain the intended format.
Thank you #dash2 for creating such a powerful package!

how do I make this a neat table

UNRAVEL_study %>%
group_by(sleep_dep) %>%
summarise(mean=mean(unravel), SD=sd(unravel), min=min(unravel), max=max(unravel), IQR=IQR(unravel))
how do I get a presentable table that gives me the same info as this code chunk? im trig to do a presentation for class and we can't show any code
Ok, this is how I fixed it:
UNRAVEL_study %>%
group_by(sleep_dep) %>%
summarise(mean=mean(unravel), SD=sd(unravel), min=min(unravel), max=max(unravel), IQR=IQR(unravel))%>%
kable(., caption = "sleep deprivation descriptive statistics")
I used the Kable function which makes it into a really tidy table. Then up top I added the directions echo=FALSE to not include the code, and message=FALSE to not include the R console

How to be able to look at more than one window of data in Rstudio when apply View()?

Every time I apply View() on any data, it replaces the actual window of data from previous View(). I assume it should be some configuration on Rstudio, but I don't know how and on the internet it's not a common problem.
You can specify the title of the View() window with the title option, e.g.:
View(mtcars, title = "new_window")
Of if you use a pipe:
mtcars %>%
View(., title = "new_window")
Just make sure that your windows have different titles.

FlexTable output .docx horizontal table adjusted to narrow margins

I have a flextable object called html_table which I want to directly put in a word document, in horizontal layout with narrow margins.
I face 2 problems:
1) The approach suggested in the vignette produces extra pages (one before, one after the table). I think this is a known issue but not clear how to solve it.
2) I would like to have narrow margins and the resulting table on horizontal pages to be automatically fitted to the page. I want this so that I can print the table using as much page as I can. My current approach is to manually open the document, change the layout and select "autofit" on Word.
Here's the code I'm using to produce the document. For illustrative purposes, I will use mtcars for my table, but the real one has more rows than mtcars.
html_table <- regulartable(mtcars)
doc <- read_docx() %>%
# Make it landscape
body_end_section_continuous() %>%
# Add the table
body_add_flextable(value = html_table,
split = TRUE
) %>%
body_end_section_landscape()
# Write the .docx
print( doc, target = "my_table.docx" )
In Word document, sections are only defined when they stop (I can not explain why it has been made that way but this is how the underlying xml is...). Also a landscape oriented section need a page break if the preceding section is not landscape oriented.
To autofit a flextable, use function autofit.
library(flextable)
library(officer)
library(magrittr)
html_table <- regulartable(mtcars) %>%
autofit()
doc <- read_docx() %>%
body_add_flextable(value = html_table, split = TRUE) %>%
body_end_section_landscape() %>% # a landscape section is ending here
print( target = "my_table.docx" )
If you don't want extra page, you will need a template with a default page orientation as landscape. Also, you would not need any code to manage orientation nor margins.

RStudio : Refresh viewer output at runtime when plotting graphs

I am running the following loop in a function:
for (i in 1:400) {
m<-update_values() # Updates values in the dataframe
dygraph(m[,1:4]) %>%
dyCandlestick() %>%
dyRangeSelector()
Sys.sleep(1)
}
The problem is that RStudio's viewer is locked while the function runs and the plot is not even displayed when the function returns (I can only manually plot the collected data afterwards). I would want the plot to be displayed at each step. Any idea how to achieve this?
Edit: This function monitors sensors in real time, so it needs to plot at runtime.
The dygraph function uses an HTML widget, so the result needs to be printed to appear in the viewer. Just add %>% print() at the end and the output should appear, i.e.
for (i in 1:400) {
m<-update_values() # Updates values in the dataframe
dygraph(m[,1:4]) %>%
dyCandlestick() %>%
dyRangeSelector() %>%
print()
Sys.sleep(1)
}
However, a disadvantage of this approach is that you'll end up with 400 pages in the viewer. As far as I know there's no way to say to replace the current view, you can just add new ones. Maybe rstudioapi has a "Delete viewer page" function, but I don't see it.

Resources