How do I add currency to my valueBox, £ in this case.
The valueBox generated in the server as:
valueBox(Test_data %>%
filter(`Region` == input$Region, `Locality` == input$Locality) %>%
summarise("Annual_Value" = sum(`Value (Annualised)`)) %>%
prettyNum(big.mark = ","),
req(input$Region))
I just went into the windows explorer and found "Character Map". From there I was able to copy paste the symbol into R. Since you didn't provide a reprex, here's a simple response. Basically, calculate the value you need, feed it into paste for the value in the valueBox.
calc_value <- 200
valueBox(value = paste('£', calc_value), subtitle = '')
Related
I'm using the officer package to automate powerpoint reporting but I am encountering the below error:
"Error in xml_ns.xml_document(x) : external pointer is not valid"
when I try to run the code below. Essentially I am trying to bold the word "specifically:" in line 4 of the code below. But when I run it I get that error message. I searched online and it seems to happen within other use cases such as this: Using rvest with drake: external pointer is not valid error
but I don't really understand how to incorporate their code chunk. Can anyone help?
#these 3 lines below create formatting such as bold, font size etc.
p_normal <- fp_text(font.size = 24)
fp_bold <- update(fp_normal, bold = TRUE) #created the bold option
fp_red <- update(fp_normal, color = "red")
#this line creates an object with the bold option incorporated
studyobjective1 <- block_list(fpar(ftext("Specifically:", fp_bold)),
fpar(ftext("red text", fp_red)))
#and this creates the powerpoint slides. I redacted some names but that shouldnt affect your understanding
#as they are text names not functions etc
#begin by adding slides now that we have named all slides in template and placeholders
trial <- df %>%
add_slide(layout = "study objective", master = "rlayout") %>% #new slide
ph_with("Study Objective", location = ph_location_label(ph_label = "title")) %>%
ph_with("Survey Global Sales - Specifically:",
location = ph_location_label(ph_label = "center object")) %>%
ph_with(studyobjective1, location = ph_location_label(ph_label = "lower object")) %>%
print(target = "C:/Users/mi/Desktop/R/PPT/df.pptx") #save in new location
But when I print the powerpoint slide the word "specifically" isn't bolded which should be due to sudyobjective1 object containing the bold formatting. Instead I get the error in the title of this post. Any ideas?
In kableExtra >= 0.8.0, the canonical way to insert a linebreak into text piped into a table from a kableExtra function such as add_header_above or pack_rows is to add an \n directly.
However, this appears not to work with the escape = FALSE argument, which is required if the text also contains LaTeX code.
How can one force linebreaks in kableExtra functions with escape = FALSE?
library(dplyr)
library(knitr)
library(kableExtra)
starwars %>%
filter(species == 'Gungan' | species == 'Droid') %>%
arrange(species) %>%
select(name, eye_color) %>%
kbl(booktabs = TRUE) %>%
pack_rows(
index = c(
'The droids: everybody\'s favourite' = 6,
'The Gungans: only beloved of \nthose aged under $3^2$' = 3),
escape = FALSE)
ISSUE
The issue at hand is that you wish to escape part of your header (i.e., the break) and not escape another part (i.e., the math code).
Further Complications
This core issue is further complicated by a number of factors:
when and how kableExtra is programmed to deal with escaping
a desire to have a solution that works for both html and LaTeX output
when and how R evaluates code
A SOLUTION
Here is a solution that will work for both html and LaTeX output, but it is not as clean and straight forward as your original code:
# a new version of `kableExtra::linebreak()` that takes into account what type
# of output is desired as well as how much escaping is necessary
linebreak2 <- function(x, double_escape = TRUE, ...) {
# if LaTeX insert text into a `\makecell[]{}` command and double escape
if(knitr::is_latex_output())
return(linebreak(x, double_escape = double_escape, ...))
# if html output just replace `\n`s with `<br/>`s
if(knitr::is_html_output())
return(gsub("\n", "<br/>", x))
# let x pass through for other types of output
return(x)
}
# build the index named vector outside the pipe flow
# in order to set the names using `linebreak2()`
index <- c(6, 3)
names(index) <- c(
'The droids: everybody\'s favourite',
linebreak2('The Gungans: only beloved of \nthose aged under $3^2$')
)
# proceed as before
starwars %>%
filter(species == 'Gungan' | species == 'Droid') %>%
arrange(species) %>%
select(name, eye_color) %>%
kbl(booktabs = TRUE) %>%
pack_rows(index = index, escape = FALSE)
PDF Output
HTML Output
You could use html line break tag <br/>:
starwars %>%
filter(species == 'Gungan' | species == 'Droid') %>%
arrange(species) %>%
select(name, eye_color) %>%
kbl(booktabs = TRUE) %>%
pack_rows(
index = c(
'The droids: everybody\'s favourite' = 6,
'The Gungans: only beloved of <br/> those aged under $3^2$' = 3),
escape = FALSE)
I am trying to follow an Vignette "How to make a Markov Chain" (http://datafeedtoolbox.com/attribution-theory-the-two-best-models-for-algorithmic-marketing-attribution-implemented-in-apache-spark-and-r/).
This tutorial is interesting, because it is using the same data source as I use. But, a part of the code is using "Spark SQL code" (what I got back from my previous question Concat_ws() function in Sparklyr is missing).
My question: I googled a lot and tried to solve this by myself. But I have no idea how, since I don't know exactly what the data should look like (the author didn't gave an example of his DF before and after the function).
How can I transform this piece of code into "normal" R code (without using Spark) (especially: the concat_ws & collect_list functions are causing trouble
He is using this line of code:
channel_stacks = data_feed_tbl %>%
group_by(visitor_id, order_seq) %>%
summarize(
path = concat_ws(" > ", collect_list(mid_campaign)),
conversion = sum(conversion)
) %>% ungroup() %>%
group_by(path) %>%
summarize(
conversion = sum(conversion)
) %>%
filter(path != "") %>%
collect()
From my previous question, I know that we can replace a part of the code:
concat_ws() can be replaced the paste() function
But again, another part of code is jumping in:
collect_list() # describtion: Aggregate function: returns a list of objects with duplicates.
I hope that I described this question as clear as possible.
paste has the ability to collapse the string vector with a separator that is provided with the collapse parameter.
This can act as a drop in replacement for concat_ws(" > ", collect_list(mid_campaign))
channel_stacks = data_feed_tbl %>%
group_by(visitor_id, order_seq) %>%
summarize(
path = paste(mid_campaign, collapse = " > "),
conversion = sum(conversion)
) %>% ungroup() %>%
group_by(path) %>%
summarize(
conversion = sum(conversion)
) %>%
filter(path != "")
I was wondering if somebody knows any alternative method to export ggvis objects to PNG. Can be implemented on Linux or Windows.
Thanks
I found several ways, I'm posting the one that is more straight forward.
Required Libraries
library(ggvis)
library(XML)
library(webshot)
library(htmltools)
Generating the ggvis and printing the HTML local directory
outfile <- mtcars %>% ggvis(~wt, ~mpg) %>% layer_smooths() %>% set_options(width = 1200, height = 800) %>% view_static() %>% html_print( background = "white", viewer = getOption("viewer", utils::browseURL))
Parsing the HTML to a character vector
webst <- htmlParse(outfile, asText=FALSE)
showMethods(class=class(webst), where=search())
webst <- as(webst, "character")
class(webst)
Using regular expressions to search for the name of the SVG object inside the HTML code / Generating the HTML Selector
id <- webst %>% sub(pattern = "-container.*", replacement ="") %>% sub(pattern = ".*plot_", replacement ="")
selec <- paste0("#plot_", id, " > div > svg")
Using Webshot to capture a screenshot of the browser.
webshot(outfile, "test_webshot8.png", selector = selec)
Output
If you have suggestions how to simplify or a better method will be appreciated.
Following this post Gantt charts with R I stored my data in the data.frame using the same data structure as the link. Code is below (note the first 4 rows are auto generated by powerBI and I couldnt amend it.
df <- data.frame(task = c("task1", "task2", "task3"),
status = c("done", "active", "crit"),
pos = c("first_1", "first_2", "first_3"),
start = c("2014-01-06", "2014-01-09", "after first_2"),
end = c("2014-01-08", "3d", "5d"))
#Create dataframe
dataset <- data.frame(end, start, status, task, pos)
#Remove duplicated rows
# dataset <-unique(dataset)
df <- dataset
library(tidyr)
library(DiagrammeR)
library(dplyr)
mermaid(
paste0(
# mermaid "header", each component separated with "\n" (line break)
"gantt", "\n",
"dateFormat YYYY-MM-DD", "\n",
"title A Very Nice Gantt Diagram", "\n",
# unite the first two columns (task & status) and separate them with ":"
# then, unite the other columns and separate them with ","
# this will create the required mermaid "body"
paste(df %>%
unite(i, task, status, sep = ":") %>%
unite(j, i, pos, start, end, sep = ",") %>%
.$j,
collapse = "\n"
), "\n"
)
)
When running R script, I got the following error message.
No image was created. The R code did not result in creation of any visuals. Make sure your R script results in a plot to the R default device.
Please advice why this happened?
THank you
Peddie