【问题标题】:Display R Shiny Plot After Inputting File输入文件后显示 R Shiny Plot
【发布时间】:2018-01-31 00:48:24
【问题描述】:

我想根据用户通过文件输入的数据显示图表(用于 Shiny 应用程序)。在当前设置下,会出现一条错误消息,声称未找到数据,因此(来自 rCharts 包的)图不会显示。

下面附上代码:

ui.R

library(rCharts)
library(shinydashboard)
library(shiny)
dashboardPage(
  skin = "black",
  header <- dashboardHeader(
    titleWidth = 475
  ),
  sidebar <- dashboardSidebar(
    sidebarMenu(
    )    
  ),
  body <- dashboardBody(
    tabItems(
      tabItem("setup",
              box(width = 4,title = tags$b('Input Dataset'), solidHeader = T, status = 'primary', collapsible = T,
                  helpText("Default max. file size is 5 MB. Please upload both files for analysis in csv format."),
                  fileInput("file1","Upload the first file"),
                  fileInput("file2","Upload the second file")
              ),                  
              box(height = 500, width = 12,title = tags$b('Visualize Data'), solidHeader = T, status = 'primary',
                  showOutput("myPlot", "Highcharts")                  
              )
      )
    )
  )
)

服务器.R

library(shiny)
library(rCharts)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  observe({
    file1 = input$file1
    file2 = input$file2
    if (is.null(file1) || is.null(file2)) {
      return(NULL)
    }
    data1 = read.csv(file1$datapath)
    data2 = read.csv(file2$datapath)
  })
  output$myPlot<-renderChart2({
    # Prepare data

    data1[,2] <- (data1[,2])/sum(data1[,2])

    # Create chart
    a <- rCharts:::Highcharts$new()
    a$chart(type = "column")
    a$xAxis(categories = rownames(x))
    a$yAxis(title = list(text = "Normalized Intensity"))
    a$data(data1)
    a$set(width = 600, height = 500)
    return(a)
  })
})

【问题讨论】:

    标签: r shiny rcharts


    【解决方案1】:

    尝试添加类似的内容。确保检查 nrow 并返回并清空 Highcharts$new() 对象,因为 renderChart2 需要一个。

    library(shiny)
    library(rCharts)
    # Define server logic required to draw a histogram
    shinyServer(function(input, output) {
    
      data1 <- reactive({read.csv(file1$datapath)})
      data2 <- reactive({read.csv(file2$datapath)})
    
      output$myPlot<-renderChart2({
        data1 <- data1()
        # Prepare data
        if(nrow(data1==0)){return(Highcharts$new())}
        data1[,2] <- (data1[,2])/sum(data1[,2])
    
        # Create chart
        a <- rCharts:::Highcharts$new()
        a$chart(type = "column")
        a$xAxis(categories = rownames(x))
        a$yAxis(title = list(text = "Normalized Intensity"))
        a$data(data1)
        a$set(width = 600, height = 500)
        return(a)
      })
    })
    

    【讨论】:

    • 即使进行了这种修改,我仍然得到“找不到对象'file1'”。我错过了什么吗?
    猜你喜欢
    • 2017-09-18
    • 2014-12-06
    • 1970-01-01
    • 2021-09-08
    • 2016-05-10
    • 2018-07-08
    • 2019-02-05
    • 1970-01-01
    • 2020-06-27
    相关资源
    最近更新 更多