pkgdown htmlwidget - Examples - r

I have tried pkgdown for many htmlwidgets but no example work. They all return the same error:
USArrests %>%
dplyr::mutate(
State = row.names(.),
Rape = -Rape
) %>%
e_charts(State) %>%
e_bar(Murder) %>%
e_bar(Rape, name = "Sick basterd", x.index = 1) # second y axis
#> Error in yaml.load(readLines(con), error.label = error.label, ...): argument "error.label" is missing, with no default
Reverting yaml package version did not help.
Upgrading to pandoc 2.1.3 did not help either.

Sadly htmlwidgets are not supported in pkgdown at the time of writing this, see issues 617 and 343.

Related

What is generating the error 'Can't subset `.data` outside of a data mask context' with 'dplyr'?

I have a huge shiny app which uses a huge package. I'm not the author of any of them and I'm a bit lost. A function (fermentationPlot) throws the error: Can't subset .data outside of a data mask context:
Warning: Error in fermentationPlot: Can't subset `.data` outside of a data mask context.
185: <Anonymous>
173: dplyr::arrange
172: dplyr::mutate
171: as.data.frame
What could be the cause of this error? What does it mean? Below is the code block which generates it. I googled this error message and I found that it can be fixed by downgrading 'dplyr'. I tried 1.0.10, 1.0.5 and 1.0.0, and the error always occurs.
plotInfo <- dplyr::left_join(
x = dplyr::select(
plotDefaults, -c(.data$templateName, .data$minValue, .data$maxValue)
),
y = plotSettings,
by = .data$dataName
) %>%
dplyr::arrange(!is.na(.data$order), -.data$order) %>%
dplyr::mutate(
color = replace(.data$color, .data$color == "Blue", "Dark blue"),
minValue = as.numeric(.data$minValue),
maxValue = as.numeric(.data$maxValue)
) %>%
as.data.frame()
The by argument of left_join must be a character vector of column names. Probably the author wanted to do
by = "dataName"
and not
by = .data$dataName

I get Error: invalid version specification ‘0,2’ when I use the function dm_draw() in r

I want to use the function dm_draw() to visualize a dm object but when I ran the command I get the error message " Error: invalid version specification ‘0,2’". I've tried the code included in the vignette "Visualizing dm objects" (https://cran.r-project.org/web/packages/dm/vignettes/tech-dm-draw.html) and I get the same error message when I run the dm_draw() function.
library(dm)
library(dplyr)
flights_dm_w_many_keys <- dm_nycflights13(color = FALSE)
dm_draw(flights_dm_w_many_keys)
I'm using dm version 0.2.7 and DiagrammeR 1.0.8. R version 4.1.2
I'm looking for a solution to visualize a dm object, it can be also different from dm_draw().
I hope someone can help me to get that done. Sorry for my broken English and thanks for your time, any type of help is appreciated.
You can use this code:
library(dm)
library(dplyr)
library(DiagrammeR)
library(DiagrammeRsvg)
# Use this function
dm_draw_svg = function(dm,...) {
if (!requireNamespace("DiagrammeRsvg", quietly = TRUE)) {
stop(
"Package \"DiagrammeRsvg\" must be installed to use this function.",
call. = FALSE
)
}
dm::dm_draw(dm = dm, ...) %>%
DiagrammeRsvg::export_svg() %>%
htmltools::HTML() %>%
htmltools::html_print()
}
flights_dm_w_many_keys <- dm_nycflights13(color = FALSE)
# plot
dm_draw_svg(flights_dm_w_many_keys)
Output:

When performing multisession work, future_lapply says that a package doesn't exist, but it works fine when running plan(sequential)

When I try to use future_apply with plan(multisession), it says that the package I'm trying to use doesn't exist. When I use plan(sequential) it works fine. I also get the same error when using plan(callr).
Here's the error:
Error in loadNamespace(name): there is no package called 'fuzzyjoin'
Can anyone help me figure out a solution or what's going wrong here?
I'm not sure if this is related to the future.apply package or future or globals packages as I know that they are also involved here.
Here's my code showing the issue:
library(fuzzyjoin)
library(future.apply)
#> Loading required package: future
library(dplyr)
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
iris_mod<- iris %>%
mutate(examplefield= Sepal.Width + Petal.Length,
Species = as.character(Species))
iristype <- iris_mod$Species %>% unique()
plan(sequential)
test_sequential <- future_lapply(iristype,
FUN = function(x) {
fuzzyjoin::fuzzy_left_join(
iris_mod %>% filter(Species %in% x),
iris_mod,
by = c("Species"="Species",
"examplefield"="Sepal.Length"),
match_fun = list(`==`, `<`)
)},
future.chunk.size= 2
)
plan(multisession)
test_multisession <- future_lapply(iristype,
FUN = function(x) {
fuzzyjoin::fuzzy_left_join(
iris_mod %>% filter(Species %in% x),
iris_mod,
by = c("Species"="Species",
"examplefield"="Sepal.Length"),
match_fun = list(`==`, `<`)
)},
future.chunk.size=2
)
#> Error in loadNamespace(name): there is no package called 'fuzzyjoin'
Created on 2022-01-28 by the reprex package (v2.0.1)
I'm running R v4.0.3 if that's relevant.
I ran the following code and found that the library paths weren't being passed correctly for some reason. My dirty fix was just to make sure the packages were installed on the libPath where future was looking.
install.packages("fuzzyjoin", lib= "C:/Program Files/R/R-4.0.3/library" )
Here's the code that I ran to discover my normal session and future_lapply/future session were using different library paths:
.libPaths()
# [1] "\\\\networkfileservername/Userdata/myusername/Home/R/win-library/4.0" "C:/Program Files/R/R-4.0.3/library"
f_libs%<-% .libPaths()
print(f_libs)
# [1] "C:/Program Files/R/R-4.0.3/library"

