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

Use the data rendered in output through DT::renderDataTable for further plotting

我开发了一个 shiny app,我在其中的每个选项卡中上传了一些 CSV 文件。在对上传的数据执行一些数学运算后,我得到了 N 个数据表作为输出。我正在使用 DT::renderDataTable 呈现这些数据表。 现在,假设我使用 DT::renderDataTable 渲染了 3 个不同的 datatables 我想使用这些数据表中渲染的输出来绘制组合图。 (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 的输入,但我闪亮的应用程序向我显示错误提示

Reading from shinyoutput object is not allowed.

我已经知道 'Reading from shinyoutput object is not allowed'。 我只想知道是否有任何方法可以使用数据表中呈现的输出在闪亮的应用程序中进一步绘图。

这是一个 MWE 演示我认为你想做的事情。

请注意数据与表示的分离:t1t2t3 是表示 CSV 文件的响应式。每个都在不同的数据中呈现 table。 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 文件可能值得一看。