【问题标题】:r shiny rhandsontable: create empty table for copy & paste of datar shiny rhandsontable:为复制和粘贴数据创建空表
【发布时间】:2018-12-11 21:01:54
【问题描述】:

我尝试使用 rhandsontable 构建一个闪亮的应用程序。
当首先运行应用程序时,应该呈现一个空表,然后可以通过复制和粘贴来填充数据。粘贴数据后,应呈现值图。
我的代码如下:

library(shiny)
library(rhandsontable)
library(data.table)

X = c("","","")
Y = c("","","")

daten = data.table(X, Y)

ui <- fluidPage(

  rHandsontableOutput("tabelle"),

  plotOutput("grafik")
 )

server <- function(input, output) {

  data <- reactiveValues(values=daten)

  output$tabelle <- renderRHandsontable({
    rhandsontable(data$values)
  })

  observeEvent(input$tabelle, {
    data$values <- hot_to_r(input$tabelle)
  })

  output$grafik <- renderPlot({
    if(is.null(data$values)) return(NULL)
    plot(data$values)
    })
}

shinyApp(ui, server)  

该应用程序正常运行,但给出了错误消息

Warning: Error in plot.window: need finite 'xlim' values  

在控制台中并在浏览器窗口中显示错误消息:

Error: need finite 'xlim' values

将数据粘贴到表格中时,错误消息消失。
我认为错误是由于第一次调用绘图函数时缺少数据。在 github 我找到了要添加的建议

if(is.null(data$values)) return(NULL)  

在 renderPlot 函数中,但不幸的是这没有帮助。
如果有人可以帮助我用 rhandsontable 创建一个空表,我会非常高兴,该表可以通过复制和粘贴来填充数据。

【问题讨论】:

    标签: r shiny rhandsontable


    【解决方案1】:

    我知道你很久以前就问过这个问题,但由于目前还没有提供解决方案,所以让我提出一个。

    回答您的主要问题:为什么您会收到错误消息 Warning: Error in plot.window: need finite 'xlim' values?

    答案是:因为您要求R 绘制没有值的东西。你测试if(is.null(data$values))。这可能是一个解决方案。但是,您设置了data$values &lt;- hot_to_r(input$tabelle)。因此,它不是NULL。

    我可以建议您按如下方式更改您的代码(尤其是因为data 是一个内置的R 函数,因此将data 用于您的输入值会产生误导):

    library(shiny)
    library(rhandsontable)
    library(data.table)
    
    X <- c("", "", "")
    Y <- c("", "", "")
    
    daten <- data.table(X, Y)
    
    ui <- fluidPage(
      rHandsontableOutput(outputId = "tabelle"),
      plotOutput(outputId = "grafik")
    )
    
    server <- function(input, output){
      data.in <- reactiveValues(values = daten)
      output$tabelle <- renderRHandsontable({
        rhandsontable(data.in$values)
      })
    
      observeEvent(eventExpr = input$tabelle, {
        data.in$values <- hot_to_r(input$tabelle)
        output$grafik <- renderPlot({
          if(!is.null(tryCatch(plot(data.in$values), error = function(e){})))
            {plot(data.in$values)}
        })
      })
    }
    
    shinyApp(ui, server)
    

    tryCatch 允许您评估表达式,检查是否发生错误,如果是,则 error = function(e){} 删除 NULL 并且没有任何反应,如果没有,则将有一个情节。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 2023-04-03
      • 2021-10-14
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      相关资源
      最近更新 更多