Package environments are not working as expected in a pipeline

In a package I'm working on, I'm using environments to save and retrieve the labels of a dataframe.
In a magrittr pipeline, I want to save them in an environment variable which I would retrieve later.
However, I'm facing a problem: it seems as if the environment variables were not modified until the end of the pipeline.
Here is an example, with most of useful functions:
devtools::install_github("DanChaltiel/crosstable", build_vignettes=TRUE)
library(crosstable) #for functions set_label() and get_label() but you can test
#with other label-management packages (Hmisc, expss...)
labels_env = rlang::new_environment()
save_labels = function(.tbl){
labels_env$last_save = tibble(
name=names(.tbl),
label=get_label(.tbl)[.data$name]
)
invisible(.tbl)
}
get_last_save = function(){
labels_env$last_save
}
import_labels = function(.tbl){
data_label = get_last_save()
for(i in 1:nrow(data_label)){
name = as.character(data_label[i, name_from])
label = as.character(data_label[i, label_from])
.tbl[name] = set_label(.tbl[name], label)
}
.tbl
}
This works exactly as intended, as label for disp would be NULL otherwise:
library(dplyr)
library(crosstable)
save_labels(mtcars2)
mtcars2 %>%
transmute(disp=as.numeric(disp)+1) %>% #removes the label attribute of disp
import_labels() %>% #
crosstable(disp)
#> .id label variable value
#> 1 disp Displacement (cu.in.) Min / Max 72.1 / 473.0
#> 2 disp Displacement (cu.in.) Med [IQR] 197.3 [121.8;327.0]
#> 3 disp Displacement (cu.in.) Mean (std) 231.7 (123.9)
#> 4 disp Displacement (cu.in.) N (NA) 32 (0)
Created on 2021-01-26 by the reprex package (v0.3.0)
However, save_labels(mtcars2) returns mtcars2 invisibly so I'd like to be able to pipe the whole sequence. Unfortunately, this throws an error:
library(dplyr)
library(crosstable)
mtcars2 %>%
save_labels() %>%
transmute(disp=as.numeric(disp)+1) %>%
import_labels() %>% #
crosstable(disp)
#> Error in .subset2(x, i, exact = exact): attempt to select less than one element in get1index
Created on 2021-01-26 by the reprex package (v0.3.0)
Indeed, when using pipes, the environment variable is not set yet when we get to import_labels(). If I re-run this code, it won't throw any error but that would be misleading as it would refer to the previous value of labels_env$last_save.
My understanding of pipes is not good enough to get this working. Moreover, it seems to be specific to the package environment, as I could not reproduce this behavior in a plain R script.
Is there a way I can use pipes with such an environment variable inside a package?
This was actually caused by a breaking change when the package magrittr (which provides pipes) went from v1.5 to v2.0.
This was explained on the blog and on NEWS.md.
A more specific reproducible example can be found on this GitHub issue.
In the new magrittr version, the evaluation sequenced has changed, so for side effects to happen in the correct order, you have to force the evaluation:
import_labels = function(.tbl){
force(.tbl) #force evaluation
data_label = get_last_save()
for(i in 1:nrow(data_label)){
name = as.character(data_label[i, name_from])
label = as.character(data_label[i, label_from])
.tbl[name] = set_label(.tbl[name], label)
}
.tbl
}

Problems installing github

Hi I have a problem with github package.
This is my code:
Installing packages:
install.packages("magrittr")
library(magrittr)
install.packages("ggplot2")
library(ggplot2)
install.packages("cowplot")
library(cowplot)
install.packages("usethis")
library(usethis)
install.packages("devtools")
library(devtools)
Here comes the error , when I try to force the installation:
There are 4 options:
1: All
2: CRAN packages only
3: None
4: tibble (2.1.3 -> 3.0.0) [CRAN]
When I press 1 I get this error message:
Installing 1 packages: tibble
Installing package into ‘C:/Users/josem/OneDrive/Documents/R/win-library/3.6’
(as ‘lib’ is unspecified)
There is a binary version available but the source version is later:
binary source needs_compilation
tibble 2.1.3 3.0.0 TRUE
Error: Failed to install 'JLutils' from GitHub:
(converted from warning) package ‘tibble’ is in use and will not be installed
devtools::install_github("larmarange/JLutils", force= TRUE)
install.packages("tidyverse")
library(tidyverse)
library(plyr)
library(dplyr)
library(JLutils)
library(tidyverse)
install.packages("rio")
library(rio)
Then when I choose option 3 I can download jutils package but I can not create dat2 dataframe. I don't know why. It's really frustrating ( yesterday it worked perfectly)`
dat <- rio::import("https://github.com/jincio/COVID_19_PERU/blob/master/docs/reportes_minsa.xlsx?raw=true")
dat1 <- dat %>%
mutate(pos_new = Positivos-lag(Positivos,default = 0),
des_new = Descartados-lag(Descartados,default = 0)) %>%
group_by(Dia) %>%
summarise(pos_new = sum(pos_new), des_new = sum(des_new)) %>%
mutate(cum_pos = cumsum(pos_new),
tot_pruebas = pos_new+des_new)
Here is the error:
(creating dat2):
Error in .f(.x[[i]], ...) : object 'Dia' not found
dat2 <- dat1 %>%
mutate(neg_new = tot_pruebas-pos_new) %>%
dplyr::select(Dia, pos_new, neg_new) %>%
rename(Positivo = pos_new, Negativo = neg_new) %>%
gather(res, count, -Dia) %>%
uncount(count)
I tried everything :(

Resources