【问题标题】:Render echart from uploading a csv file通过上传 csv 文件渲染 echart
【发布时间】:2021-09-20 06:28:12
【问题描述】:

我正在尝试制作一个应用程序,该应用程序将通过将 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)

【问题讨论】:

    标签: r shiny echarts4r


    【解决方案1】:

    代码中的语法错误很少,逻辑错误也很少。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 2022-10-05
      • 2019-12-21
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      相关资源
      最近更新 更多