【发布时间】:2018-05-31 19:41:08
【问题描述】:
我想在以闪亮的方式更新表格后制作一个情节(ggplot),但我似乎无法让它工作 - 情节没有出现。只有为 x 和 y 创建列后,该图才会出现。理想情况下,将显示为值的点编辑到表中。下面是我扩展的一些可重现的代码 (from here)。
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)
【问题讨论】:
-
我将标题修改为更具体,如果不适合您,请随时收回 :-)
标签: r ggplot2 shiny reactive-programming rhandsontable