通过上传 csv 文件渲染 echart

Render echart from uploading a csv file

我正在尝试制作一个应用程序,通过将 csv 文件上传到它来呈现图表,您可以在其中选择要绘制图表的变量。事实是我不知道我做错了什么,因为该应用程序不呈现图形。有什么想法或建议吗?

密码是:

library(shiny)
library(echarts4r)

ui <- fluidPage(
  
  selectInput('mydropdown', label = 'Selección de variables', choices = 'Aún no hay variables a escoger'),
  selectInput('mydropdown1', label = 'Selección de variables', choices = 'Aún no hay variables a escoger'),
  fileInput('myfileinput', label = 'Archivo a usar', accept = c(".csv")),
  echarts4rOutput("plot")

)

   #Server
   server <- function(input, output, session) {
  
   observeEvent(input$myfileinput, {
    
    mytable <- read.csv(input$myfileinput$datapath)
    
    updateSelectInput(session, "mydropdown", label = "Select", choices = colnames(mytable))
    updateSelectInput(session, "mydropdown1", label = "Select", choices = colnames(mytable))
    
  })
  
  mytable <- renderEcharts4r({
    myfileinput |> 
      e_charts(input$mydropdown)  |> 
      e_line(input$mydropdown1)
  })
}
shinyApp(ui, server)

代码中几乎没有语法错误和逻辑错误。

  1. 您应该将数据存储在一个反应​​对象中,该对象可以在应用程序的任何地方使用。

  2. 剧情应该保存在output$plot对应echarts4rOutput("plot").

  3. 因为您要将列名的字符值传递给 echarts,所以请使用函数 e_charts_e_line_.

尝试以下 -

library(shiny)
library(echarts4r)

ui <- fluidPage(
  
  selectInput('mydropdown', label = 'Selección de variables', choices = 'Aún no hay variables a escoger'),
  selectInput('mydropdown1', label = 'Selección de variables', choices = 'Aún no hay variables a escoger'),
  fileInput('myfileinput', label = 'Archivo a usar', accept = c(".csv")),
  echarts4rOutput("plot")
  
)

#Server
server <- function(input, output, session) {
  
  rv <- reactiveValues()
  
  observeEvent(input$myfileinput, {
    
    rv$mytable <- read.csv(input$myfileinput$datapath)
    
    updateSelectInput(session, "mydropdown", label = "Select", choices = colnames(rv$mytable))
    updateSelectInput(session, "mydropdown1", label = "Select", choices = colnames(rv$mytable))
    
  })
  
  output$plot <- renderEcharts4r({
    req(rv$mytable)
    rv$mytable |> 
      e_charts_(input$mydropdown)  |> 
      e_line_(input$mydropdown1)
  })
}

shinyApp(ui, server)