使用通过DT :: renderDataTable在输出中呈现的数据进行进一步绘制

时间:2020-08-07 20:05:05

标签: r ggplot2 shiny dt

我已经开发了shiny app,在其中每个标签中都上传了许多CSV文件。在对上传的数据执行一些数学运算之后,我得到了N个数据表作为输出。我正在使用DT::renderDataTable渲染这些数据表。 现在,假设我使用datatables渲染了3个不同的DT::renderDataTable,我想使用这些数据表中渲染的输出来绘制组合图。 (3个geom_line()彼此重叠)

这是我在数据表中呈现数据的方式:

output$Data_FSC <- DT::renderDataTable({
          x1 <- data2()[, c(input$xcol2, input$ycol2)]
          M <- x1
          #calculate rotation angle
          alpha <- -atan((M[1,2]-tail(M,1)[,2])/(M[1,1]-tail(M,1)[,1]))
          #rotation matrix
          rotm <- matrix(c(cos(alpha),sin(alpha),-sin(alpha),cos(alpha)),ncol=2)
          #shift, rotate, shift back
          M2 <- t(rotm %*% (t(M)-c(M[1,1],M[1,2]))+c(M[1,1],M[1,2]))
          M2[nrow(M2),2] <- M2[1,2]
          M2
          
          d_f3 <- data.frame(x = M2[,1], y = (M2[,2]-min(M2[1,2])))
          
          v_f1 <- subset(d_f3,  y > ((input$below2)/1000) & y < ((input$above2)/1000), select = c(x,y))
          
          fla_upper2 <- lm(formula = y+((input$Upper_Poly_Limit2)/1000000) ~ poly(x,input$degree2, raw = TRUE), v_f1)
          fla_lower2 <- lm(formula = y-((input$Lower_Poly_Limit2)/1000000) ~ poly(x,input$degree2, raw = TRUE), v_f1)
          
          v_f1$upper2 <- predict(fla_upper2, newdata=v_f1)
          v_f1$lower2 <- predict(fla_lower2, newdata=v_f1)
          
          v_f1$region2 <- ifelse(v_f1[,2] <= v_f1$upper2 & v_f1[,2] >= v_f1$lower2, 'inside', 'outside')
          
          kl <- subset(v_f1, region2 =='inside')
          
          g <- ggplot() + theme_bw() +
            geom_smooth(data = kl, aes_string(kl[,1], kl[,2]), formula = y ~ poly(x,input$degree_2, raw = TRUE), method = "lm", color = "green3", level = 1, size = 0.5)
          
          r <- ggplot_build(g)$data[[1]]
          q <- data.frame(x = r[,1], y = r[,2])
          
          #calculate rotation angle
          beta <- -atan((q[1,2]-tail(q,1)[,2])/(q[1,1]-tail(q,1)[,1]))
          #rotation matrix
          rot_m <- matrix(c(cos(beta),sin(beta),-sin(beta),cos(beta)),ncol=2)
          #shift, rotate, shift back
          M_2 <- t(rot_m %*% (t(q)-c(q[1,1],q[1,2]))+c(q[1,1],q[1,2]))
          M_2[nrow(M_2),2] <- M_2[1,2]
          M_2
          
          M_3 <- data.frame(x= (M_2[,1]-median(M_2[,1])), y= (M_2[,2]-min(M_2[,2])))
          the_data <- reactive(M_3)
          the_data()
        })

我尝试将DT::renderDataTable的输出作为ggplot的输入,但是我闪亮的应用程序向我显示一条错误消息

不允许从shinyoutput对象进行读取。

我已经知道“不允许从Shinyoutput对象读取”。 我只想知道是否可以使用数据表中呈现的输出在闪亮的应用程序中进行进一步的绘制。

1 个答案:

答案 0 :(得分:1)

这里有一个MWE,展示了我认为您想做的事。

请注意将数据与演示文稿分开:t1t2t3是表示您的CSV文件的反应性。每个都呈现在不同的数据表中。 allData是CSV数据的反应式包含并集。用作绘图的源数据。

library(shiny)
library(DT)
library(tidyverse)

ui <- fluidPage(
  numericInput("n", "Number of points:", min=2, max=20, value=10),
  plotOutput("plot"),
  dataTableOutput("table1"),
  dataTableOutput("table2"),
  dataTableOutput("table3")
)

server <- function(input, output) {
  t1 <- reactive({ tibble(x=1:input$n, y=rnorm(input$n), key="Table 1") })
  t2 <- reactive({ tibble(x=1:input$n, y=rnorm(input$n), key="Table 2") })
  t3 <- reactive({ tibble(x=1:input$n, y=rnorm(input$n), key="Table 3") })
  allData <- reactive({ bind_rows(t1(), t2(), t3()) })
  
  output$table1 <- renderDT({ t1() })
  output$table2 <- renderDT({ t2() })
  output$table3 <- renderDT({ t3() })
  output$plot <-renderPlot({ allData() %>% ggplot() + geom_line(aes(x=x, y=y, colour=key)) })
}

shinyApp(ui = ui, server = server)

使用modules来管理和显示CSV文件可能值得一看。

相关问题