簡體   English   中英

我如何 plot r shiny 中呈現的數據表中的兩個變量,其中一個是新創建的列?

[英]How can I plot two variables from a rendered Data Table in r shiny where one is a newly made column?

我正在使用 mtcars 數據集並創建了另一個隨機數 (x) * 2 的列。然后我使用 r shiny 中的 renderDataTable 來打印它。 我現在想在 new_col 列和任何其他列上使用 renderPlot({}) 。 我 go 如何調用該新列?

library(shiny)
library(ggplot2)

df<- mtcars


ui<- fluidPage(
      titlePanel("Mtcars"),
      sidebarLayout(
         sidebarPanel(
            selectInput(inputID = 'test', label = "TEST", choices = c(1,2,3,4), selected = 3)

     mainPanel(
         DT::dataTableOutput('table1')
         plotOutput('basicplot')

))


server <- function(input, output) {


      func<-function(x){
           df%>%
           mutate(new_col = x*2)


       output$table1 <- renderTable({
          func(2) 
      })
     
       output$basicplot <-renderPlot({     
           plot(* $new_col, *$mpg)    #what do i call the new dataframe with the new_col
      })
      
)


shinyApp(ui = ui, server = server)

     


你有很多語法錯誤。 請在以后發布問題之前檢查一下。 嘗試這個

library(shiny)
library(ggplot2)

df<- mtcars

ui<- fluidPage(
  titlePanel("Mtcars"),
  sidebarLayout(
    sidebarPanel(
      selectInput('test', label = "TEST", choices = names(df)[-1] )
      ),
      
      mainPanel(
        DTOutput('table1'),
        plotOutput('basicplot')
      )
  )
)

server <- function(input, output) {
  
  mydata <- reactive({
    df %>%
      mutate(new_col = as.numeric(df[[input$test]])*2)
  })
  
  output$table1 <- renderDT({
    mydata() 
  })
  
  output$basicplot <-renderPlot({
    req(mydata())
    df <- data.frame(mydata()$new_col,mydata()$mpg)
    plot(df)    
  })
  
}

shinyApp(ui = ui, server = server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM