在Shiny夸托文档中找不到函数renderEcharts4r

atmip9wb  于 5个月前  发布在  Echarts
关注(0)|答案(1)|浏览(66)

第2轮。我正在创建一个夸托/shiny文档,其中有一个echart图形,带有一个简单的单选按钮输入小部件,我现在遇到的错误是“Error in renderEcharts 4 r({:could not find function“renderEcharts 4 r”“.”该函数确实存在于包中,但当我查看包窗格中的函数时!
下面是一个可重复的例子。这里的问题是什么?我也尝试使用devtools下载包,但我得到了同样的错误。
谢谢

编辑以包含版本:

echarts 4 r 0.4.5(从CRAN下载)
R版本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'

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

echarts4rOutput("plot1")
#| 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")
  
})
字符串
py49o6xq

py49o6xq1#

第一个问题与您之前的问题相同:删除反引号和#| label: setup之间的空行。除此之外,代码中还有其他几个问题。首先,它应该是sum(Deaths)而不是sum(deaths)。其次,由于deathsreactive,因此必须添加()才能访问renderEcharts4r中的值。第三,input$time_period是一个字符串,在group_by中不起作用。相反,您必须使用.data代词或使用across或...。最后,为了简化一点,我将一个命名向量传递给choices=参数以摆脱tolower(),并在group_by中创建一个新列time_period,可以在创建图表时使用。

---
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"
radioGroupButtons(
  inputId = "time_period",
  label = "Choices",
  choices = c(Month = "month", Year = "year"),
  status = "primary"
)

echarts4rOutput("plot1")
#| 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")
})
字符串

![](https://i.stack.imgur.com/fQ3lp.png)

相关问题