rshinyechartsquartoecharts4r

Cannot find function renderEcharts4r in Shiny Quarto document


Round 2. I am creating a quarto/shiny doc that has an echart graphic in it with a simple radio button input widget,and the error that I am running into now is a 'Error in renderEcharts4r({ : could not find function "renderEcharts4r" '. The function does exist in the package though when I am looking through the functions in the packages pane!

Below is a reproducible example. What is the issue here? I've also tried downloading the package using devtools but I get the same error.

Thanks

EDIT to include versions:
echarts4r 0.4.5 (downloaded from CRAN)
R version 4.3.2

---
title: "Test"
format: html
server: shiny
execute:
  echo: false
  warning: false
---


```{r}

#| label: setup

library(dplyr)
library(echarts4r)
library(shinyWidgets)
library(lubridate)


data("USAccDeaths")
USAccDeaths <- data.frame(as.matrix(USAccDeaths), date=time(USAccDeaths))
USAccDeaths$year <- trunc(USAccDeaths$date)
USAccDeaths$month <- (USAccDeaths$date - USAccDeaths$year) * 12 + 1
colnames(USAccDeaths)[1] <- 'Deaths'

```

```{r}

radioGroupButtons(
  inputId = "time_period",
  label = "Choices", 
  choices = c("Month", "Year"),
  status = "primary"
)

echarts4rOutput("plot1")

```

```{r}
#| context: server

deaths <- reactive({
  USAccDeaths %>% 
    group_by(tolower(input$time_period)) %>% 
    summarise('Deaths' = sum(deaths))
})


output$plot1 <- renderEcharts4r({
  deaths |> 
    e_charts(x = tolower(input$time_period)) |> # initialise and set x
    e_line(serie = Deaths, smooth = TRUE) |>
    e_tooltip(trigger = "axis")
  
})

```

Solution

  • The first issue is the same as with your former question: Remove the empty line between the backticks and #| label: setup. Besides of this there are several other issues in your code. First, it should be sum(Deaths) instead of sum(deaths). Second, as deaths is a reactive you have to add () to access its value in renderEcharts4r. Third, input$time_period is a character string and will not work in group_by. Instead you have to wrap in the .data pronoun or use across or ... . Finally, to simplify a bit I pass a named vector to the choices= argument to get rid of tolower() and in the group_by I create a new column time_period which could then be used when creating the chart.

    ---
    title: "Test"
    format: html
    server: shiny
    execute:
      echo: false
      warning: false
    ---
    
    
    ```{r}
    #| label: setup
    library(dplyr)
    library(echarts4r)
    library(shinyWidgets)
    library(lubridate)
    
    data("USAccDeaths")
    USAccDeaths <- data.frame(as.matrix(USAccDeaths), date = time(USAccDeaths))
    USAccDeaths$year <- trunc(USAccDeaths$date)
    USAccDeaths$month <- (USAccDeaths$date - USAccDeaths$year) * 12 + 1
    colnames(USAccDeaths)[1] <- "Deaths"
    ```
    
    ```{r}
    radioGroupButtons(
      inputId = "time_period",
      label = "Choices",
      choices = c(Month = "month", Year = "year"),
      status = "primary"
    )
    
    echarts4rOutput("plot1")
    ```
    
    ```{r}
    #| context: server
    
    deaths <- reactive({
      USAccDeaths %>%
        group_by(time_period = .data[[input$time_period]]) %>%
        summarise(Deaths = sum(Deaths))
    })
    
    
    output$plot1 <- renderEcharts4r({
      deaths() |>
        e_charts(
          x = time_period
        ) |>
        e_line(serie = Deaths, smooth = TRUE) |>
        e_tooltip(trigger = "axis")
    })
    ```
    

    enter image description here