R Shiny:计算 "selectInput" 选择的新变量

R Shiny: Computing new Variables selected by "selectInput"

我正在使用 Shiny 开发仪表板,并希望根据 selectInput.
选择的 Variabels 计算新变量 与普通 R 代码中的这个相当:

library(dplyr) 
new_df <- old_df %>% mutate(new_1 = old_var1 + old_var2)

我可以使用 sliderInput 计算新值,但这只是单个值。我想计算一个空洞新变量,并提供在表格和图形中显示新变量的所有机会。

请尝试以下语法(数据在线可用)。 正如您所提到的,所有输入都在正常工作。

 library(shiny)
 library(readr)
 library(ggplot2)
 library(stringr)
 library(dplyr)
 library(DT)
 library(tools)
 load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))

ui <- fluidPage( 

 sidebarLayout(

# Inputs
sidebarPanel(
  h3("Plotting"),      # Third level header: Plotting

  # Select variable for y-axis 
  selectInput(inputId = "y", 
              label = "Y-axis:",
              choices = c("IMDB rating" = "imdb_rating", 
                          "IMDB number of votes" = "imdb_num_votes", 
                          "Critics Score" = "critics_score", 
                          "Audience Score" = "audience_score", 
                          "Runtime" = "runtime"), 
              selected = "audience_score"),

  # Select variable for x-axis 
  selectInput(inputId = "x", 
              label = "X-axis:",
              choices = c("IMDB rating" = "imdb_rating", 
                          "IMDB number of votes" = "imdb_num_votes", 
                          "Critics Score" = "critics_score", 
                          "Audience Score" = "audience_score", 
                          "Runtime" = "runtime"), 
              selected = "critics_score"),

  # Select variable for color
  selectInput(inputId = "z", 
              label = "Color by:",
              choices = c("Title Type" = "title_type", 
                          "Genre" = "genre", 
                          "MPAA Rating" = "mpaa_rating", 
                          "Critics Rating" = "critics_rating", 
                          "Audience Rating" = "audience_rating"),
              selected = "mpaa_rating"),

  hr(),

  # Set alpha level
  sliderInput(inputId = "alpha", 
              label = "Alpha:", 
              min = 0, max = 1, 
              value = 0.5),

  # Set point size
  sliderInput(inputId = "beta", 
              label = "Beta:", 
              min = 0, max = 5, 
              value = 2)
  ),

# Output:
mainPanel(plotOutput(outputId = "scatterplot"),
          textOutput(outputId = "description"),
          DT::dataTableOutput("moviestable"))
)  
)  
server <- function(input, output, session) {

output$scatterplot <- renderPlot({
ggplot(data = movies, aes_string(x = input$x, y = input$y,
                                          color = input$z)) +
  geom_point(alpha = input$alpha, size = input$beta) +
  labs(x = toTitleCase(str_replace_all(input$x, "_", " ")),
       y = toTitleCase(str_replace_all(input$y, "_", " ")),
       color = toTitleCase(str_replace_all(input$z, "_", " ")))
 })  
vals <- reactiveValues()
observe({
vals$x <- input$alpha
vals$y <- input$beta
vals$sum <- vals$x + vals$y
  })

output$description <- renderText({
paste0("Alpha: ",input$alpha, " Beta:", input$beta," and the sum of alpha and beta:",vals$sum, ".")
})


output$moviestable <- DT::renderDataTable({
DT::datatable(data = movies, 
              options = list(pageLength = 10), 
              rownames = FALSE)
})



}


shinyApp(ui = ui, server = server)

我尝试了不同的方法来解决这个问题:

第一次尝试:

vals2 <- reactiveValues()
observe({
vals2$x <- input$y
vals2$y <- input$x
vals2$sum <- vals2$x + vals2$y
})

output$description2 <- renderText({
paste0("Input y: ",input$y, " Input x:", input$x," and the sum of both variables is:",vals2$sum, ".")
})

警告:+ 错误:二元运算符的非数字参数 堆栈跟踪(从最里面开始): 56:observerFunc [C:/Users/XXXXXX/Desktop/app.R#110] 1:运行应用 错误:[on_request_read] 连接被 peer

重置

第二次尝试:

output$try2 <- renderUI({
movies_2 <- movies %>% mutate(new_1 = input$y + input$x)
})

output$moviestable2 <- DT::renderDataTable({
DT::datatable(data = movies_2,
options = list(pageLength = 10),
rownames = FALSE)
})

警告:继承错误:未找到对象'movies_2'

我不知道下一步可以尝试什么...

我很高兴能得到各种帮助!

你应该 movies_2 反应。您的 output$try2 将无法工作,因为它需要 UI 个对象。

为了匹配您在 UI 端所做的调用,我已重命名回 moviestable 并将 input$x + input$y 更改为 paste0(input$y, input$x) 因为他们都是性格。

movies_2 <- reactive({
  movies %>% mutate(new_1 := movies[[input$x]] + movies[[input$y]])
})

output$moviestable <- DT::renderDataTable({
  DT::datatable(data = movies_2(),
    options = list(pageLength = 10),
    rownames = FALSE)
})