R 雷汉松表;闪亮:根据添加的列创建条件打印

R 雷汉松表;闪亮:根据添加的列创建条件打印,r,ggplot2,shiny,reactive-programming,rhandsontable,R,Ggplot2,Shiny,Reactive Programming,Rhandsontable,我想在更新了一个闪亮的表格后制作一个绘图(ggplot),但我似乎无法使它工作-绘图没有出现。只有在为x和y创建列后,才会出现绘图。理想情况下,点显示为编辑到表中的值。下面是一些可复制的代码(),我对其进行了扩展 library(rhandsontable) library(tidyverse) ui <- fluidPage( h2("The mtcars data"), rHandsontableOutput("mytable"), textInput('NewCol',

我想在更新了一个闪亮的表格后制作一个绘图(ggplot),但我似乎无法使它工作-绘图没有出现。只有在为x和y创建列后,才会出现绘图。理想情况下,点显示为编辑到表中的值。下面是一些可复制的代码(),我对其进行了扩展

library(rhandsontable)
library(tidyverse)

ui <- fluidPage(
  h2("The mtcars data"),
  rHandsontableOutput("mytable"),
  textInput('NewCol', 'Enter new column name'),
  radioButtons("type", "Column type:",
               c("Integer" = "integer",
                 "Floating point" = "numeric",
                 "Text" = "character")),
  actionButton("goButton", "Update Table"),
  plotOutput("plot")
)

server <- function(input, output) {

  # g <- reactiveValues(d=NULL) #define it ouside

  mydata <- mtcars[1:5,]
  output$mytable = renderRHandsontable(df())
  df <- eventReactive(input$goButton, {
    if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
      if (input$type == "integer") v1 <- integer(NROW(mydata))
      if (input$type == "numeric") v1 <- numeric(NROW(mydata))
      if (input$type == "character") v1 <- character(NROW(mydata))
      newcol <- data.frame(v1)
      names(newcol) <- input$NewCol
      mydata <<- cbind(mydata, newcol)
    }
    rhandsontable(mydata, stretchH = "all")
  }, ignoreNULL = FALSE)
  observe(if (!is.null(input$mytable)) mydata <<- hot_to_r(input$mytable))

  output$plot <- renderPlot({
    if (req(mydata$x) >= 0 & req(mydata$y) >= 0) 
      ggplot(mydata, aes(x=mydata$x,y=mydata$y)) +
        geom_point()
    # else if (req(mydata$x) = 0 & req(mydata$y) = 0) {
    #   print("empty")
    # }
  })  
}

shinyApp(ui,server)
库(rhandsontable)
图书馆(tidyverse)

ui我不知道这些功能是如何工作的(
rhandsontable
hot\u to\u r
),但您想要做的似乎与经典的
reactiveValues
框架兼容,就像您的服务器代码中这样:

r = reactiveValues(mydata=mtcars[1:5,])
  output$mytable = renderRHandsontable(df())
  df <- eventReactive(input$goButton, {
    if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
      if (input$type == "integer") v1 <- integer(NROW(r$mydata))
      if (input$type == "numeric") v1 <- numeric(NROW(r$mydata))
      if (input$type == "character") v1 <- character(NROW(r$mydata))
      newcol <- data.frame(v1)
      names(newcol) <- input$NewCol
      r$mydata <- cbind(r$mydata, newcol)
    }
    rhandsontable(r$mydata, stretchH = "all")
  }, ignoreNULL = FALSE)
  observe({if (!is.null(input$mytable)) r$mydata <- hot_to_r(input$mytable)})
  output$plot <- renderPlot({
    if(is.null(r$mydata$x) | is.null(r$mydata$y)) {return(NULL)}
      ggplot(r$mydata, aes(x=x,y=y)) +
      geom_point()})  }
r=reactiveValues(mydata=mtcars[1:5,])
输出$mytable=renderHandsOnTable(df())
df(0){

如果(input$type==“integer”)v1i将标题修改得更具体,如果它不适合您,请随意取回:-)