简体   繁体   中英

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

I am using the mtcars dataset and have created another column that is a random number(x) * 2. I have then used the renderDataTable in r shiny to print it. I now want to use renderPlot({}) on the new_col column and any other column. How would I go about calling that new column?

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)

     


You had numerous syntax errors. Please check it before posting a question in the future. Try this

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM